-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (69 loc) · 1.7 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
package main
import (
"fmt"
"os"
"github.com/axetroy/dvs/internal/command"
"github.com/axetroy/dvs/internal/dir"
"github.com/axetroy/dvs/internal/version"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func main() {
app := cli.NewApp()
app.Name = "dvs"
app.Usage = "Docker-based Virtual System"
app.Version = version.GetCurrentUsingVersion()
app.Authors = []*cli.Author{
{
Name: "Axetroy",
Email: "axetroy.dev@gmail.com",
},
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "image",
Aliases: []string{"i"},
Usage: "Specifying the running image. eg. ubuntu:19.10",
Value: "alpine:3.11", // default image
},
}
app.Commands = []*cli.Command{
{
Name: "run",
Usage: "Run command in container",
Action: func(c *cli.Context) error {
return command.Run(c.Args().Slice(), &command.RunOption{
Image: c.String("image"),
})
},
},
{
Name: "upgrade",
Usage: "Upgrade dvs",
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"))
},
},
}
app.Action = func(c *cli.Context) error {
return command.Repl(&command.ReplOption{
Image: c.String("image"),
})
}
// 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(dir.CacheDir)
os.Exit(1)
} else {
_ = os.RemoveAll(dir.CacheDir)
}
}