/
cli.go
170 lines (161 loc) · 4.29 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cli
import (
"fmt"
"os"
"time"
"github.com/urfave/cli/v2"
"github.com/mvisonneau/tfcw/internal/cmd"
)
// Run handles the instanciation of the CLI application
func Run(version string, args []string) {
err := NewApp(version, time.Now()).Run(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// NewApp configures the CLI application
func NewApp(version string, start time.Time) (app *cli.App) {
app = cli.NewApp()
app.Name = "tfcw"
app.Version = version
app.Usage = "Terraform Cloud Wrapper"
app.EnableBashCompletion = true
app.Flags = cli.FlagsByName{
&cli.StringFlag{
Name: "address",
Aliases: []string{"a"},
EnvVars: []string{"TFCW_ADDRESS"},
Usage: "`address` to access Terraform Cloud API",
},
&cli.StringFlag{
Name: "config-file",
Aliases: []string{"c"},
EnvVars: []string{"TFCW_CONFIG_FILE"},
Usage: "`path` of a readable TFCW configuration file (.hcl or .json)",
Value: "<working-dir>/tfcw.hcl",
},
&cli.StringFlag{
Name: "log-level",
EnvVars: []string{"TFCW_LOG_LEVEL"},
Usage: "log `level` (debug,info,warn,fatal,panic)",
Value: "info",
},
&cli.StringFlag{
Name: "log-format",
EnvVars: []string{"TFCW_LOG_FORMAT"},
Usage: "log `format` (json,text)",
Value: "text",
},
&cli.StringFlag{
Name: "organization",
Aliases: []string{"o"},
EnvVars: []string{"TFCW_ORGANIZATION"},
Usage: "`organization` to use on Terraform Cloud API",
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
EnvVars: []string{"TFCW_TOKEN"},
Usage: "`token` to access Terraform Cloud API",
},
&cli.StringFlag{
Name: "working-dir",
Aliases: []string{"d"},
EnvVars: []string{"TFCW_WORKING_DIR"},
Usage: "`path` of the directory containing your Terraform files",
Value: ".",
},
&cli.StringFlag{
Name: "workspace",
Aliases: []string{"w"},
EnvVars: []string{"TFCW_WORKSPACE"},
Usage: "`workspace` to use on Terraform Cloud API",
},
}
app.Commands = cli.CommandsByName{
{
Name: "render",
Usage: "render variables values",
Action: cmd.ExecWrapper(cmd.Render),
Flags: cli.FlagsByName{renderType, ignoreTTLs, dryRun},
},
{
Name: "run",
Usage: "manipulate runs",
Subcommands: cli.Commands{
{
Name: "approve",
Usage: "approve a run given its 'ID'",
Action: cmd.ExecWrapper(cmd.RunApprove),
Flags: cli.FlagsByName{currentRun, message},
},
{
Name: "create",
Usage: "create a run on TFC",
Action: cmd.ExecWrapper(cmd.RunCreate),
Flags: append(runCreate, message, renderType, ignoreTTLs),
},
{
Name: "discard",
Usage: "discard a run given its 'ID'",
Action: cmd.ExecWrapper(cmd.RunDiscard),
Flags: cli.FlagsByName{currentRun, message},
},
{
Name: "current-id",
Usage: "return the id of the current run",
Action: cmd.ExecWrapper(cmd.RunCurrentID),
},
},
},
{
Name: "workspace",
Aliases: []string{"ws"},
Usage: "manipulate the workspace",
Subcommands: cli.Commands{
{
Name: "configure",
Aliases: []string{"cfg"},
Usage: "apply defined configuration to the workspace",
Action: cmd.ExecWrapper(cmd.WorkspaceConfigure),
Flags: cli.FlagsByName{dryRun},
},
{
Name: "delete-variables",
Usage: "remove configured workspace variables (default: scoped to variables defined in the config file)",
Action: cmd.ExecWrapper(cmd.WorkspaceDeleteVariables),
Flags: cli.FlagsByName{&cli.BoolFlag{
Name: "all, a",
Usage: "delete all variables",
}},
},
{
Name: "status",
Usage: "return the status of the workspace",
Action: cmd.ExecWrapper(cmd.WorkspaceStatus),
},
{
Name: "operations",
Usage: "manages the operations value of the workspace",
Subcommands: cli.Commands{
{
Name: "enable",
Usage: "enable remote operations on the workspace",
Action: cmd.ExecWrapper(cmd.WorkspaceEnableOperations),
},
{
Name: "disable",
Usage: "disable remote operations on the workspace",
Action: cmd.ExecWrapper(cmd.WorkspaceDisableOperations),
},
},
},
},
},
}
app.Metadata = map[string]interface{}{
"startTime": start,
}
return
}