From d5297cc5d4194125ab0183bcc96829a7a3743e05 Mon Sep 17 00:00:00 2001 From: Mike Sawka Date: Fri, 8 Nov 2024 11:08:52 -0800 Subject: [PATCH] wsh editconfig (#1243) --- cmd/wsh/cmd/wshcmd-editconfig.go | 58 ++++++++++++++++++++++++++++++++ cmd/wsh/cmd/wshcmd-version.go | 28 +++++++++++++-- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 cmd/wsh/cmd/wshcmd-editconfig.go diff --git a/cmd/wsh/cmd/wshcmd-editconfig.go b/cmd/wsh/cmd/wshcmd-editconfig.go new file mode 100644 index 000000000..804a5f753 --- /dev/null +++ b/cmd/wsh/cmd/wshcmd-editconfig.go @@ -0,0 +1,58 @@ +// Copyright 2024, Command Line Inc. +// SPDX-License-Identifier: Apache-2.0 + +package cmd + +import ( + "path/filepath" + + "github.com/spf13/cobra" + "github.com/wavetermdev/waveterm/pkg/waveobj" + "github.com/wavetermdev/waveterm/pkg/wshrpc" + "github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient" +) + +var editConfigCmd = &cobra.Command{ + Use: "editconfig [configfile]", + Short: "edit Wave configuration files", + Long: "Edit Wave configuration files. Defaults to settings.json if no file specified. Common files: settings.json, presets.json, widgets.json", + Args: cobra.MaximumNArgs(1), + Run: editConfigRun, + PreRunE: preRunSetupRpcClient, +} + +func init() { + rootCmd.AddCommand(editConfigCmd) +} + +func editConfigRun(cmd *cobra.Command, args []string) { + // Get config directory from Wave info + resp, err := wshclient.WaveInfoCommand(RpcClient, &wshrpc.RpcOpts{Timeout: 2000}) + if err != nil { + WriteStderr("[error] getting Wave info: %v\n", err) + return + } + + configFile := "settings.json" // default + if len(args) > 0 { + configFile = args[0] + } + + settingsFile := filepath.Join(resp.ConfigDir, configFile) + + wshCmd := &wshrpc.CommandCreateBlockData{ + BlockDef: &waveobj.BlockDef{ + Meta: map[string]interface{}{ + waveobj.MetaKey_View: "preview", + waveobj.MetaKey_File: settingsFile, + waveobj.MetaKey_Edit: true, + }, + }, + } + + _, err = RpcClient.SendRpcRequest(wshrpc.Command_CreateBlock, wshCmd, &wshrpc.RpcOpts{Timeout: 2000}) + if err != nil { + WriteStderr("[error] opening config file: %v\n", err) + return + } +} diff --git a/cmd/wsh/cmd/wshcmd-version.go b/cmd/wsh/cmd/wshcmd-version.go index 480e9ea35..cf4debf1a 100644 --- a/cmd/wsh/cmd/wshcmd-version.go +++ b/cmd/wsh/cmd/wshcmd-version.go @@ -4,6 +4,7 @@ package cmd import ( + "encoding/json" "fmt" "github.com/spf13/cobra" @@ -14,36 +15,59 @@ import ( ) var versionVerbose bool +var versionJSON bool // versionCmd represents the version command var versionCmd = &cobra.Command{ - Use: "version [-v]", + Use: "version [-v] [--json]", Short: "Print the version number of wsh", RunE: runVersionCmd, } func init() { versionCmd.Flags().BoolVarP(&versionVerbose, "verbose", "v", false, "Display full version information") + versionCmd.Flags().BoolVar(&versionJSON, "json", false, "Output version information in JSON format") rootCmd.AddCommand(versionCmd) } func runVersionCmd(cmd *cobra.Command, args []string) error { - if !versionVerbose { + if !versionVerbose && !versionJSON { WriteStdout("wsh v%s\n", wavebase.WaveVersion) return nil } + err := preRunSetupRpcClient(cmd, args) if err != nil { return err } + resp, err := wshclient.WaveInfoCommand(RpcClient, &wshrpc.RpcOpts{Timeout: 2000}) if err != nil { return err } + updateChannel, err := wshclient.GetUpdateChannelCommand(RpcClient, &wshrpc.RpcOpts{Timeout: 2000, Route: wshutil.ElectronRoute}) if err != nil { return err } + + if versionJSON { + info := map[string]interface{}{ + "version": resp.Version, + "buildtime": resp.BuildTime, + "configdir": resp.ConfigDir, + "datadir": resp.DataDir, + "updatechannel": updateChannel, + } + outBArr, err := json.MarshalIndent(info, "", " ") + if err != nil { + return fmt.Errorf("formatting version info: %v", err) + } + WriteStdout("%s\n", string(outBArr)) + return nil + } + + // Default verbose text output fmt.Printf("v%s (%s)\n", resp.Version, resp.BuildTime) fmt.Printf("configdir: %s\n", resp.ConfigDir) fmt.Printf("datadir: %s\n", resp.DataDir)