-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable.go
82 lines (71 loc) · 1.57 KB
/
enable.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
package cli
import (
"fmt"
"os"
"github.com/tennashi/got"
"github.com/urfave/cli/v2"
)
func NewEnableCommand() *cli.Command {
return &cli.Command{
Name: "enable",
Usage: "enable the specified executable",
Flags: []cli.Flag{
&cli.PathFlag{
Name: "bindir",
Usage: "bin directory",
Aliases: []string{"b"},
EnvVars: []string{"GOT_BIN_DIR"},
},
&cli.PathFlag{
Name: "datadir",
Usage: "data directory",
Aliases: []string{"d"},
EnvVars: []string{"GOT_DATA_DIR"},
},
},
Before: func(c *cli.Context) error {
cf, err := NewConfigFile(c.Path("config"))
if err != nil {
if !os.IsNotExist(err) {
return err
}
fmt.Fprintln(c.App.ErrWriter, "Load the default config")
}
if !c.IsSet("bindir") {
if err := c.Set("bindir", cf.BinDir); err != nil {
return err
}
}
if !c.IsSet("datadir") {
if err := c.Set("datadir", cf.DataDir); err != nil {
return err
}
}
if c.NArg() != 2 {
return &got.InvalidParamError{
Message: "package name and executable name is required",
}
}
return nil
},
Action: func(c *cli.Context) error {
cfg := &got.EnableCommandConfig{
DataDir: c.Path("datadir"),
BinDir: c.Path("bindir"),
IsDebug: c.Bool("debug"),
}
ioStream := &got.IOStream{
In: c.App.Reader,
Out: c.App.Writer,
Err: c.App.ErrWriter,
}
cmd, err := got.NewEnableCommand(ioStream, cfg)
if err != nil {
return err
}
pkgName := c.Args().Get(0)
execName := c.Args().Get(1)
return cmd.Run(pkgName, execName)
},
}
}