Skip to content

Commit

Permalink
Pretty print exported dashboards to output (elastic#9925)
Browse files Browse the repository at this point in the history
From now on exported dashboards are pretty printed to all outputs.
  • Loading branch information
kvch authored Jan 9, 2019
1 parent e09cbe2 commit febcc31
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
54 changes: 32 additions & 22 deletions dev-tools/cmd/dashboards/export_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
Expand Down Expand Up @@ -54,7 +54,7 @@ func main() {

u, err := url.Parse(*kibanaURL)
if err != nil {
log.Fatalf("Error parsing Kibana URL: %v", err)
log.Fatalf("Error parsing Kibana URL: %+v", err)
}

client, err := kibana.NewClientWithConfig(&kibana.ClientConfig{
Expand All @@ -73,36 +73,46 @@ func main() {
}

if len(*ymlFile) > 0 {
results, info, err := dashboards.ExportAllFromYml(client, *ymlFile)
for i, r := range results {
log.Printf("id=%s, name=%s\n", info.Dashboards[i].ID, info.Dashboards[i].File)
r = dashboards.DecodeExported(r)
err = dashboards.SaveToFile(r, info.Dashboards[i].File, filepath.Dir(*ymlFile), client.GetVersion())
if err != nil {
log.Fatalf("failed to export the dashboards: %s", err)
}
err = exportDashboardsFromYML(client, *ymlFile)
if err != nil {
log.Fatalf("Failed to export dashboards from YML file: %+v", err)
}
os.Exit(0)
}

if len(*dashboard) > 0 {
result, err := dashboards.Export(client, *dashboard)
err = exportSingleDashboard(client, *dashboard, *fileOutput)
if err != nil {
log.Fatalf("Failed to export the dashboard: %s", err)
log.Fatalf("Failed to export the dashboard: %+v", err)
}
result = dashboards.DecodeExported(result)
bytes, err := json.Marshal(result)
if err != nil {
log.Fatalf("Failed to save the dashboard: %s", err)
if !quiet {
log.Printf("The dashboard %s was exported under '%s'\n", *dashboard, *fileOutput)
}
}
}

err = ioutil.WriteFile(*fileOutput, bytes, 0644)
func exportDashboardsFromYML(client *kibana.Client, ymlFile string) error {
results, info, err := dashboards.ExportAllFromYml(client, ymlFile)
for i, r := range results {
log.Printf("id=%s, name=%s\n", info.Dashboards[i].ID, info.Dashboards[i].File)
r = dashboards.DecodeExported(r)
err = dashboards.SaveToFile(r, info.Dashboards[i].File, filepath.Dir(ymlFile), client.GetVersion())
if err != nil {
log.Fatalf("Failed to save the dashboard: %s", err)

}
if !quiet {
log.Printf("The dashboard %s was exported under the %s file\n", *dashboard, *fileOutput)
return err
}
}
return nil
}

func exportSingleDashboard(client *kibana.Client, dashboard, output string) error {
result, err := dashboards.Export(client, dashboard)
if err != nil {
return fmt.Errorf("failed to export the dashboard: %+v", err)
}
result = dashboards.DecodeExported(result)
err = ioutil.WriteFile(output, []byte(result.StringToPrint()), dashboards.OutputPermission)
if err != nil {
return fmt.Errorf("failed to save the dashboards: %+v", err)
}
return nil
}
10 changes: 3 additions & 7 deletions libbeat/dashboards/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package dashboards

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
Expand All @@ -32,7 +31,8 @@ import (
)

const (
dashboardPerm = 0644
// OutputPermission is the permission of dashboard output files.
OutputPermission = 0644
)

// ListYML is the yaml file which contains list of available dashboards.
Expand Down Expand Up @@ -92,10 +92,6 @@ func SaveToFile(dashboard common.MapStr, filename, root string, version common.V
}

out := filepath.Join(root, dashboardsPath, filename)
bytes, err := json.Marshal(dashboard)
if err != nil {
return err
}

return ioutil.WriteFile(out, bytes, dashboardPerm)
return ioutil.WriteFile(out, []byte(dashboard.StringToPrint()), OutputPermission)
}

0 comments on commit febcc31

Please sign in to comment.