Skip to content

Commit

Permalink
Implement the PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
baduker committed Nov 17, 2023
1 parent bcb4ee8 commit 2634e57
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
13 changes: 8 additions & 5 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ 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) {
resp, err := c.MakeRequest("/validate_token", false)
resp, err := c.MakeRequest("GET", "/validate_token", false)
if err != nil {
return false, err
}
Expand All @@ -86,7 +86,7 @@ func (c *Client) TokenIsValid() (bool, error) {

// IsPingable calls the API /ping to determine whether the API can be reached.
func (c *Client) IsPingable() error {
resp, err := c.MakeRequest("/ping", false)
resp, err := c.MakeRequest("GET", "/ping", false)
if err != nil {
return err
}
Expand All @@ -97,8 +97,11 @@ func (c *Client) IsPingable() error {
return nil
}

// MakeRequest makes a http request to the given path (which may be a full URL).
func (c *Client) MakeRequest(path string, isFullURL bool) (*http.Response, error) {
// 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 {
Expand All @@ -107,7 +110,7 @@ func (c *Client) MakeRequest(path string, isFullURL bool) (*http.Response, error
url = fmt.Sprintf("%s%s", c.APIBaseURL, path)
}

req, err := c.NewRequest("GET", url, nil)
req, err := c.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
Expand Down
21 changes: 13 additions & 8 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,31 @@ places.
You can also override certain default settings to suit your preferences.
`,
RunE: func(cmd *cobra.Command, args []string) error {
configuration := LoadUserConfig()
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())
},
}

// LoadUserConfig wraps the logic for loading the user config
func LoadUserConfig() config.Config {
// 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.Fprintln(os.Stderr, err)
os.Exit(1)
fmt.Fprintf(os.Stderr, "Invalid user config: %v\n", err)
return cfg, err

}
return cfg
return cfg, nil
}

func runConfigure(configuration config.Config, flags *pflag.FlagSet) error {
Expand Down
40 changes: 18 additions & 22 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ type ExerciseSolution struct {

// solutionDownload is a helper container for managing the download process.
type solutionDownload struct {
// either/or
slug string
uuid string
// optional
track string
team string

slug, uuid string // slug and uuid are mutually exclusive and only one can be specified either one at run time
track string
team string
solutionURL string

solution *ExerciseSolution
Expand All @@ -91,7 +87,11 @@ latest solution.
Download other people's solutions by providing the UUID.
`,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := LoadUserConfig()
cfg, err := LoadUserConfigFile()
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid user config: %v\n", err)
return err
}
return runDownload(cfg, cmd.Flags(), args)
},
}
Expand All @@ -102,29 +102,25 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
return err
}

token := usrCfg.GetString("token")
apiURL := usrCfg.GetString("apibaseurl")
usrWorkspace := usrCfg.GetString("workspace")

isForceDownload, err := flags.GetBool("force")
client, err := api.NewClient(usrCfg.GetString("token"), usrCfg.GetString("apibaseurl"))
if err != nil {
return err
}

client, err := api.NewClient(token, apiURL)
download, err := newDownload(client, flags, usrCfg)
if err != nil {
return err
}

download, err := newDownload(client, flags, usrCfg)
metadata := download.solution.metadata()
exerciseDir := metadata.Exercise(usrCfg.GetString("workspace")).MetadataDir()

forceDownload, err := flags.GetBool("force")
if err != nil {
return err
}

metadata := download.solution.metadata()
exerciseDir := metadata.Exercise(usrWorkspace).MetadataDir()

if err := createExerciseDir(exerciseDir, isForceDownload); err != nil {
if err := createExerciseDir(exerciseDir, forceDownload); err != nil {
return err
}
// This writes the metadata file to the exercise directory.
Expand All @@ -140,7 +136,7 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
return nil
}

// createExerciseDir is a helper that creates the exercise directory and checks if it already exists.
// // createExerciseDir creates the exercise directory and checks if it already exists.
func createExerciseDir(dirName string, force bool) error {
if _, err := os.Stat(dirName); !force && err == nil {
return fmt.Errorf("directory '%s' already exists, use --force to overwrite", dirName)
Expand All @@ -162,7 +158,7 @@ func newDownload(client *api.Client, flags *pflag.FlagSet, usrCfg *viper.Viper)
}
d.buildSolutionURL(usrCfg.GetString("apibaseurl"))

res, err := client.MakeRequest(d.solutionURL, true)
res, err := client.MakeRequest("GET", d.solutionURL, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -310,7 +306,7 @@ func (sf solutionFile) fetchExerciseFiles(client *api.Client, targetDir string)

exerciseFilePath := sf.relativePath()

res, err := client.MakeRequest(url, true)
res, err := client.MakeRequest("GET", url, true)
if err != nil {
return err
}
Expand Down

0 comments on commit 2634e57

Please sign in to comment.