Skip to content

Commit

Permalink
Get rid of the workspace.Locate behavior and comms package (sequelize…
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
Katrina Owen authored and nywilken committed Aug 7, 2018
1 parent 11b04d8 commit 1bb612f
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 853 deletions.
38 changes: 17 additions & 21 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,19 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
if err != nil {
return err
}
exercise, err := flags.GetString("exercise")
slug, err := flags.GetString("exercise")
if err != nil {
return err
}
if uuid == "" && exercise == "" {
if uuid == "" && slug == "" {
return errors.New("need an --exercise name or a solution --uuid")
}

var slug string
if uuid == "" {
slug = "latest"
} else {
slug = uuid
param := "latest"
if param == "" {
param = uuid
}
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), slug)
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), param)

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

if uuid == "" {
q := req.URL.Query()
q.Add("exercise_id", exercise)
q.Add("exercise_id", slug)
if track != "" {
q.Add("track_id", track)
}
Expand Down Expand Up @@ -144,28 +142,26 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
IsRequester: payload.Solution.User.IsRequester,
}

dir := usrCfg.GetString("workspace")
root := usrCfg.GetString("workspace")
if solution.Team != "" {
dir = filepath.Join(dir, "teams", solution.Team)
root = filepath.Join(root, "teams", solution.Team)
}
if !solution.IsRequester {
dir = filepath.Join(dir, "users", solution.Handle)
root = filepath.Join(root, "users", solution.Handle)
}
dir = filepath.Join(dir, solution.Track)

os.MkdirAll(dir, os.FileMode(0755))
ws, err := workspace.New(dir)
if err != nil {
return err
exercise := workspace.Exercise{
Root: root,
Track: solution.Track,
Slug: solution.Exercise,
}

dir, err = ws.SolutionPath(solution.Exercise, solution.ID)
if err != nil {
dir := exercise.MetadataDir()

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

os.MkdirAll(dir, os.FileMode(0755))

err = solution.Write(dir)
if err != nil {
return err
Expand Down
72 changes: 4 additions & 68 deletions cmd/open.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package cmd

import (
"errors"
"fmt"

"github.com/exercism/cli/browser"
"github.com/exercism/cli/comms"
"github.com/exercism/cli/config"
"github.com/exercism/cli/workspace"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// openCmd opens the designated exercise in the browser.
Expand All @@ -19,74 +13,16 @@ var openCmd = &cobra.Command{
Short: "Open an exercise on the website.",
Long: `Open the specified exercise to the solution page on the Exercism website.
Pass either the name of an exercise, or the path to the directory that contains
the solution you want to see on the website.
Pass the path to the directory that contains the solution you want to see on the website.
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := config.NewConfig()

v := viper.New()
v.AddConfigPath(cfg.Dir)
v.SetConfigName("user")
v.SetConfigType("json")
// Ignore error. If the file doesn't exist, that is fine.
_ = v.ReadInConfig()

ws, err := workspace.New(v.GetString("workspace"))
if err != nil {
return err
}

paths, err := ws.Locate(args[0])
solution, err := workspace.NewSolution(args[0])
if err != nil {
return err
}

solutions, err := workspace.NewSolutions(paths)
if err != nil {
return err
}

if len(solutions) == 0 {
return nil
}

if len(solutions) > 1 {
var mine []*workspace.Solution
for _, s := range solutions {
if s.IsRequester {
mine = append(mine, s)
}
}
solutions = mine
}

selection := comms.NewSelection()
for _, solution := range solutions {
selection.Items = append(selection.Items, solution)
}
for {
prompt := `
We found more than one. Which one did you mean?
Type the number of the one you want to select.
%s
> `
option, err := selection.Pick(prompt)
if err != nil {
fmt.Println(err)
continue
}
solution, ok := option.(*workspace.Solution)
if ok {
browser.Open(solution.URL)
return nil
}
if err != nil {
return errors.New("should never happen")
}
}
browser.Open(solution.URL)
return nil
},
}

Expand Down
17 changes: 1 addition & 16 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,11 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
exerciseDir = dir
}

dirs, err := ws.Locate(exerciseDir)
solution, err := workspace.NewSolution(exerciseDir)
if err != nil {
return err
}

sx, err := workspace.NewSolutions(dirs)
if err != nil {
return err
}
if len(sx) > 1 {
msg := `
You are submitting files belonging to different solutions.
Please submit the files for one solution at a time.
`
return errors.New(msg)
}
solution := sx[0]

if !solution.IsRequester {
// TODO: add test
msg := `
Expand Down
36 changes: 0 additions & 36 deletions comms/question.go

This file was deleted.

38 changes: 0 additions & 38 deletions comms/question_test.go

This file was deleted.

78 changes: 0 additions & 78 deletions comms/selection.go

This file was deleted.

Loading

0 comments on commit 1bb612f

Please sign in to comment.