diff --git a/cmd/skywire-cli/commands/dmsghttp/root.go b/cmd/skywire-cli/commands/dmsghttp/root.go new file mode 100644 index 000000000..b27373057 --- /dev/null +++ b/cmd/skywire-cli/commands/dmsghttp/root.go @@ -0,0 +1,102 @@ +// Package clidmsghttp cmd/skywire-cli/commands/dmsghttp/root.go +package clidmsghttp + +import ( + "context" + "encoding/json" + "io" + "io/ioutil" + "net/http" + "os" + + "github.com/spf13/cobra" + + "github.com/skycoin/skywire-utilities/pkg/cmdutil" + "github.com/skycoin/skywire-utilities/pkg/httputil" + "github.com/skycoin/skywire-utilities/pkg/logging" + "github.com/skycoin/skywire-utilities/pkg/skyenv" +) + +var ( + path string +) + +func init() { + dmsghttpCmd.Flags().SortFlags = false + dmsghttpCmd.Flags().StringVarP(&path, "path", "p", "/opt/skywire/dmsghttp-config.json", "path of dmsghttp-config file, default is for pkg installation") +} + +// RootCmd is surveyCmd +var RootCmd = dmsghttpCmd + +var dmsghttpCmd = &cobra.Command{ + Use: "dmsghttp update", + Short: "update dmsghttp-config.json file from config bootstrap service", + Run: func(cmd *cobra.Command, args []string) { + log := logging.MustGetLogger("dmsghttp_updater") + + ctx, cancel := cmdutil.SignalContext(context.Background(), log) + defer cancel() + go func() { + <-ctx.Done() + cancel() + os.Exit(1) + }() + + dmsghttpConf, err := fetchDmsghttpConf() + if err != nil { + log.WithError(err).Error("Cannot fetching updated dmsghttp-config data") + } + + file, err := json.MarshalIndent(dmsghttpConf, "", " ") + if err != nil { + log.WithError(err).Error("Error accurs during marshal content to json file") + } + + err = ioutil.WriteFile(path, file, 0600) + if err != nil { + log.WithError(err).Errorf("Cannot save new dmsghttp-config.json file at %s", path) + } + }, +} + +type dmsghttpConf struct { //nolint + Test httputil.DMSGHTTPConf `json:"test"` + Prod httputil.DMSGHTTPConf `json:"prod"` +} + +func fetchDmsghttpConf() (dmsghttpConf, error) { + var newConf dmsghttpConf + var prodConf httputil.DMSGHTTPConf + prodResp, err := http.Get(skyenv.ServiceConfAddr + "/dmsghttp") + if err != nil { + return newConf, err + } + defer prodResp.Body.Close() //nolint + body, err := io.ReadAll(prodResp.Body) + if err != nil { + return newConf, err + } + err = json.Unmarshal(body, &prodConf) + if err != nil { + return newConf, err + } + newConf.Prod = prodConf + + var testConf httputil.DMSGHTTPConf + testResp, err := http.Get(skyenv.TestServiceConfAddr + "/dmsghttp") + if err != nil { + return newConf, err + } + defer testResp.Body.Close() //nolint + body, err = io.ReadAll(testResp.Body) + if err != nil { + return newConf, err + } + err = json.Unmarshal(body, &testConf) + if err != nil { + return newConf, err + } + newConf.Test = testConf + return newConf, nil +} diff --git a/cmd/skywire-cli/commands/root.go b/cmd/skywire-cli/commands/root.go index 417f273f0..b474a1276 100644 --- a/cmd/skywire-cli/commands/root.go +++ b/cmd/skywire-cli/commands/root.go @@ -15,6 +15,7 @@ import ( "github.com/skycoin/skywire-utilities/pkg/buildinfo" clicompletion "github.com/skycoin/skywire/cmd/skywire-cli/commands/completion" cliconfig "github.com/skycoin/skywire/cmd/skywire-cli/commands/config" + clidmsghttp "github.com/skycoin/skywire/cmd/skywire-cli/commands/dmsghttp" clidmsgpty "github.com/skycoin/skywire/cmd/skywire-cli/commands/dmsgpty" clilog "github.com/skycoin/skywire/cmd/skywire-cli/commands/log" climdisc "github.com/skycoin/skywire/cmd/skywire-cli/commands/mdisc" @@ -195,6 +196,7 @@ func init() { cliskysocksc.RootCmd, treeCmd, docCmd, + clidmsghttp.RootCmd, ) var jsonOutput bool RootCmd.PersistentFlags().BoolVar(&jsonOutput, internal.JSONString, false, "print output in json")