Skip to content

Commit

Permalink
download: Infer track and exercise slug from CWD
Browse files Browse the repository at this point in the history
If I'm in the `exercism/c` directory and invoke:

    exercism download --exercise hello-world

I would like `--track c` to be inferred.

If I'm in the `exercism/c/hello-world` directory and invoke without
arguments:

    exercism download

I would like both `--track c` and `--exercise hello-world` to be
inferred.
  • Loading branch information
petertseng committed Sep 15, 2019
1 parent d95de6b commit b50dbad
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ Download other people's solutions by providing the UUID.
},
}

func componentsAfterWorkspace(workspace string, n int) ([]string, bool) {
cwd, err := os.Getwd()
if err != nil {
return nil, false
}

if !strings.HasPrefix(cwd, workspace) {
return nil, false
}
remaining := cwd[len(workspace):]
if remaining[0] == '/' {
remaining = remaining[1:]
}

components := strings.Split(remaining, "/")
if n <= len(components) {
return components[:n], true
}
return nil, false
}

func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
usrCfg := cfg.UserViperConfig
if err := validateUserConfig(usrCfg); err != nil {
Expand Down Expand Up @@ -162,6 +183,19 @@ func newDownload(flags *pflag.FlagSet, usrCfg *viper.Viper) (*download, error) {
d.apibaseurl = usrCfg.GetString("apibaseurl")
d.workspace = usrCfg.GetString("workspace")

if d.uuid == "" {
if d.slug == "" {
if components, ok := componentsAfterWorkspace(d.workspace, 2); ok {
d.slug = components[1]
}
}
if d.track == "" && d.team == "" {
if components, ok := componentsAfterWorkspace(d.workspace, 1); ok {
d.track = components[0]
}
}
}

if err = d.needsSlugXorUUID(); err != nil {
return nil, err
}
Expand Down

0 comments on commit b50dbad

Please sign in to comment.