Skip to content

Commit 54c4280

Browse files
committed
Use yaml for dashboard interactions instead of json
More user friendly and matches the behavior we have for the `settings` command.
1 parent cf24b1e commit 54c4280

4 files changed

Lines changed: 21 additions & 18 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ release-notes.md
2323
release-notes.json
2424

2525
.localdev/
26+
stackstate-cli

cmd/dashboard/dashboard_apply.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
1212
"github.com/stackvista/stackstate-cli/internal/common"
1313
"github.com/stackvista/stackstate-cli/internal/di"
14+
"sigs.k8s.io/kustomize/kyaml/yaml"
1415
)
1516

1617
// ApplyArgs contains arguments for dashboard apply command
@@ -22,12 +23,12 @@ func DashboardApplyCommand(cli *di.Deps) *cobra.Command {
2223
args := &ApplyArgs{}
2324
cmd := &cobra.Command{
2425
Use: "apply",
25-
Short: "Create or edit a dashboard from JSON",
26-
Long: "Create or edit a dashboard from JSON file.",
26+
Short: "Create or edit a dashboard from YAML",
27+
Long: "Create or edit a dashboard from YAML file.",
2728
RunE: cli.CmdRunEWithApi(RunDashboardApplyCommand(args)),
2829
}
2930

30-
common.AddRequiredFileFlagVar(cmd, &args.File, "Path to a .json file with the dashboard definition")
31+
common.AddRequiredFileFlagVar(cmd, &args.File, "Path to a .yaml file with the dashboard definition")
3132

3233
return cmd
3334
}
@@ -41,8 +42,8 @@ func RunDashboardApplyCommand(args *ApplyArgs) di.CmdWithApiFn {
4142

4243
// Determine file type by extension
4344
ext := strings.ToLower(filepath.Ext(args.File))
44-
if ext != ".json" {
45-
return common.NewCLIArgParseError(fmt.Errorf("unsupported file type: %s. Only .json files are supported", ext))
45+
if ext != ".yaml" {
46+
return common.NewCLIArgParseError(fmt.Errorf("unsupported file type: %s. Only .yaml files are supported", ext))
4647
}
4748

4849
return applyJSONDashboard(cli, api, fileBytes)
@@ -53,7 +54,7 @@ func RunDashboardApplyCommand(args *ApplyArgs) di.CmdWithApiFn {
5354
func applyJSONDashboard(cli *di.Deps, api *stackstate_api.APIClient, fileBytes []byte) common.CLIError {
5455
// Parse the JSON to determine if it's a create or update operation
5556
var dashboardData map[string]interface{}
56-
if err := json.Unmarshal(fileBytes, &dashboardData); err != nil {
57+
if err := yaml.Unmarshal(fileBytes, &dashboardData); err != nil {
5758
return common.NewCLIArgParseError(fmt.Errorf("failed to parse JSON: %v", err))
5859
}
5960

cmd/dashboard/dashboard_describe.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dashboard
22

33
import (
4-
"encoding/json"
54
"fmt"
65

76
"github.com/spf13/cobra"
@@ -10,6 +9,7 @@ import (
109
"github.com/stackvista/stackstate-cli/internal/common"
1110
"github.com/stackvista/stackstate-cli/internal/di"
1211
"github.com/stackvista/stackstate-cli/internal/util"
12+
"sigs.k8s.io/kustomize/kyaml/yaml"
1313
)
1414

1515
type DescribeArgs struct {
@@ -22,7 +22,7 @@ func DashboardDescribeCommand(cli *di.Deps) *cobra.Command {
2222
args := &DescribeArgs{}
2323
cmd := &cobra.Command{
2424
Use: "describe",
25-
Short: "Describe a dashboard in STY format",
25+
Short: "Describe a dashboard in YAML format",
2626
Long: "Describe a dashboard in StackState Templated YAML.",
2727
RunE: cli.CmdRunEWithApi(RunDashboardDescribeCommand(args)),
2828
}
@@ -48,11 +48,11 @@ func RunDashboardDescribeCommand(args *DescribeArgs) di.CmdWithApiFn {
4848
return common.NewResponseError(err, resp)
4949
}
5050

51-
jsonData, err := json.MarshalIndent(dashboard, "", " ")
51+
yamlData, err := yaml.Marshal(dashboard)
5252
if err != nil {
5353
return common.NewExecutionError(fmt.Errorf("failed to marshal dashboard: %v", err))
5454
}
55-
data := string(jsonData)
55+
data := string(yamlData)
5656

5757
if args.FilePath != "" {
5858
if err := util.WriteFile(args.FilePath, []byte(data)); err != nil {

cmd/dashboard/dashboard_edit.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
stscobra "github.com/stackvista/stackstate-cli/internal/cobra"
1111
"github.com/stackvista/stackstate-cli/internal/common"
1212
"github.com/stackvista/stackstate-cli/internal/di"
13+
"sigs.k8s.io/kustomize/kyaml/yaml"
1314
)
1415

1516
const LongDescription = `Edit a dashboard.
@@ -60,19 +61,19 @@ func RunDashboardEditCommand(args *EditArgs) di.CmdWithApiFn {
6061
}
6162

6263
// Convert dashboard to pretty JSON for editing
63-
originalJSON, err := json.MarshalIndent(dashboard, "", " ")
64+
originalYAML, err := yaml.Marshal(dashboard)
6465
if err != nil {
65-
return common.NewExecutionError(fmt.Errorf("failed to marshal dashboard to JSON: %v", err))
66+
return common.NewExecutionError(fmt.Errorf("failed to marshal dashboard to YAML: %v", err))
6667
}
6768

6869
// Open editor with the dashboard JSON
69-
editedContent, err := cli.Editor.Edit("dashboard-", ".json", strings.NewReader(string(originalJSON)))
70+
editedContent, err := cli.Editor.Edit("dashboard-", ".yaml", strings.NewReader(string(originalYAML)))
7071
if err != nil {
7172
return common.NewExecutionError(fmt.Errorf("failed to open editor: %v", err))
7273
}
7374

7475
// Check if any changes were made
75-
if strings.Compare(string(originalJSON), string(editedContent)) == 0 {
76+
if strings.Compare(string(originalYAML), string(editedContent)) == 0 {
7677
if cli.IsJson() {
7778
cli.Printer.PrintJson(map[string]interface{}{"message": "No changes made"})
7879
} else {
@@ -81,13 +82,13 @@ func RunDashboardEditCommand(args *EditArgs) di.CmdWithApiFn {
8182
return nil
8283
}
8384

84-
// Parse the edited JSON
85+
// Parse the edited YAML
8586
var editedDashboard map[string]interface{}
86-
if err := json.Unmarshal(editedContent, &editedDashboard); err != nil {
87-
return common.NewExecutionError(fmt.Errorf("failed to parse edited JSON: %v", err))
87+
if err := yaml.Unmarshal(editedContent, &editedDashboard); err != nil {
88+
return common.NewExecutionError(fmt.Errorf("failed to parse edited YAML: %v", err))
8889
}
8990

90-
// Create patch schema from the edited JSON
91+
// Create patch schema from the edited YAML
9192
patchSchema := stackstate_api.NewDashboardPatchSchema()
9293

9394
if name, ok := editedDashboard["name"].(string); ok && name != "" {

0 commit comments

Comments
 (0)