Skip to content

Commit

Permalink
add new skywire-cli command as dmsghttp update
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpalide committed Jun 21, 2023
1 parent a33093b commit 3c685e3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
102 changes: 102 additions & 0 deletions cmd/skywire-cli/commands/dmsghttp/root.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions cmd/skywire-cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 3c685e3

Please sign in to comment.