Skip to content

Commit

Permalink
Workspace run discard (#73)
Browse files Browse the repository at this point in the history
* 🗑️ Discard workspace run

* version 0.1.2

* Unhide json flag

---------

Co-authored-by: Julian Maze <[email protected]>
  • Loading branch information
julianmaze and julianmaze committed Oct 13, 2023
1 parent 366c0c2 commit 1afaf8c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 87 deletions.
120 changes: 36 additions & 84 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,39 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"cv",
"list",
"--workspaceName=tfx-test",
]
"args": ["cv", "list", "--workspaceName=tfx-test"]
},
{
"name": "TFX - Run Show",
"name": "TFX - Workspace Run Show",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"run",
"show",
"-i=run-tVxVKhhZeNn8GpZS",
]
"args": ["workspace", "run", "show", "-i=run-tVxVKhhZeNn8GpZS"]
},
{
"name": "TFX - Run List",
"name": "TFX - Workspace Run List",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"run",
"list",
"-w=tfx-test",
]
"args": ["workspace", "run", "list", "-w=tfx-test"]
},
{
"name": "TFX - Workspace Run Discard",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": ["workspace", "run", "discard", "-i=run-WH5WBvxJPLh1utN8"]
},
{
"name": "TFX - Workspace Run Show config",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": ["workspace", "run", "discard", "-h"]
},
{
"name": "TFX - Plan",
Expand All @@ -49,7 +53,7 @@
"args": [
"plan",
"-w=tfx-test-tfc",
"-d=./terraform",
"-d=./terraform"
// "--envs=a=b,c=d"
]
},
Expand All @@ -59,21 +63,15 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"apply",
"--runId=run-VCMv2kHVwS4RMVRu",
]
"args": ["apply", "--runId=run-VCMv2kHVwS4RMVRu"]
},
{
"name": "TFX - PMR List",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"pmr",
"list",
]
"args": ["pmr", "list"]
},
{
"name": "TFX - Module Download",
Expand Down Expand Up @@ -101,7 +99,7 @@
"version",
"-n=my-module",
"-p=aws",
"--moduleVersion=0.0.3",
"--moduleVersion=0.0.3"
]
},
{
Expand All @@ -110,86 +108,55 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"tfv",
"list",
"--enabled=false"
]
"args": ["tfv", "list", "--enabled=false"]
},
{
"name": "TFX - Test env",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"plan",
"test",
"--env", "a=14=33",
]
"args": ["plan", "test", "--env", "a=14=33"]
},
{
"name": "TFX - GPG Keys List",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"gpg",
"list",
"--tfeOrganization=terraform-tom"
]
"args": ["gpg", "list", "--tfeOrganization=terraform-tom"]
},
{
"name": "TFX - GPG Keys Delete",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"gpg",
"delete",
"-n=firefly",
"--keyId=34365D9472D7468F"
]
"args": ["gpg", "delete", "-n=firefly", "--keyId=34365D9472D7468F"]
},
{
"name": "TFX - GPG Keys Show",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"gpg",
"show",
"-n=firefly",
"--keyId=34365D9472D7468F"
]
"args": ["gpg", "show", "-n=firefly", "--keyId=34365D9472D7468F"]
},
{
"name": "TFX - TFE Download",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"release",
"tfe",
"download",
"-r=639"
]
"args": ["release", "tfe", "download", "-r=639"]
},
{
"name": "TFX - Replicated List",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"airgap",
"replicated",
"list"
]
"args": ["airgap", "replicated", "list"]
},
{
"name": "TFX - providers List",
Expand All @@ -210,12 +177,7 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"registry",
"provider",
"show",
"-n=random"
]
"args": ["registry", "provider", "show", "-n=random"]
},
{
"name": "TFX - providers version create",
Expand All @@ -241,12 +203,7 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"variable",
"list",
"-w=tfx-test",
"-o=json"
]
"args": ["variable", "list", "-w=tfx-test", "-o=json"]
},
{
"name": "TFX - variable create",
Expand All @@ -269,12 +226,7 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}/../main.go",
"args": [
"variable",
"show",
"-w=tfx-test",
"-k=variable3"
]
"args": ["variable", "show", "-w=tfx-test", "-k=variable3"]
}
]
}
}
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ func init() {
rootCmd.PersistentFlags().String("tfeOrganization", "", "The name of the TFx Organization. Can also be set with the environment variable TFE_ORGANIZATION.")
rootCmd.PersistentFlags().String("tfeToken", "", "The API token used to authenticate to TFx. Can also be set with the environment variable TFE_TOKEN.")

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

// required
rootCmd.MarkPersistentFlagRequired("tfeOrganization")
Expand Down
30 changes: 30 additions & 0 deletions cmd/workspace_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ var (
*viperString("id"))
},
}

// `tfx workspace discard` command
runDiscardCmd = &cobra.Command{
Use: "discard",
Short: "Discard Run",
Long: "Discard Run for a TFx Workspace.",
RunE: func(cmd *cobra.Command, args []string) error {
return runDiscard(
getTfxClientContext(),
*viperString("id"))
},
}
)

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

// `tfx workspace run discard` command
runDiscardCmd.Flags().StringP("id", "i", "", "Run Id (i.e. run-*)")
runDiscardCmd.MarkFlagRequired("id")

workspaceCmd.AddCommand(runCmd)
runCmd.AddCommand(runListCmd)
runCmd.AddCommand(runCreateCmd)
runCmd.AddCommand(runShowCmd)
runCmd.AddCommand(runDiscardCmd)
}

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

func runDiscard(c TfxClientContext, runId string) error {
err := c.Client.Runs.Discard(c.Context, runId, tfe.RunDiscardOptions{
Comment: tfe.String("Discarded by tfx"),
})
if err != nil {
return errors.Wrap(err, "failed to discard run")
}

o.AddDeferredMessageRead("Discarded run id", runId)

return nil
}

func getWorkspaceId(c TfxClientContext, workspaceName string) (string, error) {
w, err := c.Client.Workspaces.Read(c.Context, c.OrganizationName, workspaceName)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package version

var (
Version = "0.1.1"
Version = "0.1.2"
Prerelease = ""
Build = ""
Date = ""
Expand Down

0 comments on commit 1afaf8c

Please sign in to comment.