-
-
Notifications
You must be signed in to change notification settings - Fork 361
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
download: Infer track and exercise slug from CWD #880
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,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 _, slug, ok := trackAndSlugFromCwd(d.workspace); ok && slug != "" { | ||
d.slug = slug | ||
} | ||
} | ||
if d.track == "" && d.team == "" { | ||
if track, _, ok := trackAndSlugFromCwd(d.workspace); ok { | ||
d.track = track | ||
} | ||
} | ||
} | ||
|
||
if err = d.needsSlugXorUUID(); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -349,6 +362,46 @@ func (sf solutionFile) relativePath() string { | |
return filepath.FromSlash(file) | ||
} | ||
|
||
// trackAndSlugFromCwd infers track and slug from current working directory, | ||
// by finding the two path components coming immediately after the workspace. | ||
func trackAndSlugFromCwd(workspace string) (string, string, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. surely, it would be possible to call this
petertseng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return "", "", false | ||
} | ||
|
||
if !strings.HasPrefix(cwd, workspace) { | ||
return "", "", false | ||
} | ||
|
||
afterWorkspace := strings.TrimPrefix(cwd, workspace) | ||
separator := string([]rune{os.PathSeparator}) | ||
afterWorkspace = strings.TrimPrefix(afterWorkspace, separator) | ||
|
||
components := strings.Split(afterWorkspace, separator) | ||
if len(components) == 0 { | ||
return "", "", false | ||
} | ||
|
||
// We have at least one component, possibly more. | ||
// First is presumably the track, but check whether it actually is one. | ||
track := components[0] | ||
if !isTrack(track) { | ||
return "", "", false | ||
} | ||
|
||
slug := "" | ||
if len(components) >= 2 { | ||
slug = components[1] | ||
} | ||
|
||
return track, slug, true | ||
} | ||
|
||
func isTrack(dir string) bool { | ||
return dir != "users" && dir != "teams" | ||
} | ||
|
||
func setupDownloadFlags(flags *pflag.FlagSet) { | ||
flags.StringP("uuid", "u", "", "the solution UUID") | ||
flags.StringP("track", "t", "", "the track ID") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, the
!= ""
check is, strictly speaking, unnecessary. If we have gotten to this point, thend.slug
is already""
and setting it from""
to""
does nothing.