Skip to content

Commit b50dbad

Browse files
committed
download: Infer track and exercise slug from CWD
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.
1 parent d95de6b commit b50dbad

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

cmd/download.go

+34
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ Download other people's solutions by providing the UUID.
5050
},
5151
}
5252

53+
func componentsAfterWorkspace(workspace string, n int) ([]string, bool) {
54+
cwd, err := os.Getwd()
55+
if err != nil {
56+
return nil, false
57+
}
58+
59+
if !strings.HasPrefix(cwd, workspace) {
60+
return nil, false
61+
}
62+
remaining := cwd[len(workspace):]
63+
if remaining[0] == '/' {
64+
remaining = remaining[1:]
65+
}
66+
67+
components := strings.Split(remaining, "/")
68+
if n <= len(components) {
69+
return components[:n], true
70+
}
71+
return nil, false
72+
}
73+
5374
func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
5475
usrCfg := cfg.UserViperConfig
5576
if err := validateUserConfig(usrCfg); err != nil {
@@ -162,6 +183,19 @@ func newDownload(flags *pflag.FlagSet, usrCfg *viper.Viper) (*download, error) {
162183
d.apibaseurl = usrCfg.GetString("apibaseurl")
163184
d.workspace = usrCfg.GetString("workspace")
164185

186+
if d.uuid == "" {
187+
if d.slug == "" {
188+
if components, ok := componentsAfterWorkspace(d.workspace, 2); ok {
189+
d.slug = components[1]
190+
}
191+
}
192+
if d.track == "" && d.team == "" {
193+
if components, ok := componentsAfterWorkspace(d.workspace, 1); ok {
194+
d.track = components[0]
195+
}
196+
}
197+
}
198+
165199
if err = d.needsSlugXorUUID(); err != nil {
166200
return nil, err
167201
}

0 commit comments

Comments
 (0)