|
| 1 | +package visualization |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/duneanalytics/cli/cmdutil" |
| 9 | + "github.com/duneanalytics/cli/output" |
| 10 | + "github.com/duneanalytics/duneapi-client-go/models" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func newUpdateCmd() *cobra.Command { |
| 15 | + cmd := &cobra.Command{ |
| 16 | + Use: "update <visualization_id>", |
| 17 | + Short: "Update an existing visualization", |
| 18 | + Long: "Replace a visualization's properties. Fetches the current visualization first,\n" + |
| 19 | + "applies your changes, and sends the full updated object.\n\n" + |
| 20 | + "Only supply the flags you want to change; unchanged fields are preserved.\n" + |
| 21 | + "At least one flag must be provided.\n\n" + |
| 22 | + "Examples:\n" + |
| 23 | + " dune viz update 12345 --name \"New Name\"\n" + |
| 24 | + " dune viz update 12345 --type counter --options '{\"counterColName\":\"count\"}'", |
| 25 | + Args: cobra.ExactArgs(1), |
| 26 | + RunE: runUpdate, |
| 27 | + } |
| 28 | + |
| 29 | + cmd.Flags().String("name", "", "new name for the visualization") |
| 30 | + cmd.Flags().String("type", "", "new visualization type") |
| 31 | + cmd.Flags().String("description", "", "new description for the visualization") |
| 32 | + cmd.Flags().String("options", "", "new visualization options JSON") |
| 33 | + output.AddFormatFlag(cmd, "text") |
| 34 | + |
| 35 | + return cmd |
| 36 | +} |
| 37 | + |
| 38 | +func runUpdate(cmd *cobra.Command, args []string) error { |
| 39 | + vizID, err := strconv.Atoi(args[0]) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("invalid visualization ID %q: must be an integer", args[0]) |
| 42 | + } |
| 43 | + |
| 44 | + if !cmd.Flags().Changed("name") && !cmd.Flags().Changed("type") && |
| 45 | + !cmd.Flags().Changed("description") && !cmd.Flags().Changed("options") { |
| 46 | + return fmt.Errorf("at least one flag must be provided (--name, --type, --description, or --options)") |
| 47 | + } |
| 48 | + |
| 49 | + client := cmdutil.ClientFromCmd(cmd) |
| 50 | + |
| 51 | + // Fetch current visualization to preserve unchanged fields |
| 52 | + current, err := client.GetVisualization(vizID) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("failed to fetch current visualization: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + // Start with current values, override with changed flags |
| 58 | + req := models.UpdateVisualizationRequest{ |
| 59 | + Name: current.Name, |
| 60 | + Type: current.Type, |
| 61 | + Description: current.Description, |
| 62 | + Options: current.Options, |
| 63 | + } |
| 64 | + |
| 65 | + if cmd.Flags().Changed("name") { |
| 66 | + req.Name, _ = cmd.Flags().GetString("name") |
| 67 | + } |
| 68 | + if cmd.Flags().Changed("type") { |
| 69 | + req.Type, _ = cmd.Flags().GetString("type") |
| 70 | + } |
| 71 | + if cmd.Flags().Changed("description") { |
| 72 | + req.Description, _ = cmd.Flags().GetString("description") |
| 73 | + } |
| 74 | + if cmd.Flags().Changed("options") { |
| 75 | + optionsStr, _ := cmd.Flags().GetString("options") |
| 76 | + var options map[string]any |
| 77 | + if err := json.Unmarshal([]byte(optionsStr), &options); err != nil { |
| 78 | + return fmt.Errorf("invalid --options JSON: %w", err) |
| 79 | + } |
| 80 | + req.Options = options |
| 81 | + } |
| 82 | + |
| 83 | + resp, err := client.UpdateVisualization(vizID, req) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + w := cmd.OutOrStdout() |
| 89 | + switch output.FormatFromCmd(cmd) { |
| 90 | + case output.FormatJSON: |
| 91 | + return output.PrintJSON(w, resp) |
| 92 | + default: |
| 93 | + fmt.Fprintf(w, "Updated visualization %d\n", resp.ID) |
| 94 | + return nil |
| 95 | + } |
| 96 | +} |
0 commit comments