-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathcommand.go
97 lines (83 loc) · 1.67 KB
/
command.go
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
87
88
89
90
91
92
93
94
95
96
97
package subcmd
import (
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/blang/semver"
"github.com/pkg/errors"
)
var (
Version string
reVersion = regexp.MustCompile(`\d+\.\d+\.\d+`)
)
type (
// Command sub command interface
Command interface {
Name() string
Summary() string
Usage()
Run([]string) error
}
// CommandRepository sub command repository interface
CommandRepository interface {
Find(string) (Command, error)
Commands() []Command
}
repository struct {
commands []Command
commandMap map[string]Command
}
)
func Repository() CommandRepository {
repo := &repository{
commandMap: make(map[string]Command),
}
repo.commands = []Command{
NewHelp(),
NewInit(),
NewInstall(),
NewUninstall(),
NewList(),
NewSwitch(),
NewUse(),
NewOff(),
NewVenv(),
NewCleanup(),
NewUpdate(),
}
for _, c := range repo.commands {
repo.commandMap[c.Name()] = c
}
return repo
}
func (repo *repository) Find(name string) (Command, error) {
c, ok := repo.commandMap[name]
if !ok {
return nil, errors.New("command is missing")
}
return c, nil
}
func (repo *repository) Commands() []Command {
return repo.commands[:]
}
func writeEnvPython(filename string, installdir string) error {
env := fmt.Sprintf(`PYTHONBREW_VERSION=%s
PYTHONBREW_VERSION_BIN=%s
PYTHONBREW_VERSION_LIB=%s
`, installdir, filepath.Join(installdir, "bin"), filepath.Join(installdir, "lib"))
fp, err := os.Create(filename)
if err != nil {
return err
}
defer fp.Close()
fp.WriteString(env)
return nil
}
func semverVersion() semver.Version {
v := Version
if loc := reVersion.FindStringIndex(v); loc != nil && loc[0] > 0 {
v = v[loc[0]:]
}
return semver.MustParse(v)
}