| 45 | } |
| 46 | |
| 47 | func runConfigure(configuration config.Config, flags *pflag.FlagSet) error { |
| 48 | cfg := configuration.UserViperConfig |
| 49 | |
| 50 | // Show the existing configuration and exit. |
| 51 | show, err := flags.GetBool("show") |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if show { |
| 56 | printCurrentConfig(configuration) |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | // If the command is run 'bare' and we have no token, |
| 61 | // explain how to set the token. |
| 62 | if flags.NFlag() == 0 && cfg.GetString("token") == "" { |
| 63 | tokenURL := config.TokenURL(cfg.GetString("apibaseurl")) |
| 64 | return fmt.Errorf("There is no token configured. Find your token on %s, and call this command again with --token=<your-token>.", tokenURL) |
| 65 | } |
| 66 | |
| 67 | // Determine the base API URL. |
| 68 | baseURL, err := flags.GetString("api") |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if baseURL == "" { |
| 73 | baseURL = cfg.GetString("apibaseurl") |
| 74 | } |
| 75 | if baseURL == "" { |
| 76 | baseURL = configuration.DefaultBaseURL |
| 77 | } |
| 78 | |
| 79 | // By default we verify that |
| 80 | // - the configured API URL is reachable. |
| 81 | // - the configured token is valid. |
| 82 | skipVerification, err := flags.GetBool("no-verify") |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | // Is the API URL reachable? |
| 88 | if !skipVerification { |
| 89 | client, err := api.NewClient("", baseURL) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | if err := client.IsPingable(); err != nil { |
| 95 | return fmt.Errorf("The base API URL '%s' cannot be reached.\n\n%s", baseURL, err) |
| 96 | } |
| 97 | } |
| 98 | // Finally, configure the URL. |
| 99 | cfg.Set("apibaseurl", baseURL) |
| 100 | |
| 101 | // Determine the token. |
| 102 | token, err := flags.GetString("token") |
| 103 | if err != nil { |
| 104 | return err |