Skip to content

Commit d5297cc

Browse files
authored
wsh editconfig (#1243)
1 parent cd0e294 commit d5297cc

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

cmd/wsh/cmd/wshcmd-editconfig.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2024, Command Line Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmd
5+
6+
import (
7+
"path/filepath"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/wavetermdev/waveterm/pkg/waveobj"
11+
"github.com/wavetermdev/waveterm/pkg/wshrpc"
12+
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient"
13+
)
14+
15+
var editConfigCmd = &cobra.Command{
16+
Use: "editconfig [configfile]",
17+
Short: "edit Wave configuration files",
18+
Long: "Edit Wave configuration files. Defaults to settings.json if no file specified. Common files: settings.json, presets.json, widgets.json",
19+
Args: cobra.MaximumNArgs(1),
20+
Run: editConfigRun,
21+
PreRunE: preRunSetupRpcClient,
22+
}
23+
24+
func init() {
25+
rootCmd.AddCommand(editConfigCmd)
26+
}
27+
28+
func editConfigRun(cmd *cobra.Command, args []string) {
29+
// Get config directory from Wave info
30+
resp, err := wshclient.WaveInfoCommand(RpcClient, &wshrpc.RpcOpts{Timeout: 2000})
31+
if err != nil {
32+
WriteStderr("[error] getting Wave info: %v\n", err)
33+
return
34+
}
35+
36+
configFile := "settings.json" // default
37+
if len(args) > 0 {
38+
configFile = args[0]
39+
}
40+
41+
settingsFile := filepath.Join(resp.ConfigDir, configFile)
42+
43+
wshCmd := &wshrpc.CommandCreateBlockData{
44+
BlockDef: &waveobj.BlockDef{
45+
Meta: map[string]interface{}{
46+
waveobj.MetaKey_View: "preview",
47+
waveobj.MetaKey_File: settingsFile,
48+
waveobj.MetaKey_Edit: true,
49+
},
50+
},
51+
}
52+
53+
_, err = RpcClient.SendRpcRequest(wshrpc.Command_CreateBlock, wshCmd, &wshrpc.RpcOpts{Timeout: 2000})
54+
if err != nil {
55+
WriteStderr("[error] opening config file: %v\n", err)
56+
return
57+
}
58+
}

cmd/wsh/cmd/wshcmd-version.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"encoding/json"
78
"fmt"
89

910
"github.com/spf13/cobra"
@@ -14,36 +15,59 @@ import (
1415
)
1516

1617
var versionVerbose bool
18+
var versionJSON bool
1719

1820
// versionCmd represents the version command
1921
var versionCmd = &cobra.Command{
20-
Use: "version [-v]",
22+
Use: "version [-v] [--json]",
2123
Short: "Print the version number of wsh",
2224
RunE: runVersionCmd,
2325
}
2426

2527
func init() {
2628
versionCmd.Flags().BoolVarP(&versionVerbose, "verbose", "v", false, "Display full version information")
29+
versionCmd.Flags().BoolVar(&versionJSON, "json", false, "Output version information in JSON format")
2730
rootCmd.AddCommand(versionCmd)
2831
}
2932

3033
func runVersionCmd(cmd *cobra.Command, args []string) error {
31-
if !versionVerbose {
34+
if !versionVerbose && !versionJSON {
3235
WriteStdout("wsh v%s\n", wavebase.WaveVersion)
3336
return nil
3437
}
38+
3539
err := preRunSetupRpcClient(cmd, args)
3640
if err != nil {
3741
return err
3842
}
43+
3944
resp, err := wshclient.WaveInfoCommand(RpcClient, &wshrpc.RpcOpts{Timeout: 2000})
4045
if err != nil {
4146
return err
4247
}
48+
4349
updateChannel, err := wshclient.GetUpdateChannelCommand(RpcClient, &wshrpc.RpcOpts{Timeout: 2000, Route: wshutil.ElectronRoute})
4450
if err != nil {
4551
return err
4652
}
53+
54+
if versionJSON {
55+
info := map[string]interface{}{
56+
"version": resp.Version,
57+
"buildtime": resp.BuildTime,
58+
"configdir": resp.ConfigDir,
59+
"datadir": resp.DataDir,
60+
"updatechannel": updateChannel,
61+
}
62+
outBArr, err := json.MarshalIndent(info, "", " ")
63+
if err != nil {
64+
return fmt.Errorf("formatting version info: %v", err)
65+
}
66+
WriteStdout("%s\n", string(outBArr))
67+
return nil
68+
}
69+
70+
// Default verbose text output
4771
fmt.Printf("v%s (%s)\n", resp.Version, resp.BuildTime)
4872
fmt.Printf("configdir: %s\n", resp.ConfigDir)
4973
fmt.Printf("datadir: %s\n", resp.DataDir)

0 commit comments

Comments
 (0)