-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathopen.go
More file actions
86 lines (75 loc) · 1.73 KB
/
open.go
File metadata and controls
86 lines (75 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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"
)
// openCmd opens the designated exercise in the browser.
var openCmd = &cobra.Command{
Use: "open",
Aliases: []string{"o"},
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.
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.NewUserConfig()
if err != nil {
return err
}
ws := workspace.New(cfg.Workspace)
paths, err := ws.Locate(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")
}
}
},
}
func init() {
RootCmd.AddCommand(openCmd)
}