From ac11a19604d4f91c06ebebce6d7141fd5470b991 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 18 Jan 2024 10:54:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0edge-node=20config=E5=91=BD?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/edge-node/main.go | 38 +++++++++++++++++++++++++++++++++- internal/configs/api_config.go | 4 ++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/cmd/edge-node/main.go b/cmd/edge-node/main.go index a3b4f25..4cf4233 100644 --- a/cmd/edge-node/main.go +++ b/cmd/edge-node/main.go @@ -16,6 +16,7 @@ import ( "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" "github.com/iwind/gosock/pkg/gosock" + "gopkg.in/yaml.v3" "net" "net/http" _ "net/http/pprof" @@ -30,7 +31,7 @@ func main() { var app = apps.NewAppCmd(). Version(teaconst.Version). Product(teaconst.ProductName). - Usage(teaconst.ProcessName + " [-v|start|stop|restart|status|quit|test|reload|service|daemon|pprof|accesslog|uninstall]"). + Usage(teaconst.ProcessName + " [-v|start|stop|restart|status|quit|test|reload|service|daemon|config|pprof|accesslog|uninstall]"). Usage(teaconst.ProcessName + " [trackers|goman|conns|gc|bandwidth|disk|cache.garbage]"). Usage(teaconst.ProcessName + " [ip.drop|ip.reject|ip.remove|ip.close] IP") @@ -524,6 +525,41 @@ func main() { fmt.Println("[ERROR]" + params.GetString("error")) } }) + app.On("config", func() { + var configString = os.Args[len(os.Args)-1] + if configString == "config" { + fmt.Println("Usage: edge-node config '\nrpc.endpoints: [\"...\"]\nnodeId: \"...\"\nsecret: \"...\"\n'") + return + } + + var config = &configs.APIConfig{} + err := yaml.Unmarshal([]byte(configString), config) + if err != nil { + fmt.Println("[ERROR]decode config failed: " + err.Error()) + return + } + + err = config.Init() + if err != nil { + fmt.Println("[ERROR]validate config failed: " + err.Error()) + return + } + + // marshal again + configYAML, err := yaml.Marshal(config) + if err != nil { + fmt.Println("[ERROR]encode config failed: " + err.Error()) + return + } + + err = os.WriteFile(Tea.Root + "/configs/api_node.yaml", configYAML, 0666) + if err != nil { + fmt.Println("[ERROR]write config failed: " + err.Error()) + return + } + + fmt.Println("success") + }) app.Run(func() { var node = nodes.NewNode() node.Start() diff --git a/internal/configs/api_config.go b/internal/configs/api_config.go index 42585f8..4cc9cc5 100644 --- a/internal/configs/api_config.go +++ b/internal/configs/api_config.go @@ -12,8 +12,8 @@ const oldConfigFileName = "api.yaml" type APIConfig struct { OldRPC struct { - Endpoints []string `yaml:"endpoints" json:"endpoints"` - DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"` + Endpoints []string `yaml:"endpoints,omitempty" json:"endpoints"` + DisableUpdate bool `yaml:"disableUpdate,omitempty" json:"disableUpdate"` } `yaml:"rpc,omitempty" json:"rpc"` RPCEndpoints []string `yaml:"rpc.endpoints,flow" json:"rpc.endpoints"`