mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38:23 +01:00
implement wsh setconfig (#284)
This commit is contained in:
parent
8630e23239
commit
a3aa941b68
37
cmd/wsh/cmd/wshcmd-setconfig.go
Normal file
37
cmd/wsh/cmd/wshcmd-setconfig.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Copyright 2024, Command Line Inc.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wshrpc/wshclient"
|
||||
)
|
||||
|
||||
var setConfigCmd = &cobra.Command{
|
||||
Use: "setconfig",
|
||||
Short: "set config",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: setConfigRun,
|
||||
PreRunE: preRunSetupRpcClient,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(setConfigCmd)
|
||||
}
|
||||
|
||||
func setConfigRun(cmd *cobra.Command, args []string) {
|
||||
metaSetsStrs := args[:]
|
||||
meta, err := parseMetaSets(metaSetsStrs)
|
||||
if err != nil {
|
||||
WriteStderr("[error] %v\n", err)
|
||||
return
|
||||
}
|
||||
err = wshclient.SetConfigCommand(RpcClient, meta, &wshrpc.RpcOpts{Timeout: 2000})
|
||||
if err != nil {
|
||||
WriteStderr("[error] setting config: %v\n", err)
|
||||
return
|
||||
}
|
||||
WriteStdout("config set\n")
|
||||
}
|
@ -122,6 +122,11 @@ class WshServerType {
|
||||
return WOS.wshServerRpcHelper_call("resolveids", data, opts);
|
||||
}
|
||||
|
||||
// command "setconfig" [call]
|
||||
SetConfigCommand(data: MetaMapType, opts?: RpcOpts): Promise<void> {
|
||||
return WOS.wshServerRpcHelper_call("setconfig", data, opts);
|
||||
}
|
||||
|
||||
// command "setmeta" [call]
|
||||
SetMetaCommand(data: CommandSetMetaData, opts?: RpcOpts): Promise<void> {
|
||||
return WOS.wshServerRpcHelper_call("setmeta", data, opts);
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
|
||||
)
|
||||
|
||||
@ -42,10 +43,7 @@ func GenerateMetaMapConsts(buf *strings.Builder, constPrefix string, rtype refle
|
||||
continue
|
||||
}
|
||||
fieldName := field.Name
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if commaIdx := strings.Index(jsonTag, ","); commaIdx != -1 {
|
||||
jsonTag = jsonTag[:commaIdx]
|
||||
}
|
||||
jsonTag := utilfn.GetJsonTag(field)
|
||||
if jsonTag == "" {
|
||||
jsonTag = fieldName
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ import (
|
||||
"github.com/wavetermdev/thenextwave/pkg/service"
|
||||
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
|
||||
"github.com/wavetermdev/thenextwave/pkg/userinput"
|
||||
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||
"github.com/wavetermdev/thenextwave/pkg/vdom"
|
||||
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wconfig"
|
||||
@ -84,20 +85,15 @@ func getTSFieldName(field reflect.StructField) string {
|
||||
}
|
||||
return tsFieldTag
|
||||
}
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if jsonTag != "" {
|
||||
parts := strings.Split(jsonTag, ",")
|
||||
namePart := parts[0]
|
||||
if namePart != "" {
|
||||
if namePart == "-" {
|
||||
jsonTag := utilfn.GetJsonTag(field)
|
||||
if jsonTag == "-" {
|
||||
return ""
|
||||
}
|
||||
if strings.Contains(namePart, ":") {
|
||||
return "\"" + namePart + "\""
|
||||
if strings.Contains(jsonTag, ":") {
|
||||
return "\"" + jsonTag + "\""
|
||||
}
|
||||
return namePart
|
||||
}
|
||||
// if namePart is empty, still uses default
|
||||
if jsonTag != "" {
|
||||
return jsonTag
|
||||
}
|
||||
return field.Name
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
@ -899,3 +900,15 @@ func RandomHexString(numHexDigits int) (string, error) {
|
||||
hexStr := hex.EncodeToString(bytes)
|
||||
return hexStr[:numHexDigits], nil // Return the exact number of hex digits
|
||||
}
|
||||
|
||||
func GetJsonTag(field reflect.StructField) string {
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if jsonTag == "" {
|
||||
return ""
|
||||
}
|
||||
commaIdx := strings.Index(jsonTag, ",")
|
||||
if commaIdx != -1 {
|
||||
jsonTag = jsonTag[:commaIdx]
|
||||
}
|
||||
return jsonTag
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -127,7 +128,8 @@ func RegisterType(rtype reflect.Type) {
|
||||
if oidField.Type.Kind() != reflect.String {
|
||||
panic(fmt.Sprintf("OID field must be string for %v", rtype))
|
||||
}
|
||||
if oidField.Tag.Get("json") != OIDKeyName {
|
||||
oidJsonTag := utilfn.GetJsonTag(oidField)
|
||||
if oidJsonTag != OIDKeyName {
|
||||
panic(fmt.Sprintf("OID field json tag must be %q for %v", OIDKeyName, rtype))
|
||||
}
|
||||
versionField, found := rtype.Elem().FieldByName(VersionGoFieldName)
|
||||
@ -137,7 +139,8 @@ func RegisterType(rtype reflect.Type) {
|
||||
if versionField.Type.Kind() != reflect.Int {
|
||||
panic(fmt.Sprintf("Version field must be int for %v", rtype))
|
||||
}
|
||||
if versionField.Tag.Get("json") != VersionKeyName {
|
||||
versionJsonTag := utilfn.GetJsonTag(versionField)
|
||||
if versionJsonTag != VersionKeyName {
|
||||
panic(fmt.Sprintf("Version field json tag must be %q for %v", VersionKeyName, rtype))
|
||||
}
|
||||
metaField, found := rtype.Elem().FieldByName(MetaGoFieldName)
|
||||
|
@ -157,12 +157,12 @@ func ReadFullConfig() FullConfigType {
|
||||
if configFile == "-" {
|
||||
continue
|
||||
}
|
||||
jsonTag := field.Tag.Get("json")
|
||||
jsonTag := utilfn.GetJsonTag(field)
|
||||
if jsonTag == "-" || jsonTag == "" {
|
||||
continue
|
||||
}
|
||||
simpleMerge := field.Tag.Get("merge") == ""
|
||||
fileName := field.Tag.Get("json") + ".json"
|
||||
fileName := jsonTag + ".json"
|
||||
configPart, cerrs := ReadConfigPart(fileName, simpleMerge)
|
||||
fullConfig.ConfigErrors = append(fullConfig.ConfigErrors, cerrs...)
|
||||
if configPart != nil {
|
||||
@ -177,7 +177,8 @@ func getConfigKeyType(configKey string) reflect.Type {
|
||||
ctype := reflect.TypeOf(SettingsType{})
|
||||
for i := 0; i < ctype.NumField(); i++ {
|
||||
field := ctype.Field(i)
|
||||
if field.Tag.Get("json") == configKey {
|
||||
jsonTag := utilfn.GetJsonTag(field)
|
||||
if jsonTag == configKey {
|
||||
return field.Type
|
||||
}
|
||||
}
|
||||
@ -261,11 +262,7 @@ func jsonMarshalConfigInOrder(m waveobj.MetaMapType) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func SetBaseConfigValue(configKey string, val any) error {
|
||||
ctype := getConfigKeyType(configKey)
|
||||
if ctype == nil {
|
||||
return fmt.Errorf("invalid config key: %s", configKey)
|
||||
}
|
||||
func SetBaseConfigValue(toMerge waveobj.MetaMapType) error {
|
||||
m, cerrs := ReadWaveHomeConfigFile(SettingsFile)
|
||||
if len(cerrs) > 0 {
|
||||
return fmt.Errorf("error reading config file: %v", cerrs[0])
|
||||
@ -273,6 +270,11 @@ func SetBaseConfigValue(configKey string, val any) error {
|
||||
if m == nil {
|
||||
m = make(waveobj.MetaMapType)
|
||||
}
|
||||
for configKey, val := range toMerge {
|
||||
ctype := getConfigKeyType(configKey)
|
||||
if ctype == nil {
|
||||
return fmt.Errorf("invalid config key: %s", configKey)
|
||||
}
|
||||
if val == nil {
|
||||
delete(m, configKey)
|
||||
} else {
|
||||
@ -281,6 +283,7 @@ func SetBaseConfigValue(configKey string, val any) error {
|
||||
}
|
||||
m[configKey] = val
|
||||
}
|
||||
}
|
||||
return WriteWaveHomeConfigFile(SettingsFile, m)
|
||||
}
|
||||
|
||||
|
@ -147,6 +147,12 @@ func ResolveIdsCommand(w *wshutil.WshRpc, data wshrpc.CommandResolveIdsData, opt
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// command "setconfig", wshserver.SetConfigCommand
|
||||
func SetConfigCommand(w *wshutil.WshRpc, data waveobj.MetaMapType, opts *wshrpc.RpcOpts) error {
|
||||
_, err := sendRpcRequestCallHelper[any](w, "setconfig", data, opts)
|
||||
return err
|
||||
}
|
||||
|
||||
// command "setmeta", wshserver.SetMetaCommand
|
||||
func SetMetaCommand(w *wshutil.WshRpc, data wshrpc.CommandSetMetaData, opts *wshrpc.RpcOpts) error {
|
||||
_, err := sendRpcRequestCallHelper[any](w, "setmeta", data, opts)
|
||||
|
@ -90,6 +90,7 @@ type WshRpcInterface interface {
|
||||
StreamWaveAiCommand(ctx context.Context, request OpenAiStreamRequest) chan RespOrErrorUnion[OpenAIPacketType]
|
||||
StreamCpuDataCommand(ctx context.Context, request CpuDataRequest) chan RespOrErrorUnion[TimeSeriesData]
|
||||
TestCommand(ctx context.Context, data string) error
|
||||
SetConfigCommand(ctx context.Context, data waveobj.MetaMapType) error
|
||||
|
||||
// eventrecv is special, it's handled internally by WshRpc with EventListener
|
||||
EventRecvCommand(ctx context.Context, data WaveEvent) error
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
"github.com/wavetermdev/thenextwave/pkg/filestore"
|
||||
"github.com/wavetermdev/thenextwave/pkg/waveai"
|
||||
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wconfig"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wcore"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wlayout"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wps"
|
||||
@ -477,3 +478,7 @@ func (ws *WshServer) EventUnsubAllCommand(ctx context.Context) error {
|
||||
wps.Broker.UnsubscribeAll(rpcSource)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WshServer) SetConfigCommand(ctx context.Context, data waveobj.MetaMapType) error {
|
||||
return wconfig.SetBaseConfigValue(data)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user