Skip to content

Commit 1afaf8c

Browse files
authored
Workspace run discard (#73)
* 🗑️ Discard workspace run * version 0.1.2 * Unhide json flag --------- Co-authored-by: Julian Maze <julian.maze@viasat.com>
1 parent 366c0c2 commit 1afaf8c

File tree

4 files changed

+68
-87
lines changed

4 files changed

+68
-87
lines changed

.vscode/launch.json

Lines changed: 36 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,39 @@
1010
"request": "launch",
1111
"mode": "auto",
1212
"program": "${fileDirname}/../main.go",
13-
"args": [
14-
"cv",
15-
"list",
16-
"--workspaceName=tfx-test",
17-
]
13+
"args": ["cv", "list", "--workspaceName=tfx-test"]
1814
},
1915
{
20-
"name": "TFX - Run Show",
16+
"name": "TFX - Workspace Run Show",
2117
"type": "go",
2218
"request": "launch",
2319
"mode": "auto",
2420
"program": "${fileDirname}/../main.go",
25-
"args": [
26-
"run",
27-
"show",
28-
"-i=run-tVxVKhhZeNn8GpZS",
29-
]
21+
"args": ["workspace", "run", "show", "-i=run-tVxVKhhZeNn8GpZS"]
3022
},
3123
{
32-
"name": "TFX - Run List",
24+
"name": "TFX - Workspace Run List",
3325
"type": "go",
3426
"request": "launch",
3527
"mode": "auto",
3628
"program": "${fileDirname}/../main.go",
37-
"args": [
38-
"run",
39-
"list",
40-
"-w=tfx-test",
41-
]
29+
"args": ["workspace", "run", "list", "-w=tfx-test"]
30+
},
31+
{
32+
"name": "TFX - Workspace Run Discard",
33+
"type": "go",
34+
"request": "launch",
35+
"mode": "auto",
36+
"program": "${fileDirname}/../main.go",
37+
"args": ["workspace", "run", "discard", "-i=run-WH5WBvxJPLh1utN8"]
38+
},
39+
{
40+
"name": "TFX - Workspace Run Show config",
41+
"type": "go",
42+
"request": "launch",
43+
"mode": "auto",
44+
"program": "${fileDirname}/../main.go",
45+
"args": ["workspace", "run", "discard", "-h"]
4246
},
4347
{
4448
"name": "TFX - Plan",
@@ -49,7 +53,7 @@
4953
"args": [
5054
"plan",
5155
"-w=tfx-test-tfc",
52-
"-d=./terraform",
56+
"-d=./terraform"
5357
// "--envs=a=b,c=d"
5458
]
5559
},
@@ -59,21 +63,15 @@
5963
"request": "launch",
6064
"mode": "auto",
6165
"program": "${fileDirname}/../main.go",
62-
"args": [
63-
"apply",
64-
"--runId=run-VCMv2kHVwS4RMVRu",
65-
]
66+
"args": ["apply", "--runId=run-VCMv2kHVwS4RMVRu"]
6667
},
6768
{
6869
"name": "TFX - PMR List",
6970
"type": "go",
7071
"request": "launch",
7172
"mode": "auto",
7273
"program": "${fileDirname}/../main.go",
73-
"args": [
74-
"pmr",
75-
"list",
76-
]
74+
"args": ["pmr", "list"]
7775
},
7876
{
7977
"name": "TFX - Module Download",
@@ -101,7 +99,7 @@
10199
"version",
102100
"-n=my-module",
103101
"-p=aws",
104-
"--moduleVersion=0.0.3",
102+
"--moduleVersion=0.0.3"
105103
]
106104
},
107105
{
@@ -110,86 +108,55 @@
110108
"request": "launch",
111109
"mode": "auto",
112110
"program": "${fileDirname}/../main.go",
113-
"args": [
114-
"tfv",
115-
"list",
116-
"--enabled=false"
117-
]
111+
"args": ["tfv", "list", "--enabled=false"]
118112
},
119113
{
120114
"name": "TFX - Test env",
121115
"type": "go",
122116
"request": "launch",
123117
"mode": "auto",
124118
"program": "${fileDirname}/../main.go",
125-
"args": [
126-
"plan",
127-
"test",
128-
"--env", "a=14=33",
129-
]
119+
"args": ["plan", "test", "--env", "a=14=33"]
130120
},
131121
{
132122
"name": "TFX - GPG Keys List",
133123
"type": "go",
134124
"request": "launch",
135125
"mode": "auto",
136126
"program": "${fileDirname}/../main.go",
137-
"args": [
138-
"gpg",
139-
"list",
140-
"--tfeOrganization=terraform-tom"
141-
]
127+
"args": ["gpg", "list", "--tfeOrganization=terraform-tom"]
142128
},
143129
{
144130
"name": "TFX - GPG Keys Delete",
145131
"type": "go",
146132
"request": "launch",
147133
"mode": "auto",
148134
"program": "${fileDirname}/../main.go",
149-
"args": [
150-
"gpg",
151-
"delete",
152-
"-n=firefly",
153-
"--keyId=34365D9472D7468F"
154-
]
135+
"args": ["gpg", "delete", "-n=firefly", "--keyId=34365D9472D7468F"]
155136
},
156137
{
157138
"name": "TFX - GPG Keys Show",
158139
"type": "go",
159140
"request": "launch",
160141
"mode": "auto",
161142
"program": "${fileDirname}/../main.go",
162-
"args": [
163-
"gpg",
164-
"show",
165-
"-n=firefly",
166-
"--keyId=34365D9472D7468F"
167-
]
143+
"args": ["gpg", "show", "-n=firefly", "--keyId=34365D9472D7468F"]
168144
},
169145
{
170146
"name": "TFX - TFE Download",
171147
"type": "go",
172148
"request": "launch",
173149
"mode": "auto",
174150
"program": "${fileDirname}/../main.go",
175-
"args": [
176-
"release",
177-
"tfe",
178-
"download",
179-
"-r=639"
180-
]
151+
"args": ["release", "tfe", "download", "-r=639"]
181152
},
182153
{
183154
"name": "TFX - Replicated List",
184155
"type": "go",
185156
"request": "launch",
186157
"mode": "auto",
187158
"program": "${fileDirname}/../main.go",
188-
"args": [
189-
"airgap",
190-
"replicated",
191-
"list"
192-
]
159+
"args": ["airgap", "replicated", "list"]
193160
},
194161
{
195162
"name": "TFX - providers List",
@@ -210,12 +177,7 @@
210177
"request": "launch",
211178
"mode": "auto",
212179
"program": "${fileDirname}/../main.go",
213-
"args": [
214-
"registry",
215-
"provider",
216-
"show",
217-
"-n=random"
218-
]
180+
"args": ["registry", "provider", "show", "-n=random"]
219181
},
220182
{
221183
"name": "TFX - providers version create",
@@ -241,12 +203,7 @@
241203
"request": "launch",
242204
"mode": "auto",
243205
"program": "${fileDirname}/../main.go",
244-
"args": [
245-
"variable",
246-
"list",
247-
"-w=tfx-test",
248-
"-o=json"
249-
]
206+
"args": ["variable", "list", "-w=tfx-test", "-o=json"]
250207
},
251208
{
252209
"name": "TFX - variable create",
@@ -269,12 +226,7 @@
269226
"request": "launch",
270227
"mode": "auto",
271228
"program": "${fileDirname}/../main.go",
272-
"args": [
273-
"variable",
274-
"show",
275-
"-w=tfx-test",
276-
"-k=variable3"
277-
]
229+
"args": ["variable", "show", "-w=tfx-test", "-k=variable3"]
278230
}
279231
]
280-
}
232+
}

