wsh editconfig (#1243)

This commit is contained in:
Mike Sawka 2024-11-08 11:08:52 -08:00 committed by GitHub
parent cd0e2949ee
commit d5297cc5d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 84 additions and 2 deletions

View File

@ -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
}
}

View File

@ -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)