Skip to content

Commit 1bb612f

Browse files
Katrina Owennywilken
authored andcommitted
Get rid of the workspace.Locate behavior and comms package (#696)
* Simplify open command This no longer accepts just an exercise name. It mirrors the behavior of the submit command where you pass the path to the exercise you want to open in the browser. In the future we can expand this to take --exercise, --track, and --team. * Simplify submit command We determine the exact exercise directory based on the filepaths. Since we have the directory, we don't need to do complicated guessing, we can just load in the exercise metadata directly. * Rename slug to param in download command for clarity * Rename exercise to slug in download for clarity * Rename dir variable in download command for clarity We were overloading the variable, which makes it confusing. * Add missing error handling in download command * Add metadata dir method to exercise type We're going to want to ensure that the directory exists. For the moment, this directory is the same as the exercise directory, but we're working towards putting this in a subdirectory to match common industry conventions. * Simplify download command to rely on Exercise type * Get rid of solution path abstraction in workspace We've moved the idea of the exercise metadata path onto the Exercise type. * Delete complicated Locate behavior in workspace It's no longer used. * Delete unused comms package * Tweak download command for clarity Simplify a conditional.
1 parent 11b04d8 commit 1bb612f

12 files changed

Lines changed: 28 additions & 853 deletions

cmd/download.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,19 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
6060
if err != nil {
6161
return err
6262
}
63-
exercise, err := flags.GetString("exercise")
63+
slug, err := flags.GetString("exercise")
6464
if err != nil {
6565
return err
6666
}
67-
if uuid == "" && exercise == "" {
67+
if uuid == "" && slug == "" {
6868
return errors.New("need an --exercise name or a solution --uuid")
6969
}
7070

71-
var slug string
72-
if uuid == "" {
73-
slug = "latest"
74-
} else {
75-
slug = uuid
71+
param := "latest"
72+
if param == "" {
73+
param = uuid
7674
}
77-
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), slug)
75+
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), param)
7876

7977
client, err := api.NewClient(usrCfg.GetString("token"), usrCfg.GetString("apibaseurl"))
8078
if err != nil {
@@ -98,7 +96,7 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
9896

9997
if uuid == "" {
10098
q := req.URL.Query()
101-
q.Add("exercise_id", exercise)
99+
q.Add("exercise_id", slug)
102100
if track != "" {
103101
q.Add("track_id", track)
104102
}
@@ -144,28 +142,26 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
144142
IsRequester: payload.Solution.User.IsRequester,
145143
}
146144

147-
dir := usrCfg.GetString("workspace")
145+
root := usrCfg.GetString("workspace")
148146
if solution.Team != "" {
149-
dir = filepath.Join(dir, "teams", solution.Team)
147+
root = filepath.Join(root, "teams", solution.Team)
150148
}
151149
if !solution.IsRequester {
152-
dir = filepath.Join(dir, "users", solution.Handle)
150+
root = filepath.Join(root, "users", solution.Handle)
153151
}
154-
dir = filepath.Join(dir, solution.Track)
155152

156-
os.MkdirAll(dir, os.FileMode(0755))
157-
ws, err := workspace.New(dir)
158-
if err != nil {
159-
return err
153+
exercise := workspace.Exercise{
154+
Root: root,
155+
Track: solution.Track,
156+
Slug: solution.Exercise,
160157
}
161158

162-
dir, err = ws.SolutionPath(solution.Exercise, solution.ID)
163-
if err != nil {
159+
dir := exercise.MetadataDir()
160+
161+
if err := os.MkdirAll(dir, os.FileMode(0755)); err != nil {
164162
return err
165163
}
166164

167-
os.MkdirAll(dir, os.FileMode(0755))
168-
169165
err = solution.Write(dir)
170166
if err != nil {
171167
return err

cmd/open.go

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package cmd
22

33
import (
4-
"errors"
5-
"fmt"
6-
74
"github.com/exercism/cli/browser"
8-
"github.com/exercism/cli/comms"
9-
"github.com/exercism/cli/config"
105
"github.com/exercism/cli/workspace"
116
"github.com/spf13/cobra"
12-
"github.com/spf13/viper"
137
)
148

159
// openCmd opens the designated exercise in the browser.
@@ -19,74 +13,16 @@ var openCmd = &cobra.Command{
1913
Short: "Open an exercise on the website.",
2014
Long: `Open the specified exercise to the solution page on the Exercism website.
2115
22-
Pass either the name of an exercise, or the path to the directory that contains
23-
the solution you want to see on the website.
16+
Pass the path to the directory that contains the solution you want to see on the website.
2417
`,
2518
Args: cobra.ExactArgs(1),
2619
RunE: func(cmd *cobra.Command, args []string) error {
27-
cfg := config.NewConfig()
28-
29-
v := viper.New()
30-
v.AddConfigPath(cfg.Dir)
31-
v.SetConfigName("user")
32-
v.SetConfigType("json")
33-
// Ignore error. If the file doesn't exist, that is fine.
34-
_ = v.ReadInConfig()
35-
36-
ws, err := workspace.New(v.GetString("workspace"))
37-
if err != nil {
38-
return err
39-
}
40-
41-
paths, err := ws.Locate(args[0])
20+
solution, err := workspace.NewSolution(args[0])
4221
if err != nil {
4322
return err
4423
}
45-
46-
solutions, err := workspace.NewSolutions(paths)
47-
if err != nil {
48-
return err
49-
}
50-
51-
if len(solutions) == 0 {
52-
return nil
53-
}
54-
55-
if len(solutions) > 1 {
56-
var mine []*workspace.Solution
57-
for _, s := range solutions {
58-
if s.IsRequester {
59-
mine = append(mine, s)
60-
}
61-
}
62-
solutions = mine
63-
}
64-
65-
selection := comms.NewSelection()
66-
for _, solution := range solutions {
67-
selection.Items = append(selection.Items, solution)
68-
}
69-
for {
70-
prompt := `
71-
We found more than one. Which one did you mean?
72-
Type the number of the one you want to select.
73-
74-
%s
75-
> `
76-
option, err := selection.Pick(prompt)
77-
if err != nil {
78-
fmt.Println(err)
79-
continue
80-
}
81-
solution, ok := option.(*workspace.Solution)
82-
if ok {
83-
browser.Open(solution.URL)
84-
return nil
85-
}
86-
if err != nil {
87-
return errors.New("should never happen")
88-
}
89-
}
24+
browser.Open(solution.URL)
25+
return nil
9026
},
9127
}
9228

cmd/submit.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,11 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
125125
exerciseDir = dir
126126
}
127127

128-
dirs, err := ws.Locate(exerciseDir)
128+
solution, err := workspace.NewSolution(exerciseDir)
129129
if err != nil {
130130
return err
131131
}
132132

133-
sx, err := workspace.NewSolutions(dirs)
134-
if err != nil {
135-
return err
136-
}
137-
if len(sx) > 1 {
138-
msg := `
139-
140-
You are submitting files belonging to different solutions.
141-
Please submit the files for one solution at a time.
142-
143-
`
144-
return errors.New(msg)
145-
}
146-
solution := sx[0]
147-
148133
if !solution.IsRequester {
149134
// TODO: add test
150135
msg := `

comms/question.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

comms/question_test.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

comms/selection.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)