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

Initial refactor for list command #1117

Closed
Closed
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
41 changes: 27 additions & 14 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,48 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {

// TokenIsValid calls the API to determine whether the token is valid.
func (c *Client) TokenIsValid() (bool, error) {
url := fmt.Sprintf("%s/validate_token", c.APIBaseURL)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return false, err
}
resp, err := c.Do(req)
resp, err := c.MakeRequest("GET", "/validate_token", false)
if err != nil {
return false, err
}
defer resp.Body.Close()

return resp.StatusCode == http.StatusOK, nil
}

// IsPingable calls the API /ping to determine whether the API can be reached.
func (c *Client) IsPingable() error {
url := fmt.Sprintf("%s/ping", c.APIBaseURL)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := c.Do(req)
resp, err := c.MakeRequest("GET", "/ping", false)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API returned %s", resp.Status)
}
return nil
}

// MakeRequest makes a http request to the given path.
// If isFullURL is true, the function treats the path as a full URL.
// If isFullURL is false, the function treats the path as a relative path and prepends the API base URL.
// The method parameter allows for different HTTP methods (e.g., "GET", "POST").
func (c *Client) MakeRequest(method string, path string, isFullURL bool) (*http.Response, error) {
var url string

if isFullURL {
url = path
} else {
url = fmt.Sprintf("%s%s", c.APIBaseURL, path)
}

req, err := c.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}
40 changes: 27 additions & 13 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,33 @@ places.
You can also override certain default settings to suit your preferences.
`,
RunE: func(cmd *cobra.Command, args []string) error {
configuration := config.NewConfig()

viperConfig.AddConfigPath(configuration.Dir)
viperConfig.SetConfigName("user")
viperConfig.SetConfigType("json")
// Ignore error. If the file doesn't exist, that is fine.
_ = viperConfig.ReadInConfig()
configuration.UserViperConfig = viperConfig

return runConfigure(configuration, cmd.Flags())
cfg, err := LoadUserConfigFile()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading user config: %v\n", err)
return err
}
return runConfigure(cfg, cmd.Flags())
},
}

// LoadUserConfigFile creates a new configuration and loads the user config data into it.
func LoadUserConfigFile() (config.Config, error) {
cfg := config.NewConfig()
viperConfig := viper.New()
viperConfig.AddConfigPath(cfg.Dir)
viperConfig.SetConfigName("user")
viperConfig.SetConfigType("json")
// Ignore error. If the file doesn't exist, that is fine.
_ = viperConfig.ReadInConfig()
cfg.UserViperConfig = viperConfig
if err := validateUserConfig(cfg.UserViperConfig); err != nil {
fmt.Fprintf(os.Stderr, "Invalid user config: %v\n", err)
return cfg, err

}
return cfg, nil
}

func runConfigure(configuration config.Config, flags *pflag.FlagSet) error {
cfg := configuration.UserViperConfig

Expand Down Expand Up @@ -76,7 +90,7 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error {
baseURL = configuration.DefaultBaseURL
}

// By default we verify that
// By default, we verify that
// - the configured API URL is reachable.
// - the configured token is valid.
skipVerification, err := flags.GetBool("no-verify")
Expand Down Expand Up @@ -111,7 +125,7 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error {

// If we don't have a token then explain how to set it and bail.
if token == "" {
return fmt.Errorf("There is no token configured. Find your token on %s, and call this command again with --token=<your-token>.", tokenURL)
return fmt.Errorf("there is no token configured. Find your token on %s, and call this command again with --token=<your-token>", tokenURL)
}

// Verify that the token is valid.
Expand All @@ -125,7 +139,7 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error {
return err
}
if !ok {
return fmt.Errorf("The token '%s' is invalid. Find your token on %s.", token, tokenURL)
return fmt.Errorf("the token '%s' is invalid. Find your token on %s", token, tokenURL)
}
}

Expand Down
Loading
Loading