add output-file flag

This commit is contained in:
Bobby Brennan
2019-04-17 22:06:18 +00:00
parent a505313d63
commit 38ddba2d9f

54
main.go
View File

@@ -50,7 +50,8 @@ func main() {
audit := flag.Bool("audit", false, "Runs a one-time audit.")
dashboardPort := flag.Int("dashboard-port", 8080, "Port for the dashboard webserver")
webhookPort := flag.Int("webhook-port", 9876, "Port for the webhook webserver")
auditDestination := flag.String("audit-destination", "", "Destination URL to send audit results (prints to stdout if unspecified)")
auditOutputURL := flag.String("output-url", "", "Destination URL to send audit results")
auditOutputFile := flag.String("output-file", "", "Destination file for audit results")
configPath := flag.String("config", "config.yaml", "Location of Fairwinds configuration file")
var disableWebhookConfigInstaller bool
@@ -74,7 +75,7 @@ func main() {
} else if *dashboard {
startDashboardServer(c, *dashboardPort)
} else if *audit {
runAudit(c, *auditDestination)
runAudit(c, *auditOutputFile, *auditOutputURL)
}
}
@@ -164,36 +165,45 @@ func startWebhookServer(c conf.Configuration, disableWebhookConfigInstaller bool
}
}
func runAudit(c conf.Configuration, destination string) {
func runAudit(c conf.Configuration, outputFile string, outputURL string) {
k, _ := kube.CreateKubeAPI()
auditData, err := validator.RunAudit(c, k)
if err != nil {
panic(err)
}
if destination != "" {
jsonData, err := json.Marshal(auditData)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", destination, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
glog.Println(string(body))
} else {
if outputURL == "" && outputFile == "" {
y, err := yaml.Marshal(auditData)
if err != nil {
panic(err)
}
fmt.Println(string(y))
} else {
jsonData, err := json.Marshal(auditData)
if err != nil {
panic(err)
}
if outputFile != "" {
err := ioutil.WriteFile(outputFile, []byte(jsonData), 0644)
if err != nil {
panic(err)
}
}
if outputURL != "" {
req, err := http.NewRequest("POST", outputURL, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
glog.Println(string(body))
}
}
}