Skip to content

Commit 936bbc7

Browse files
author
Katrina Owen
authored
Merge pull request #817 from exercism/metadata-to-exercise
Move some logic out of download command into exercise metadata type
2 parents a88b3f1 + 3700417 commit 936bbc7

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

cmd/download.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,7 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
130130
}
131131

132132
metadata := payload.metadata()
133-
134-
root := usrCfg.GetString("workspace")
135-
if metadata.Team != "" {
136-
root = filepath.Join(root, "teams", metadata.Team)
137-
}
138-
if !metadata.IsRequester {
139-
root = filepath.Join(root, "users", metadata.Handle)
140-
}
141-
142-
exercise := workspace.Exercise{
143-
Root: root,
144-
Track: metadata.Track,
145-
Slug: metadata.ExerciseSlug,
146-
}
147-
148-
dir := exercise.MetadataDir()
133+
dir := metadata.Exercise(usrCfg.GetString("workspace")).MetadataDir()
149134

150135
if err := os.MkdirAll(dir, os.FileMode(0755)); err != nil {
151136
return err

workspace/exercise_metadata.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,23 @@ func (em *ExerciseMetadata) PathToParent() string {
8787
}
8888
return filepath.Join(dir, em.Track)
8989
}
90+
91+
// Exercise is an implementation of a problem on disk.
92+
func (em *ExerciseMetadata) Exercise(workspace string) Exercise {
93+
return Exercise{
94+
Root: em.root(workspace),
95+
Track: em.Track,
96+
Slug: em.ExerciseSlug,
97+
}
98+
}
99+
100+
// root represents the root of the exercise.
101+
func (em *ExerciseMetadata) root(workspace string) string {
102+
if em.Team != "" {
103+
return filepath.Join(workspace, "teams", em.Team)
104+
}
105+
if !em.IsRequester {
106+
return filepath.Join(workspace, "users", em.Handle)
107+
}
108+
return workspace
109+
}

0 commit comments

Comments
 (0)