Skip to content

Commit

Permalink
Merge pull request #1629 from rsteube/terraform-state-resources
Browse files Browse the repository at this point in the history
terraform: complete state resources
  • Loading branch information
rsteube authored Apr 3, 2023
2 parents f8b371f + 28e6d5c commit 89645f2
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 51 deletions.
16 changes: 16 additions & 0 deletions completers/terraform_completer/cmd/action/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package action

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/terraform"
"github.com/spf13/cobra"
)

func ActionResources(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if f := cmd.Flag("state"); f != nil {
return terraform.ActionResources(f.Value.String())
}
return terraform.ActionResources("")
})
}
4 changes: 4 additions & 0 deletions completers/terraform_completer/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ func init() {
"state": carapace.ActionFiles(),
"state-out": carapace.ActionFiles(),
})

carapace.Gen(applyCmd).PositionalCompletion(
carapace.ActionFiles(),
)
}
14 changes: 5 additions & 9 deletions completers/terraform_completer/cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/terraform"
"github.com/rsteube/carapace-bin/completers/terraform_completer/cmd/action"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -46,14 +46,10 @@ func init() {
destroyCmd.Flag("var-file").NoOptDefVal = " "

carapace.Gen(destroyCmd).FlagCompletion(carapace.ActionMap{
"out": carapace.ActionFiles(),
"replace": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return terraform.ActionResources().Invoke(c).ToMultiPartsA(".")
}),
"state": carapace.ActionFiles(),
"target": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return terraform.ActionResources().Invoke(c).ToMultiPartsA(".")
}),
"out": carapace.ActionFiles(),
"replace": action.ActionResources(destroyCmd).MultiParts("."),
"state": carapace.ActionFiles(),
"target": action.ActionResources(destroyCmd).MultiParts("."),
"var-file": carapace.ActionFiles(),
})
}
2 changes: 1 addition & 1 deletion completers/terraform_completer/cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func init() {

carapace.Gen(graphCmd).FlagCompletion(carapace.ActionMap{
"plan": carapace.ActionFiles(),
"type": carapace.ActionValues("plan", "plan-destroy", "apply", "validate", "input", "refresh"),
"type": carapace.ActionValues("plan", "plan-refresh-only", "plan-destroy", "apply"),
})
}
14 changes: 5 additions & 9 deletions completers/terraform_completer/cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/terraform"
"github.com/rsteube/carapace-bin/completers/terraform_completer/cmd/action"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -44,14 +44,10 @@ func init() {
planCmd.Flag("var-file").NoOptDefVal = " "

carapace.Gen(planCmd).FlagCompletion(carapace.ActionMap{
"out": carapace.ActionFiles(),
"replace": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return terraform.ActionResources().Invoke(c).ToMultiPartsA(".")
}),
"state": carapace.ActionFiles(),
"target": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return terraform.ActionResources().Invoke(c).ToMultiPartsA(".")
}),
"out": carapace.ActionFiles(),
"replace": action.ActionResources(planCmd).MultiParts("."),
"state": carapace.ActionFiles(),
"target": action.ActionResources(planCmd).MultiParts("."),
"var-file": carapace.ActionFiles(),
})
}
6 changes: 2 additions & 4 deletions completers/terraform_completer/cmd/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/terraform"
"github.com/rsteube/carapace-bin/completers/terraform_completer/cmd/action"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -33,9 +33,7 @@ func init() {

// TODO complete var
carapace.Gen(refreshCmd).FlagCompletion(carapace.ActionMap{
"target": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return terraform.ActionResources().Invoke(c).ToMultiPartsA(".")
}),
"target": action.ActionResources(refreshCmd).MultiParts("."),
"var-file": carapace.ActionFiles(),
})
}
20 changes: 0 additions & 20 deletions completers/terraform_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"os"
"strings"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -20,23 +17,6 @@ func Execute() error {
return rootCmd.Execute()
}

func ActionExecute() carapace.Action { // TODO this still needed / correct? maybe use bridging action
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// TODO don't change os.Args
backup := os.Args
for index, arg := range c.Args {
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") {
c.Args[index] = "-" + arg
}
}
if strings.HasPrefix(c.CallbackValue, "-") && !strings.HasPrefix(c.CallbackValue, "--") {
c.CallbackValue = "-" + c.CallbackValue
}
os.Args = backup
return carapace.ActionExecute(rootCmd).Invoke(c).ToA()
})
}

func init() {
carapace.Gen(rootCmd).Standalone()

Expand Down
6 changes: 5 additions & 1 deletion completers/terraform_completer/cmd/state_mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/terraform_completer/cmd/action"
"github.com/spf13/cobra"
)

Expand All @@ -22,5 +23,8 @@ func init() {

state_mvCmd.Flag("lock-timeout").NoOptDefVal = " "

// TODO positional completion
carapace.Gen(state_mvCmd).PositionalCompletion(
action.ActionResources(state_mvCmd).MultiParts("."),
action.ActionResources(state_mvCmd).MultiParts("."),
)
}
7 changes: 6 additions & 1 deletion completers/terraform_completer/cmd/state_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/terraform_completer/cmd/action"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -31,5 +32,9 @@ func init() {
"state": carapace.ActionFiles(),
})

// TODO positional completion
carapace.Gen(state_rmCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return action.ActionResources(state_rmCmd).Invoke(c).Filter(c.Args).ToA().MultiParts(".")
}),
)
}
5 changes: 5 additions & 0 deletions completers/terraform_completer/cmd/state_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/terraform_completer/cmd/action"
"github.com/spf13/cobra"
)

Expand All @@ -21,4 +22,8 @@ func init() {
carapace.Gen(state_showCmd).FlagCompletion(carapace.ActionMap{
"state": carapace.ActionFiles(),
})

carapace.Gen(state_showCmd).PositionalCompletion(
action.ActionResources(state_showCmd).MultiParts("."),
)
}
4 changes: 2 additions & 2 deletions completers/terragrunt_completer/cmd/runAll.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
terraform "github.com/rsteube/carapace-bin/completers/terraform_completer/cmd"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/spf13/cobra"
)

Expand All @@ -20,6 +20,6 @@ func init() {
rootCmd.AddCommand(runAllCmd)

carapace.Gen(runAllCmd).PositionalAnyCompletion(
terraform.ActionExecute(),
bridge.ActionCarapaceBin("terraform"),
)
}
14 changes: 10 additions & 4 deletions pkg/actions/tools/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import (
)

// ActionResources completes resources
func ActionResources() carapace.Action {
return carapace.ActionExecCommand("terraform", "state", "list")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
func ActionResources(state string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{"state", "list"}
if state != "" {
args = append(args, "--state", state)
}
return carapace.ActionExecCommand("terraform", args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
})
})
}

0 comments on commit 89645f2

Please sign in to comment.