-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathshow.go
56 lines (47 loc) · 1.47 KB
/
show.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
package show
import (
"context"
"fmt"
"io"
"log"
"github.com/DavidGamba/go-getoptions"
)
var Logger = log.New(io.Discard, "show ", log.LstdFlags)
// NewCommand - Populate Options definition
func NewCommand(parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("show", "Show various types of objects")
opt.Bool("show-option", false)
opt.String("password", "", opt.GetEnv("PASSWORD"), opt.Alias("p"))
opt.SetCommandFn(Run)
return opt
}
// Run - Command entry point
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
showOption := opt.Value("show-option").(bool)
password := opt.Value("password").(string)
nopt := getoptions.New()
nopt.Bool("show-option", showOption, opt.SetCalled(opt.Called("show-option")))
nopt.String("password", password, opt.SetCalled(opt.Called("password")))
nopt.Int("number", 123, opt.SetCalled(true))
nopt.Float64("float", 3.14)
err := CommandFn(ctx, nopt, []string{})
if err != nil {
return err
}
return nil
}
func CommandFn(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Printf("args to show: %v\n", args)
fmt.Printf("show output... %v\n", args)
if opt.Called("show-option") {
fmt.Printf("show option was called...\n")
}
if opt.Called("password") {
fmt.Printf("The secret was... %s\n", opt.Value("password"))
}
if opt.Called("number") {
fmt.Printf("show number: %d\n", opt.Value("number"))
}
fmt.Printf("show float: %f\n", opt.Value("float"))
return nil
}