From 945f78608355b1aa183e40a5c6eea5d2f65e6868 Mon Sep 17 00:00:00 2001 From: Moses Narrow <36607567+0pcom@users.noreply.github.com> Date: Fri, 9 Feb 2024 12:44:29 -0600 Subject: [PATCH] Fix `skywire-cli config gen --all` (#1735) * fix erroneous behavior on return from prerun function * fix confiig gen logging for service conf url * adjust logging for config gen * fix logic for fetching services configuration * make format --- cmd/skywire-cli/commands/config/gen.go | 120 +++++++++++++++---------- 1 file changed, 71 insertions(+), 49 deletions(-) diff --git a/cmd/skywire-cli/commands/config/gen.go b/cmd/skywire-cli/commands/config/gen.go index 84dba8cebe..fa0e95b775 100644 --- a/cmd/skywire-cli/commands/config/gen.go +++ b/cmd/skywire-cli/commands/config/gen.go @@ -252,7 +252,7 @@ var genConfigCmd = &cobra.Command{ envfile = envfileLinux } fmt.Println(envfile) - return + os.Exit(0) } //--all unhides flags, prints help menu, and exits @@ -263,7 +263,7 @@ var genConfigCmd = &cobra.Command{ } cmd.Flags().MarkHidden("all") //nolint cmd.Help() //nolint - return + os.Exit(0) } //set default output filename if output == "" { @@ -293,6 +293,10 @@ var genConfigCmd = &cobra.Command{ if isUsrEnv { isHypervisor = true } + //use test deployment + if isTestEnv { + serviceConfURL = utilenv.TestServiceConfAddr + } var err error if isDmsgHTTP { if isPkgEnv { @@ -372,13 +376,33 @@ var genConfigCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { log := logger + wasStdout := isStdout + var body []byte + var err error + // enable errors from service conf fetch from the combination of these flags + if isStdout && isHide { + isStdout = false + } if !noFetch { - wasStdout := isStdout - var body []byte - var err error - - if configServicePath != "" { + // create an http client to fetch the services + client := http.Client{ + Timeout: time.Second * 15, // Timeout after 15 seconds + } + if serviceConfURL == "" { + serviceConfURL = utilenv.ServiceConfAddr + } + if !isStdout { + log.Infof("Fetching service endpoints from %s", serviceConfURL) + } + // 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 services-config.json") + } body, err = os.ReadFile(configServicePath) if err != nil { if !isStdout { @@ -389,67 +413,65 @@ var genConfigCmd = &cobra.Command{ //fill in services struct with the response err = json.Unmarshal(body, &servicesConfig) if err != nil { - log.WithError(err).Fatal("Failed to unmarshal json response\n") - } - if !isStdout { - log.Infof("Fetched service endpoints from '%s'", serviceConfURL) - } - services = servicesConfig.Prod - if isTestEnv { - services = servicesConfig.Test + if !isStdout { + log.WithError(err).Error("Failed to unmarshal services-config.json file\n") + } + } else { + services = servicesConfig.Prod + if isTestEnv { + services = servicesConfig.Test + } } - // reset the state of isStdout - isStdout = wasStdout + } } else { - // set default service conf url if none is specified - if serviceConfURL == "" { - serviceConfURL = utilenv.ServiceConfAddr + //nil error on service conf fetch + if res.Body != nil { + defer res.Body.Close() //nolint } - //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 - } - // create an http client to fetch the services - client := http.Client{ - Timeout: time.Second * 15, // Timeout after 15 seconds + body, err = io.ReadAll(res.Body) + if err != nil { + log.WithError(err).Error("Failed to read http response\n") } - // Make the HTTP GET request - res, err := client.Get(fmt.Sprint(serviceConfURL)) + //fill in services struct with the response + err = json.Unmarshal(body, &services) if err != nil { - //silence errors for stdout if !isStdout { - log.WithError(err).Error("Failed to fetch servers\n") + log.WithError(err).Error("Failed to unmarshal json response to services struct\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") + if !isStdout { + log.Infof("Fetched service endpoints from '%s'", serviceConfURL) } } + } + } else { + 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) + err = json.Unmarshal(body, &servicesConfig) if err != nil { - log.WithError(err).Fatal("Failed to unmarshal json response\n") + if !isStdout { + log.WithError(err).Error("Failed to unmarshal json response to services struct\n") + log.Warn("Falling back on hardcoded servers") + } } - if !isStdout { - log.Infof("Fetched service endpoints from '%s'", serviceConfURL) + services = servicesConfig.Prod + if isTestEnv { + services = servicesConfig.Test } - - // reset the state of isStdout - isStdout = wasStdout } + } + // reset the state of isStdout + isStdout = wasStdout // Read in old config and obtain old secret key or generate a new random secret key // and obtain old hypervisors (if any) var oldConf visorconfig.V1