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

new flag --confpath on generate config #1683

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
81 changes: 54 additions & 27 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func init() {
gHiddenFlags = append(gHiddenFlags, "envs")
genConfigCmd.Flags().BoolVar(&noFetch, "nofetch", false, "do not fetch the services from the service conf url")
gHiddenFlags = append(gHiddenFlags, "nofetch")
genConfigCmd.Flags().StringVar(&configServicePath, "confpath", "", "path of config-service offline file")
genConfigCmd.Flags().BoolVar(&noDefaults, "nodefaults", false, "do not use hardcoded defaults for production / test services")
gHiddenFlags = append(gHiddenFlags, "nodefaults")
genConfigCmd.Flags().StringVar(&ver, "version", scriptExecString("${VERSION}"), "custom version testing override\033[0m")
Expand Down Expand Up @@ -471,38 +472,64 @@ var genConfigCmd = &cobra.Command{
log := logger

if !noFetch {
// set default service conf url if none is specified
if serviceConfURL == "" {
serviceConfURL = utilenv.ServiceConfAddr
}
//use test deployment
if isTestEnv {
serviceConfURL = utilenv.TestServiceConfAddr
}
// enable errors from service conf fetch from the combination of these flags
wasStdout := isStdout
if isStdout && isHide {
isStdout = false
}
// create an http client to fetch the services
client := http.Client{
Timeout: time.Second * 15, // Timeout after 15 seconds
}
// Make the HTTP GET request
res, err := client.Get(fmt.Sprint(serviceConfURL))
if err != nil {
//silence errors for stdout
if !isStdout {
log.WithError(err).Error("Failed to fetch servers\n")
log.Warn("Falling back on hardcoded servers")
var body []byte
var err error

if configServicePath != "" {
body, err = os.ReadFile(configServicePath)
if err != nil {
if !isStdout {
log.WithError(err).Error("Failed to read config service from file\n")
log.Warn("Falling back on hardcoded servers")
}
} else {
//fill in services struct with the response
err = json.Unmarshal(body, &services)
if err != nil {
log.WithError(err).Fatal("Failed to unmarshal json response\n")
}
if !isStdout {
log.Infof("Fetched service endpoints from '%s'", serviceConfURL)
}

// reset the state of isStdout
isStdout = wasStdout
}
} else {
if res.Body != nil {
defer res.Body.Close() //nolint
// set default service conf url if none is specified
if serviceConfURL == "" {
serviceConfURL = utilenv.ServiceConfAddr
}
//use test deployment
if isTestEnv {
serviceConfURL = utilenv.TestServiceConfAddr
}
// enable errors from service conf fetch from the combination of these flags

if isStdout && isHide {
isStdout = false
}
body, err := io.ReadAll(res.Body)
// create an http client to fetch the services
client := http.Client{
Timeout: time.Second * 15, // Timeout after 15 seconds
}
// Make the HTTP GET request
res, err := client.Get(fmt.Sprint(serviceConfURL))
if err != nil {
log.WithError(err).Fatal("Failed to read response\n")
//silence errors for stdout
if !isStdout {
log.WithError(err).Error("Failed to fetch servers\n")
log.Warn("Falling back on hardcoded servers")
}
} else {
if res.Body != nil {
defer res.Body.Close() //nolint
}
body, err = io.ReadAll(res.Body)
if err != nil {
log.WithError(err).Fatal("Failed to read response\n")
}
}
//fill in services struct with the response
err = json.Unmarshal(body, &services)
Expand Down
1 change: 1 addition & 0 deletions cmd/skywire-cli/commands/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var (
disableProxyServerAutostart bool
proxyServerPass string
proxyClientPass string
configServicePath string
)

// RootCmd contains commands that interact with the config of local skywire-visor
Expand Down
Loading