-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (134 loc) · 2.92 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"log"
"os"
"path/filepath"
"time"
"github.com/urfave/cli/v2"
)
var (
SVHome string
SVRoot string
SVBin string
SVCache string
SVDownload string
)
const Ver = "v1.1.9"
func main() {
l.SetLevel("debug")
app := cli.NewApp()
app.Usage = "switch version"
app.Version = Ver
app.Compiled = time.Now()
app.EnableBashCompletion = true
//app.CustomAppHelpTemplate = "add sv to your ~/.bashrc or ~/.zshrc. export PATH=\"$HOME/.sv/bin:$PATH\""
//app.Action = baseCmd
app.Commands = []*cli.Command{
{
Name: "list",
Usage: "show all local versions",
UsageText: "sv ls",
Action: baseCmd,
Aliases: []string{"ls", "l"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "remote",
Aliases: []string{"r"},
Usage: "show all remote versions",
Required: false,
},
},
}, {
Name: "use",
Usage: "input a specific local version",
UsageText: "sv use <version>",
Action: baseCmd,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "remote",
Aliases: []string{"r"},
Usage: "input a specific remote version",
Required: false,
},
},
}, {
Name: "install",
Usage: "install a specific remote version",
UsageText: "sv install <version>",
Action: baseCmd,
Aliases: []string{"i"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "latest",
Usage: "install the latest version",
Required: false,
},
},
}, {
Name: "uninstall",
Usage: "uninstall a specific local version",
UsageText: "sv uninstall <version>",
Action: baseCmd,
Aliases: []string{"ui"},
}, {
Name: "upgrade",
Usage: "upgrade sv",
UsageText: "sv upgrade",
Action: baseCmd,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "force upgrade",
Required: false,
},
},
},
}
app.Before = func(context *cli.Context) (err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
SVHome = filepath.Join(homeDir, ".sv")
SVRoot = filepath.Join(SVHome, "go")
SVBin = filepath.Join(SVHome, "bin")
SVDownload = filepath.Join(SVHome, "downloads")
SVCache = filepath.Join(SVHome, "cache")
if err = os.MkdirAll(SVDownload, 0755); err != nil {
return err
}
if err = os.MkdirAll(SVCache, 0755); err != nil {
return err
}
if err = os.MkdirAll(SVBin, 0755); err != nil {
return err
}
return
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func baseCmd(c *cli.Context) (err error) {
if err := runApp(c); err != nil {
return cli.Exit(err, 0)
}
return
}
func runApp(c *cli.Context) (err error) {
opts := &startOpts{
cmd: c.Command.Name,
target: c.Args().First(),
remote: c.Bool("remote"),
force: c.Bool("force"),
latest: c.String("latest"),
}
a := newApp(opts)
err = a.Start()
if err != nil {
return err
}
return
}