-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdashboard_describe.go
More file actions
83 lines (71 loc) · 2.41 KB
/
dashboard_describe.go
File metadata and controls
83 lines (71 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package dashboard
import (
"fmt"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
stscobra "github.com/stackvista/stackstate-cli/internal/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stackvista/stackstate-cli/internal/util"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
type DescribeArgs struct {
ID int64
Identifier string
FilePath string
}
func DashboardDescribeCommand(cli *di.Deps) *cobra.Command {
args := &DescribeArgs{}
cmd := &cobra.Command{
Use: "describe",
Short: "Describe a dashboard in YAML format",
Long: "Describe a dashboard in StackState Templated YAML.",
RunE: cli.CmdRunEWithApi(RunDashboardDescribeCommand(args)),
}
cmd.Flags().Int64Var(&args.ID, "id", 0, "ID of the dashboard")
cmd.Flags().StringVar(&args.Identifier, "identifier", "", "Identifier (URN) of the dashboard")
common.AddFileFlagVar(cmd, &args.FilePath, "Path to the output file")
stscobra.MarkMutexFlags(cmd, []string{common.IDFlag, common.IdentifierFlag}, "identifier", true)
return cmd
}
func RunDashboardDescribeCommand(args *DescribeArgs) di.CmdWithApiFn {
return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError {
dashboardIdOrUrn, err := ResolveDashboardIdOrUrn(args.ID, args.Identifier)
if err != nil {
return common.NewCLIArgParseError(err)
}
// Get the full dashboard data
dashboard, resp, err := api.DashboardsApi.GetDashboard(cli.Context, dashboardIdOrUrn).LoadFullDashboard(true).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
yamlData, err := yaml.Marshal(dashboard)
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to marshal dashboard: %v", err))
}
data := string(yamlData)
if args.FilePath != "" {
if err := util.WriteFile(args.FilePath, []byte(data)); err != nil {
return common.NewWriteFileError(err, args.FilePath)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"filepath": args.FilePath,
})
} else {
cli.Printer.Success(fmt.Sprintf("dashboard exported to: %s", args.FilePath))
}
return nil
} else {
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"data": data,
"format": "json",
})
} else {
cli.Printer.PrintLn(data)
}
return nil
}
}
}