Skip to content

Commit

Permalink
open: support for --team
Browse files Browse the repository at this point in the history
  • Loading branch information
Smarticles101 committed Jan 8, 2019
1 parent 25f41c2 commit 1c1c997
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
47 changes: 29 additions & 18 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func runOpen(cfg config.Config, flags *pflag.FlagSet, args []string) error {
usrCfg := cfg.UserViperConfig
trackID, _ := flags.GetString("track")
exerciseID, _ := flags.GetString("exercise")
teamID, _ := flags.GetString("team")

if exerciseID == "" {
return fmt.Errorf("Must provide an `--exercise`")
Expand All @@ -68,6 +69,9 @@ func runOpen(cfg config.Config, flags *pflag.FlagSet, args []string) error {
if trackID != "" {
q.Add("track_id", trackID)
}
if teamID != "" {
q.Add("team_id", teamID)
}
req.URL.RawQuery = q.Encode()

res, err := client.Do(req)
Expand Down Expand Up @@ -101,35 +105,41 @@ func runOpen(cfg config.Config, flags *pflag.FlagSet, args []string) error {
return err
}

matchingExercises := make([]workspace.Exercise, 0, len(exercises))
matchingExerciseMeta := make([]*workspace.ExerciseMetadata, 0, len(exercises))
for _, exercise := range exercises {
if trackID != "" {
if exercise.Track == trackID && exercise.Slug == exerciseID {
matchingExercises = append(matchingExercises, exercise)
}
} else if exercise.Slug == exerciseID {
matchingExercises = append(matchingExercises, exercise)
metaDir := exercise.MetadataDir()
meta, err := workspace.NewExerciseMetadata(metaDir)
if err != nil {
return err
}

if meta.Exercise != exerciseID {
continue
}

if trackID != "" && meta.Track != trackID {
continue
}

if meta.Team != teamID {
continue
}

matchingExerciseMeta = append(matchingExerciseMeta, meta)
}

switch len(matchingExercises) {
switch len(matchingExerciseMeta) {
case 0:
return fmt.Errorf("No matching exercise found")
case 1:
metaDir := matchingExercises[0].MetadataDir()
meta, err := workspace.NewExerciseMetadata(metaDir)

if err != nil {
return err
}

url = meta.URL
url = matchingExerciseMeta[0].URL
break
default:
tracks := make([]string, 0, len(matchingExercises))
for _, exercise := range matchingExercises {
tracks := make([]string, 0, len(matchingExerciseMeta))
for _, exercise := range matchingExerciseMeta {
tracks = append(tracks, exercise.Track)
}

return fmt.Errorf("Please specify a track ID: %s", strings.Join(tracks, ", "))
}
}
Expand All @@ -153,6 +163,7 @@ func setupOpenFlags(flags *pflag.FlagSet) {
flags.BoolP("remote", "r", false, "checks for remote solutions")
flags.StringP("track", "t", "", "the track id")
flags.StringP("exercise", "e", "", "the exercise slug")
flags.StringP("team", "T", "", "the team slug")
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions cmd/open_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cmd
12 changes: 12 additions & 0 deletions workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workspace

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -57,6 +58,17 @@ func (ws Workspace) PotentialExercises() ([]Exercise, error) {
continue
}

if topInfo.Name() == "teams" {
subInfos, _ := ioutil.ReadDir(filepath.Join(ws.Dir, "teams"))
for _, subInfo := range subInfos {
teamWs, _ := New(filepath.Join(ws.Dir, "teams", subInfo.Name()))
fmt.Println(teamWs.Dir)
teamExercises, _ := teamWs.PotentialExercises()
exercises = append(exercises, teamExercises...)
}
continue
}

subInfos, err := ioutil.ReadDir(filepath.Join(ws.Dir, topInfo.Name()))
if err != nil {
return nil, err
Expand Down

0 comments on commit 1c1c997

Please sign in to comment.