Skip to content

Commit

Permalink
Merge pull request exercism#818 from exercism/download-identifier
Browse files Browse the repository at this point in the history
Extract logic for the URI variable identifying the exercise to download
  • Loading branch information
Katrina Owen authored Feb 27, 2019
2 parents 936bbc7 + 7c6cd9d commit 0ae6180
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,44 +54,40 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
return err
}

uuid, err := flags.GetString("uuid")
track, err := flags.GetString("track")
if err != nil {
return err
}
slug, err := flags.GetString("exercise")

team, err := flags.GetString("team")
if err != nil {
return err
}
if uuid != "" && slug != "" || uuid == slug {
return errors.New("need an --exercise name or a solution --uuid")
}

track, err := flags.GetString("track")
identifier, err := downloadIdentifier(flags)
if err != nil {
return err
}
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), identifier)

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

param := "latest"
if uuid != "" {
param = uuid
req, err := client.NewRequest("GET", url, nil)
if err != nil {
return err
}
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), param)

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

req, err := client.NewRequest("GET", url, nil)
slug, err := flags.GetString("exercise")
if err != nil {
return err
}

if uuid == "" {
q := req.URL.Query()
q.Add("exercise_id", slug)
Expand Down Expand Up @@ -252,6 +248,27 @@ func (dp downloadPayload) metadata() workspace.ExerciseMetadata {
}
}

// downloadIdentifier is the variable for the URI to initiate an exercise download.
func downloadIdentifier(flags *pflag.FlagSet) (string, error) {
uuid, err := flags.GetString("uuid")
if err != nil {
return "", err
}
slug, err := flags.GetString("exercise")
if err != nil {
return "", err
}
if uuid != "" && slug != "" || uuid == slug {
return "", errors.New("need an --exercise name or a solution --uuid")
}

identifier := "latest"
if uuid != "" {
identifier = uuid
}
return identifier, nil
}

func setupDownloadFlags(flags *pflag.FlagSet) {
flags.StringP("uuid", "u", "", "the solution UUID")
flags.StringP("track", "t", "", "the track ID")
Expand Down

0 comments on commit 0ae6180

Please sign in to comment.