generated from jojomi/go-project-template-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_root.go
117 lines (101 loc) · 2.23 KB
/
env_root.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
package main
import (
"encoding/json"
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
// EnvRoot encapsulates the environment for the CLI root handler.
type EnvRoot struct {
JobDir string
Manual bool
SkipTimeCheck bool
DefaultYes bool
UnattendedOnly bool
AttendedOnly bool
PossibleOnly bool
Fixable bool
Shutdown bool
Interactive bool
SelectFirst bool
ShowUnfixable bool
JobFile string
DryRun bool
Verbose bool
}
// ParseFrom reads the state from a given cobra command and its args.
func (e *EnvRoot) ParseFrom(command *cobra.Command, _ []string) error {
var err error
jobDir, err := command.Flags().GetString("job-dir")
if err != nil {
return err
}
e.JobDir, err = homedir.Expand(jobDir)
if err != nil {
return err
}
e.Manual, err = command.Flags().GetBool("manual")
if err != nil {
return err
}
e.SelectFirst, err = command.Flags().GetBool("select-first")
if err != nil {
return err
}
e.DefaultYes, err = command.Flags().GetBool("default-yes")
if err != nil {
return err
}
e.SkipTimeCheck, err = command.Flags().GetBool("skip-time-check")
if err != nil {
return err
}
e.UnattendedOnly, err = command.Flags().GetBool("unattended-only")
if err != nil {
return err
}
e.AttendedOnly, err = command.Flags().GetBool("attended-only")
if err != nil {
return err
}
e.PossibleOnly, err = command.Flags().GetBool("possible-only")
if err != nil {
return err
}
e.Fixable, err = command.Flags().GetBool("fixable")
if err != nil {
return err
}
e.ShowUnfixable, err = command.Flags().GetBool("show-unfixable")
if err != nil {
return err
}
e.Shutdown, err = command.Flags().GetBool("shutdown")
if err != nil {
return err
}
e.Interactive, err = command.Flags().GetBool("interactive")
if err != nil {
return err
}
e.JobFile, err = command.Flags().GetString("job-file")
if err != nil {
return err
}
e.DryRun, err = command.Flags().GetBool("dry-run")
if err != nil {
return err
}
e.Verbose, err = command.Flags().GetBool("verbose")
if err != nil {
return err
}
return nil
}
func (e *EnvRoot) String() string {
out, err := json.Marshal(e)
if err != nil {
panic(err)
}
return fmt.Sprintf("EnvRoot %s", string(out))
}