Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] dmsghttp updater #1628

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading