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

Add list command #1113

Closed
wants to merge 1 commit into from
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
37 changes: 23 additions & 14 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,44 @@ 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("/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("/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
}

func (c *Client) MakeRequest(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("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}
28 changes: 18 additions & 10 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,27 @@ 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

configuration := LoadUserConfig()
return runConfigure(configuration, cmd.Flags())
},
}

func LoadUserConfig() config.Config {
cfg := config.NewConfig()
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.Fprintln(os.Stderr, err)
os.Exit(1)
}
return cfg
}

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

Expand Down Expand Up @@ -116,7 +124,7 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error {

// Verify that the token is valid.
if !skipVerification {
client, err := api.NewClient(token, baseURL)
client, err := api.NewClient("", baseURL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

if err != nil {
return err
}
Expand Down
Loading