cmd/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ func init() {
7979
rootCmd.PersistentFlags().String("tfeOrganization", "", "The name of the TFx Organization. Can also be set with the environment variable TFE_ORGANIZATION.")
8080
rootCmd.PersistentFlags().String("tfeToken", "", "The API token used to authenticate to TFx. Can also be set with the environment variable TFE_TOKEN.")
8181

82-
// Add json output option, but hide during development
82+
// Add json output option
8383
rootCmd.PersistentFlags().BoolP("json", "j", false, "Will output command results as JSON.")
84-
rootCmd.PersistentFlags().MarkHidden("json")
8584

8685
// required
8786
rootCmd.MarkPersistentFlagRequired("tfeOrganization")

cmd/workspace_run.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ var (
7474
*viperString("id"))
7575
},
7676
}
77+
78+
// `tfx workspace discard` command
79+
runDiscardCmd = &cobra.Command{
80+
Use: "discard",
81+
Short: "Discard Run",
82+
Long: "Discard Run for a TFx Workspace.",
83+
RunE: func(cmd *cobra.Command, args []string) error {
84+
return runDiscard(
85+
getTfxClientContext(),
86+
*viperString("id"))
87+
},
88+
}
7789
)
7890

7991
func init() {
@@ -95,10 +107,15 @@ func init() {
95107
runShowCmd.Flags().StringP("id", "i", "", "Run Id (i.e. run-*)")
96108
runShowCmd.MarkFlagRequired("id")
97109

110+
// `tfx workspace run discard` command
111+
runDiscardCmd.Flags().StringP("id", "i", "", "Run Id (i.e. run-*)")
112+
runDiscardCmd.MarkFlagRequired("id")
113+
98114
workspaceCmd.AddCommand(runCmd)
99115
runCmd.AddCommand(runListCmd)
100116
runCmd.AddCommand(runCreateCmd)
101117
runCmd.AddCommand(runShowCmd)
118+
runCmd.AddCommand(runDiscardCmd)
102119
}
103120

104121
func workspaceRunListAll(c TfxClientContext, workspaceId string, maxItems int) ([]*tfe.Run, error) {
@@ -211,6 +228,19 @@ func runShow(c TfxClientContext, runId string) error {
211228
return nil
212229
}
213230

231+
func runDiscard(c TfxClientContext, runId string) error {
232+
err := c.Client.Runs.Discard(c.Context, runId, tfe.RunDiscardOptions{
233+
Comment: tfe.String("Discarded by tfx"),
234+
})
235+
if err != nil {
236+
return errors.Wrap(err, "failed to discard run")
237+
}
238+
239+
o.AddDeferredMessageRead("Discarded run id", runId)
240+
241+
return nil
242+
}
243+
214244
func getWorkspaceId(c TfxClientContext, workspaceName string) (string, error) {
215245
w, err := c.Client.Workspaces.Read(c.Context, c.OrganizationName, workspaceName)
216246
if err != nil {

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package version
22

33
var (
4-
Version = "0.1.1"
4+
Version = "0.1.2"
55
Prerelease = ""
66
Build = ""
77
Date = ""

0 commit comments

Comments
 (0)