mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-03-11 13:23:06 +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);
|
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]
|
// command "setmeta" [call]
|
||||||
SetMetaCommand(data: CommandSetMetaData, opts?: RpcOpts): Promise<void> {
|
SetMetaCommand(data: CommandSetMetaData, opts?: RpcOpts): Promise<void> {
|
||||||
return WOS.wshServerRpcHelper_call("setmeta", data, opts);
|
return WOS.wshServerRpcHelper_call("setmeta", data, opts);
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
|
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,10 +43,7 @@ func GenerateMetaMapConsts(buf *strings.Builder, constPrefix string, rtype refle
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fieldName := field.Name
|
fieldName := field.Name
|
||||||
jsonTag := field.Tag.Get("json")
|
jsonTag := utilfn.GetJsonTag(field)
|
||||||
if commaIdx := strings.Index(jsonTag, ","); commaIdx != -1 {
|
|
||||||
jsonTag = jsonTag[:commaIdx]
|
|
||||||
}
|
|
||||||
if jsonTag == "" {
|
if jsonTag == "" {
|
||||||
jsonTag = fieldName
|
jsonTag = fieldName
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/wavetermdev/thenextwave/pkg/service"
|
"github.com/wavetermdev/thenextwave/pkg/service"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
|
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/userinput"
|
"github.com/wavetermdev/thenextwave/pkg/userinput"
|
||||||
|
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/vdom"
|
"github.com/wavetermdev/thenextwave/pkg/vdom"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/wconfig"
|
"github.com/wavetermdev/thenextwave/pkg/wconfig"
|
||||||
@ -84,20 +85,15 @@ func getTSFieldName(field reflect.StructField) string {
|
|||||||
}
|
}
|
||||||
return tsFieldTag
|
return tsFieldTag
|
||||||
}
|
}
|
||||||
jsonTag := field.Tag.Get("json")
|
jsonTag := utilfn.GetJsonTag(field)
|
||||||
if jsonTag != "" {
|
if jsonTag == "-" {
|
||||||
parts := strings.Split(jsonTag, ",")
|
|
||||||
namePart := parts[0]
|
|
||||||
if namePart != "" {
|
|
||||||
if namePart == "-" {
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if strings.Contains(namePart, ":") {
|
if strings.Contains(jsonTag, ":") {
|
||||||
return "\"" + namePart + "\""
|
return "\"" + jsonTag + "\""
|
||||||
}
|
}
|
||||||
return namePart
|
if jsonTag != "" {
|
||||||
}
|
return jsonTag
|
||||||
// if namePart is empty, still uses default
|
|
||||||
}
|
}
|
||||||
return field.Name
|
return field.Name
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -899,3 +900,15 @@ func RandomHexString(numHexDigits int) (string, error) {
|
|||||||
hexStr := hex.EncodeToString(bytes)
|
hexStr := hex.EncodeToString(bytes)
|
||||||
return hexStr[:numHexDigits], nil // Return the exact number of hex digits
|
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/google/uuid"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
|
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -127,7 +128,8 @@ func RegisterType(rtype reflect.Type) {
|
|||||||
if oidField.Type.Kind() != reflect.String {
|
if oidField.Type.Kind() != reflect.String {
|
||||||
panic(fmt.Sprintf("OID field must be string for %v", rtype))
|
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))
|
panic(fmt.Sprintf("OID field json tag must be %q for %v", OIDKeyName, rtype))
|
||||||
}
|
}
|
||||||
versionField, found := rtype.Elem().FieldByName(VersionGoFieldName)
|
versionField, found := rtype.Elem().FieldByName(VersionGoFieldName)
|
||||||
@ -137,7 +139,8 @@ func RegisterType(rtype reflect.Type) {
|
|||||||
if versionField.Type.Kind() != reflect.Int {
|
if versionField.Type.Kind() != reflect.Int {
|
||||||
panic(fmt.Sprintf("Version field must be int for %v", rtype))
|
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))
|
panic(fmt.Sprintf("Version field json tag must be %q for %v", VersionKeyName, rtype))
|
||||||
}
|
}
|
||||||
metaField, found := rtype.Elem().FieldByName(MetaGoFieldName)
|
metaField, found := rtype.Elem().FieldByName(MetaGoFieldName)
|
||||||
|
@ -157,12 +157,12 @@ func ReadFullConfig() FullConfigType {
|
|||||||
if configFile == "-" {
|
if configFile == "-" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
jsonTag := field.Tag.Get("json")
|
jsonTag := utilfn.GetJsonTag(field)
|
||||||
if jsonTag == "-" || jsonTag == "" {
|
if jsonTag == "-" || jsonTag == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
simpleMerge := field.Tag.Get("merge") == ""
|
simpleMerge := field.Tag.Get("merge") == ""
|
||||||
fileName := field.Tag.Get("json") + ".json"
|
fileName := jsonTag + ".json"
|
||||||
configPart, cerrs := ReadConfigPart(fileName, simpleMerge)
|
configPart, cerrs := ReadConfigPart(fileName, simpleMerge)
|
||||||
fullConfig.ConfigErrors = append(fullConfig.ConfigErrors, cerrs...)
|
fullConfig.ConfigErrors = append(fullConfig.ConfigErrors, cerrs...)
|
||||||
if configPart != nil {
|
if configPart != nil {
|
||||||
@ -177,7 +177,8 @@ func getConfigKeyType(configKey string) reflect.Type {
|
|||||||
ctype := reflect.TypeOf(SettingsType{})
|
ctype := reflect.TypeOf(SettingsType{})
|
||||||
for i := 0; i < ctype.NumField(); i++ {
|
for i := 0; i < ctype.NumField(); i++ {
|
||||||
field := ctype.Field(i)
|
field := ctype.Field(i)
|
||||||
if field.Tag.Get("json") == configKey {
|
jsonTag := utilfn.GetJsonTag(field)
|
||||||
|
if jsonTag == configKey {
|
||||||
return field.Type
|
return field.Type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -261,11 +262,7 @@ func jsonMarshalConfigInOrder(m waveobj.MetaMapType) ([]byte, error) {
|
|||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetBaseConfigValue(configKey string, val any) error {
|
func SetBaseConfigValue(toMerge waveobj.MetaMapType) error {
|
||||||
ctype := getConfigKeyType(configKey)
|
|
||||||
if ctype == nil {
|
|
||||||
return fmt.Errorf("invalid config key: %s", configKey)
|
|
||||||
}
|
|
||||||
m, cerrs := ReadWaveHomeConfigFile(SettingsFile)
|
m, cerrs := ReadWaveHomeConfigFile(SettingsFile)
|
||||||
if len(cerrs) > 0 {
|
if len(cerrs) > 0 {
|
||||||
return fmt.Errorf("error reading config file: %v", 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 {
|
if m == nil {
|
||||||
m = make(waveobj.MetaMapType)
|
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 {
|
if val == nil {
|
||||||
delete(m, configKey)
|
delete(m, configKey)
|
||||||
} else {
|
} else {
|
||||||
@ -281,6 +283,7 @@ func SetBaseConfigValue(configKey string, val any) error {
|
|||||||
}
|
}
|
||||||
m[configKey] = val
|
m[configKey] = val
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return WriteWaveHomeConfigFile(SettingsFile, m)
|
return WriteWaveHomeConfigFile(SettingsFile, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,6 +147,12 @@ func ResolveIdsCommand(w *wshutil.WshRpc, data wshrpc.CommandResolveIdsData, opt
|
|||||||
return resp, err
|
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
|
// command "setmeta", wshserver.SetMetaCommand
|
||||||
func SetMetaCommand(w *wshutil.WshRpc, data wshrpc.CommandSetMetaData, opts *wshrpc.RpcOpts) error {
|
func SetMetaCommand(w *wshutil.WshRpc, data wshrpc.CommandSetMetaData, opts *wshrpc.RpcOpts) error {
|
||||||
_, err := sendRpcRequestCallHelper[any](w, "setmeta", data, opts)
|
_, err := sendRpcRequestCallHelper[any](w, "setmeta", data, opts)
|
||||||
|
@ -90,6 +90,7 @@ type WshRpcInterface interface {
|
|||||||
StreamWaveAiCommand(ctx context.Context, request OpenAiStreamRequest) chan RespOrErrorUnion[OpenAIPacketType]
|
StreamWaveAiCommand(ctx context.Context, request OpenAiStreamRequest) chan RespOrErrorUnion[OpenAIPacketType]
|
||||||
StreamCpuDataCommand(ctx context.Context, request CpuDataRequest) chan RespOrErrorUnion[TimeSeriesData]
|
StreamCpuDataCommand(ctx context.Context, request CpuDataRequest) chan RespOrErrorUnion[TimeSeriesData]
|
||||||
TestCommand(ctx context.Context, data string) error
|
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
|
// eventrecv is special, it's handled internally by WshRpc with EventListener
|
||||||
EventRecvCommand(ctx context.Context, data WaveEvent) error
|
EventRecvCommand(ctx context.Context, data WaveEvent) error
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/wavetermdev/thenextwave/pkg/filestore"
|
"github.com/wavetermdev/thenextwave/pkg/filestore"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/waveai"
|
"github.com/wavetermdev/thenextwave/pkg/waveai"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
||||||
|
"github.com/wavetermdev/thenextwave/pkg/wconfig"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/wcore"
|
"github.com/wavetermdev/thenextwave/pkg/wcore"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/wlayout"
|
"github.com/wavetermdev/thenextwave/pkg/wlayout"
|
||||||
"github.com/wavetermdev/thenextwave/pkg/wps"
|
"github.com/wavetermdev/thenextwave/pkg/wps"
|
||||||
@ -477,3 +478,7 @@ func (ws *WshServer) EventUnsubAllCommand(ctx context.Context) error {
|
|||||||
wps.Broker.UnsubscribeAll(rpcSource)
|
wps.Broker.UnsubscribeAll(rpcSource)
|
||||||
return nil
|
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