-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
138 lines (120 loc) · 2.93 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
package main
import (
"bytes"
"embed"
"fmt"
"os"
"strings"
"github.com/arsham/figurine/figurine"
"github.com/dotnetmentor/racoon/internal/command"
"github.com/dotnetmentor/racoon/internal/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const metadataExitCode string = "exitcode"
//go:embed ui/dist
var staticFiles embed.FS
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
app, ctx := createApp()
// run commands
exitCode := 0
err := app.Run(os.Args)
if err != nil {
ctx.Log.Error(err)
exitCode = 1
}
if app.Metadata[metadataExitCode] != nil {
switch metaExitCode := app.Metadata[metadataExitCode].(type) {
case int:
exitCode = metaExitCode
default:
exitCode = 128
}
}
os.Exit(exitCode)
}
func createApp() (*cli.App, config.AppContext) {
metadata := config.AppMetadata{
Version: version,
Commit: commit,
Date: date,
}
ctx, err := config.NewContext(metadata)
if err != nil {
ctx.Log.Error(err)
ctx.Log.Exit(1)
}
// configure cli app
cli.VersionPrinter = func(c *cli.Context) {
fmt.Printf("%s %s\ncommit = %s\ndate = %s\n", c.App.Name, c.App.Version, metadata.Commit, metadata.Date)
}
app := &cli.App{
Name: "racoon",
Usage: "configuration and secrets management",
Version: metadata.Version,
HideHelpCommand: true,
CommandNotFound: func(c *cli.Context, s string) {
ctx.Log.Warnf("unknown command %s", s)
c.App.Metadata[metadataExitCode] = 127
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "manifest",
Aliases: []string{"m"},
Usage: "path to the manifest file",
Value: "",
},
&cli.StringFlag{
Name: "loglevel",
Aliases: []string{"l"},
Usage: "sets the log level",
Value: "info",
},
},
Commands: []*cli.Command{
command.Export(metadata),
command.Read(metadata),
command.Write(metadata),
command.Config(metadata),
command.UI(metadata, staticFiles),
},
Before: func(c *cli.Context) error {
l := c.String("loglevel")
level, err := logrus.ParseLevel(l)
if err != nil {
return err
}
ctx.Log.SetLevel(level)
return nil
},
}
banner := newBanner("racoon", "https://github.com/dotnetmentor/racoon")
cli.AppHelpTemplate = banner + cli.AppHelpTemplate
cli.CommandHelpTemplate = banner + cli.CommandHelpTemplate
cli.SubcommandHelpTemplate = banner + cli.SubcommandHelpTemplate
for _, c := range app.Commands {
c.HideHelpCommand = true
}
return app, ctx
}
func newBanner(text, url string) string {
term := os.Getenv("TERM")
nocolor := os.Getenv("NO_COLOR")
if nocolor == "true" || term == "" || term == "dumb" {
return fmt.Sprintf("\n%s - %s\n\n", strings.ToUpper(text), url)
}
logo := rainbow(text)
return fmt.Sprintf("\n%s\n%66s\n\n", logo, url)
}
func rainbow(s string) string {
var buf bytes.Buffer
if err := figurine.Write(&buf, s, "Sub-Zero.flf"); err != nil {
return err.Error()
}
return buf.String()
}