-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
186 lines (174 loc) · 4.47 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"os"
"github.com/axetroy/dvm/internal/command"
"github.com/axetroy/dvm/internal/dvm"
"github.com/fatih/color"
"github.com/pkg/errors"
cli "github.com/urfave/cli/v2"
)
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func init() {
dvm.SetVersion(version)
os.Setenv("DVM_VERSION", version)
os.Setenv("DVM_COMMIT", commit)
os.Setenv("DVM_DATE", date)
os.Setenv("DVM_BUILD_BY", builtBy)
}
func main() {
app := cli.NewApp()
app.Name = "dvm"
app.Usage = "version manager for Deno"
app.Version = fmt.Sprintf("%s %s %s", dvm.GetCurrentUsingVersion(), commit, date)
app.Authors = []*cli.Author{
{
Name: "Axetroy",
Email: "[email protected]",
},
}
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
{{if len .Authors}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}} {{ .ArgsUsage }}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Copyright }}
COPYRIGHT:
{{.Copyright}}
{{end}}{{if .Version}}
VERSION:
{{.Version}}
{{end}}
EXAMPLES:
{{.Name}} install v1.0.0
{{.Name}} use v1.0.0
{{.Name}} uninstall v1.0.0
{{.Name}} exec v1.0.0 https://deno.land/std/examples/welcome.ts
{{.Name}} ls
{{.Name}} ls-remote
SOURCE CODE:
https://github.com/axetroy/dvm
`
app.Commands = []*cli.Command{
{
Name: "current",
Usage: "Display currently activated version of Deno",
Action: func(c *cli.Context) error {
return command.Current()
},
},
{
Name: "ls",
Usage: "List installed versions",
Action: func(c *cli.Context) error {
return command.List()
},
},
{
Name: "ls-remote",
Usage: "List remote versions available for install",
Action: func(c *cli.Context) error {
return command.ListRemote()
},
},
{
Name: "install",
Usage: "Download and install specified Deno version",
ArgsUsage: "<version>",
Aliases: []string{"i"},
Action: func(c *cli.Context) error {
if c.Args().Len() == 0 {
return errors.New(fmt.Sprintf("require argument <%s>", "version"))
}
return command.Install(c.Args().First())
},
},
{
Name: "uninstall",
Usage: "Uninstall specified Deno version",
ArgsUsage: "<version1> <version2> ...",
Aliases: []string{"rm"},
Action: func(c *cli.Context) error {
if c.Args().Len() == 0 {
return errors.New(fmt.Sprintf("require argument <%s>", "version"))
}
return command.Uninstall(c.Args().Slice())
},
},
{
Name: "use",
Usage: "Use specified Deno version",
ArgsUsage: "<version>",
Action: func(c *cli.Context) error {
if c.Args().Len() == 0 {
return errors.New(fmt.Sprintf("require argument <%s>", "version"))
}
return command.Use(c.Args().First())
},
},
{
Name: "unused",
Usage: "Unused Deno",
Action: func(c *cli.Context) error {
return command.Unused()
},
},
{
Name: "exec",
Usage: "Run Deno command on <version>.",
ArgsUsage: "<version> <args>",
Action: func(c *cli.Context) error {
if c.Args().Len() == 0 {
return errors.New(fmt.Sprintf("require argument <%s>", "version"))
}
version := c.Args().First()
args := c.Args().Slice()[1:]
return command.Exec(version, args)
},
},
// here is the commands for dvm self
{
Name: "upgrade",
Usage: "Upgrade dvm",
ArgsUsage: "[version]",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "force", Aliases: []string{"f"}, Usage: "Force upgrade", Value: false},
},
Action: func(c *cli.Context) error {
return command.Upgrade(c.Args().First(), c.Bool("force"))
},
},
{
Name: "destroy",
Usage: "Uninstall dvm",
Action: func(c *cli.Context) error {
return command.Destroy()
},
},
}
// regardless of the result, the cache directory should be delete
if err := app.Run(os.Args); err != nil {
if os.Getenv("DEBUG") != "" {
fmt.Printf("%+v\n", err)
} else {
fmt.Println(err.Error())
fmt.Printf("run with environment variables %s to print more information\n", color.GreenString("DEBUG=1"))
}
_ = os.RemoveAll(dvm.CacheDir)
os.Exit(1)
} else {
_ = os.RemoveAll(dvm.CacheDir)
}
}