-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (72 loc) · 1.95 KB
/
Copy pathmain.go
File metadata and controls
92 lines (72 loc) · 1.95 KB
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
package main
import (
"context"
"os"
"github.com/urfave/cli/v3"
"os/signal"
"syscall"
microcli "github.com/vanroy/microcli/impl"
"github.com/vanroy/microcli/impl/config"
"github.com/vanroy/microcli/impl/git"
pmt "github.com/vanroy/microcli/impl/prompt"
)
func main() {
handleSignals()
conf, err := loadConf(context.Background())
if err != nil {
pmt.PrintErrorf("Command load config '%s'", err.Error())
os.Exit(1)
}
app := &cli.Command{}
app.Name = "mbx"
app.Usage = "This script provides utilities to manage microservices git repositories."
app.Version = "1.1.0"
app.Before = initContext
app.CommandNotFound = commandNotFound
app.Commands = microcli.InitCommands(*conf)
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "quiet",
Aliases: []string{"q"},
Usage: "Disable verbose output",
},
&cli.BoolFlag{
Name: "non-interactive",
Aliases: []string{"n"},
Usage: "Non interactive mode",
},
}
app.Run(context.Background(), os.Args) //nolint:errcheck
}
// Load or init configuration
func loadConf(context context.Context) (*config.Config, error) {
if exist, err := config.Exist(); !exist && err == nil {
microcli.ShowBanner()
pmt.PrintWarn("Settings not exist, starting initialization\n")
git.Init(context, nil) //nolint:errcheck
pmt.PrintInfo("Settings initialized, enjoy !")
os.Exit(0)
}
return config.Load()
}
// Init context before commands
func initContext(ctx context.Context, c *cli.Command) (context.Context, error) {
config.Options = config.GlobalOptions{
Verbose: !c.Bool("quiet"),
Interactive: !c.Bool("non-interactive"),
}
microcli.ShowBanner()
return ctx, nil
}
// Display command not found
func commandNotFound(ctx context.Context, c *cli.Command, cmd string) {
pmt.PrintErrorf("Command not exist '%s'", cmd)
}
func handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-c
os.Exit(0)
}()
}