-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
46 lines (39 loc) · 1.04 KB
/
cli.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
package tfclean
import (
"context"
"fmt"
"github.com/alecthomas/kong"
)
var Version = "dev"
var Revision = "HEAD"
type GlobalOptions struct {
}
type CLI struct {
Tfstate string `help:"Terraform state file"`
Dir string `arg:"" required:"" help:"Directory to clean"`
Version VersionFlag `name:"version" help:"show version"`
}
type VersionFlag string
func (v VersionFlag) Decode(ctx *kong.DecodeContext) error { return nil }
func (v VersionFlag) IsBool() bool { return true }
func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
fmt.Printf("%s-%s\n", Version, Revision)
app.Exit(0)
return nil
}
func RunCLI(ctx context.Context, args []string) error {
cli := CLI{
Version: VersionFlag("0.1.0"),
}
parser, err := kong.New(&cli)
if err != nil {
return fmt.Errorf("error creating CLI parser: %w", err)
}
_, err = parser.Parse(args)
if err != nil {
fmt.Printf("error parsing CLI: %v\n", err)
return fmt.Errorf("error parsing CLI: %w", err)
}
app := New(&cli)
return app.Run(ctx)
}