Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform: complete state resources #1629

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]...)
})
})
}