-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
53 lines (45 loc) · 1.17 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
package main
import (
"context"
"fmt"
"netrc/commands"
"os"
"github.com/josegonzalez/cli-skeleton/command"
"github.com/mitchellh/cli"
)
var AppName = "netrc"
var Version string
func main() {
os.Exit(Run(os.Args[1:]))
}
// Executes the specified command
func Run(args []string) int {
ctx := context.Background()
commandMeta := command.SetupRun(ctx, AppName, Version, args)
c := cli.NewCLI(AppName, Version)
c.Args = os.Args[1:]
c.Commands = command.Commands(ctx, commandMeta, Commands)
exitCode, err := c.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
return 1
}
return exitCode
}
// Returns a list of implemented commands
func Commands(ctx context.Context, meta command.Meta) map[string]cli.CommandFactory {
return map[string]cli.CommandFactory{
"set": func() (cli.Command, error) {
return &commands.SetCommand{Meta: meta}, nil
},
"get": func() (cli.Command, error) {
return &commands.GetCommand{Meta: meta}, nil
},
"unset": func() (cli.Command, error) {
return &commands.UnsetCommand{Meta: meta}, nil
},
"version": func() (cli.Command, error) {
return &command.VersionCommand{Meta: meta}, nil
},
}
}