-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (134 loc) · 3.3 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
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"os"
cli "github.com/joseluisq/cline"
)
// App version and build time values passed at compile time
// See `Makefile` > build
var (
version string = "devel"
buildTime string
buildCommit string
)
func main() {
app := cli.New()
app.Name = "enve"
app.Summary = "Run a program in a modified environment using .env files"
app.Version = version
app.BuildTime = buildTime
app.BuildCommit = buildCommit
app.Flags = []cli.Flag{
cli.FlagString{
Name: "file",
Summary: "Load environment variables from a file path.\nSome new line description\nAnother new line description.",
Value: ".env",
Aliases: []string{"f"},
},
cli.FlagInt{
Name: "int",
Summary: "Int value",
Value: 5,
Aliases: []string{"b", "z"},
},
cli.FlagBool{
Name: "verbose",
Summary: "Enable more verbose info",
Value: true,
Aliases: []string{"V"},
EnvVar: "ENV_VERBOSE",
},
}
app.Commands = []cli.Cmd{
{
Name: "info",
Summary: "Show command information",
Flags: []cli.Flag{
cli.FlagInt{
Name: "trace",
Summary: "Enable tracing mode",
Value: 10,
Aliases: []string{"t"},
},
cli.FlagBool{
Name: "detailed",
Summary: "Enable info details",
Value: true,
Aliases: []string{"d"},
},
cli.FlagString{
Name: "FF",
Value: ".env",
Aliases: []string{"f"},
},
cli.FlagStringSlice{
Name: "II",
Value: []string{"q", "r", "s"},
Aliases: []string{"i"},
},
},
Handler: func(ctx *cli.CmdContext) error {
fmt.Printf("Cmd `%s` executed!\n", ctx.Cmd.Name)
file, err := ctx.AppContext.Flags.String("file")
if err != nil {
return err
}
fmt.Printf("App Flag `file`: `%s`\n", file.Value())
verbose, err := ctx.AppContext.Flags.Bool("verbose")
if err != nil {
return err
}
b, _ := verbose.Value()
fmt.Printf("App Flag `verbose`: `%v`\n", b)
trace, err := ctx.AppContext.Flags.Int("trace")
if err != nil {
return err
}
i, err := trace.Value()
if err != nil {
return err
}
fmt.Printf("Cmd Flag `trace`: `%d` (%T)\n", i, i)
detailed, err := ctx.AppContext.Flags.Bool("detailed")
if err != nil {
return err
}
d, _ := detailed.Value()
fmt.Printf("Cmd Flag `detailed`: `%v` (%T)\n", d, d)
fmt.Printf("Cmd Tail arguments: %#v\n", ctx.TailArgs)
return nil
},
},
}
app.Handler = func(ctx *cli.AppContext) error {
fmt.Printf("App `%s` executed!\n", ctx.App.Name)
file, err := ctx.Flags.String("file")
if err != nil {
return err
}
b := file.Value()
if err != nil {
return err
}
fmt.Printf("App Flag `file`: `%s`\n", b)
fmt.Printf("App Flag `int`: `%v`\n", ctx.Flags.Any("int"))
verbose, err := ctx.Flags.Bool("verbose")
if err != nil {
return err
}
v, err := verbose.Value()
if err != nil {
return err
}
fmt.Printf("App Flag `verbose`: `%v`\n", v)
fmt.Printf("App Tail arguments: %#v\n", ctx.TailArgs)
fmt.Printf("App Provided flags: %v\n", ctx.Flags.GetProvided())
fmt.Printf("App Provided flags (long): %v\n", ctx.Flags.GetProvidedLong())
fmt.Printf("App Provided flags (short): %v\n", ctx.Flags.GetProvidedShort())
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}