-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
96 lines (76 loc) · 2.32 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
package main
import (
"log"
"os"
"strings"
"github.com/DATA-DOG/godog/colors"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/tomatool/tomato/config"
"github.com/tomatool/tomato/tomato"
)
// AppHelpTemplate is the text template for the Default help topic.
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
const AppHelpTemplate = `Usage: {{if .UsageText}}{{.UsageText}}{{else}}tomato {{if .VisibleFlags}}[options]{{end}}{{if .ArgsUsage}}{{.ArgsUsage}}{{else}} <config path>{{end}}{{end}}
Options:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}
`
func main() {
cli.AppHelpTemplate = AppHelpTemplate
log := log.New(os.Stdout, "", 0)
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "env.file, e",
Usage: "environment variable file path",
},
cli.StringFlag{
Name: "features.path, f",
Usage: "features directory/file path (comma separated for multi path)",
},
cli.StringFlag{
Name: "config.file, c",
Usage: "[DEPRECATED PLEASE USE ARGUMENT] configuration file path",
Hidden: true,
},
}
app.Before = func(ctx *cli.Context) error {
if envFile := ctx.String("env.file"); envFile != "" {
return godotenv.Load(envFile)
}
return nil
}
app.Action = func(ctx *cli.Context) error {
var configPath string
// backward compability
if c := ctx.String("config.file"); c != "" {
log.Printf(colors.Bold(colors.Yellow)("Flag --config.file, -c is deprecated, please use args instead. For additional help try 'tomato -help'"))
configPath = c
}
if len(ctx.Args()) == 1 {
configPath = ctx.Args()[0]
}
if configPath == "" {
return errors.New("This command takes one argument: <config path>\nFor additional help try 'tomato -help'")
}
conf, err := config.Retrieve(configPath)
if err != nil {
return errors.Wrap(err, "Failed to retrieve config")
}
if featuresPath := ctx.String("features.path"); featuresPath != "" {
conf.FeaturesPaths = strings.Split(featuresPath, ",")
}
t := tomato.New(conf, log)
if err := t.Verify(); err != nil {
return errors.Wrap(err, "Verification failed")
}
return t.Run()
}
if err := app.Run(os.Args); err != nil {
log.Printf("%v", colors.Bold(colors.Red)(err))
os.Exit(1)
}
}