Skip to content

Commit f731483

Browse files
authored
cli improvments (#678)
* cli improvments * change err msg * gen mocks * linter * linter
1 parent 82d1e16 commit f731483

File tree

10 files changed

+66
-18
lines changed

10 files changed

+66
-18
lines changed

cmd/command/stacks/stacks.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/AlecAivazis/survey/v2"
7+
"github.com/pluralsh/plural-cli/pkg/api"
78
"github.com/pluralsh/plural-cli/pkg/client"
89
"github.com/pluralsh/plural-cli/pkg/common"
910
"github.com/pluralsh/plural-cli/pkg/config"
@@ -81,13 +82,28 @@ func (p *Plural) handleGenerateBackend(_ *cli.Context) error {
8182
return err
8283
}
8384

84-
id := ""
85-
err := p.askStackID(&id)
85+
stackNames := make(map[string]string)
86+
infrastructureStacks, err := p.ConsoleClient.ListaStacks()
8687
if err != nil {
88+
return api.GetErrorResponse(err, "ListaStacks")
89+
}
90+
if infrastructureStacks == nil || infrastructureStacks.InfrastructureStacks == nil || len(infrastructureStacks.InfrastructureStacks.Edges) == 0 {
91+
return fmt.Errorf("returned objects list [ListStacks] is nil")
92+
}
93+
for _, node := range infrastructureStacks.InfrastructureStacks.Edges {
94+
stackNames[node.Node.Name] = lo.FromPtr(node.Node.ID)
95+
}
96+
var name string
97+
prompt := &survey.Select{
98+
Message: "Select a stack to generate a backend for:",
99+
Options: lo.Keys(stackNames),
100+
}
101+
opts := []survey.AskOpt{survey.WithValidator(survey.Required)}
102+
if err := survey.AskOne(prompt, &name, opts...); err != nil {
87103
return err
88104
}
89105

90-
stateUrls, err := stacks.GetTerraformStateUrls(p.ConsoleClient, id)
106+
stateUrls, err := stacks.GetTerraformStateUrls(p.ConsoleClient, stackNames[name])
91107
if err != nil {
92108
return err
93109
}
@@ -113,11 +129,3 @@ func (p *Plural) handleGenerateBackend(_ *cli.Context) error {
113129

114130
return git.AppendGitIgnore(dir, []string{fileName})
115131
}
116-
117-
func (p *Plural) askStackID(id *string) (err error) {
118-
return survey.AskOne(
119-
&survey.Input{Message: "Enter the stack id:"},
120-
id,
121-
survey.WithValidator(survey.Required),
122-
)
123-
}

hack/gen-client-mocks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cd $(dirname $0)/..
66

77
source hack/lib.sh
88

9-
CONTAINERIZE_IMAGE=golang:1.23.5 containerize ./hack/gen-client-mocks.sh
9+
CONTAINERIZE_IMAGE=golang:1.25.1 containerize ./hack/gen-client-mocks.sh
1010

1111
go run github.com/vektra/mockery/v2@latest --dir=pkg/api/ --name=Client --output=pkg/test/mocks
1212
go run github.com/vektra/mockery/v2@latest --dir=pkg/kubernetes --name=Kube --output=pkg/test/mocks

pkg/client/plural.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,12 @@ func (p *Plural) HandleInit(c *cli.Context) error {
156156
}
157157

158158
prov, err := common.RunPreflights(c)
159-
if err != nil && !c.Bool("ignore-preflights") {
160-
return err
159+
if err != nil {
160+
if !c.Bool("ignore-preflights") {
161+
fmt.Println("Preflight checks failed. You can rerun with --ignore-preflights to skip these checks.")
162+
return fmt.Errorf("preflight checks failed: %w", err)
163+
}
164+
fmt.Println("Preflight checks failed, but continuing because --ignore-preflights was specified.")
161165
}
162166

163167
if !git && common.Affirm("You're attempting to setup plural outside a git repository. Would you like us to set one up for you here?", "PLURAL_INIT_AFFIRM_SETUP_REPO") {

pkg/common/crypto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const (
1717

1818
const Gitattributes = `context.yaml filter=plural-crypt diff=plural-crypt
1919
workspace.yaml filter=plural-crypt diff=plural-crypt
20-
context.yaml* filter=plural-crypt diff=plural-crypt
2120
workspace.yaml* filter=plural-crypt diff=plural-crypt
2221
helm-values/*.yaml filter=plural-crypt diff=plural-crypt
2322
.env filter=plural-crypt diff=plural-crypt
@@ -34,6 +33,7 @@ const Gitignore = `/**/.terraform
3433
*.swo
3534
.DS_STORE
3635
.vscode
36+
context.yaml*
3737
`
3838

3939
func CryptoInit(c *cli.Context) error {

pkg/console/console.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type ConsoleClient interface {
6262
CreateClusterRegistration(attributes consoleclient.ClusterRegistrationCreateAttributes) (*consoleclient.ClusterRegistrationFragment, error)
6363
IsClusterRegistrationComplete(machineID string) (bool, *consoleclient.ClusterRegistrationFragment)
6464
GetUser(email string) (*consoleclient.UserFragment, error)
65+
ListaStacks() (*consoleclient.ListInfrastructureStacks, error)
6566
}
6667

6768
type authedTransport struct {

pkg/console/stacks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ import (
88
func (c *consoleClient) ListStackRuns(stackID string) (*gqlclient.ListStackRuns, error) {
99
return c.client.ListStackRuns(c.ctx, stackID, nil, nil, lo.ToPtr(int64(100)), nil)
1010
}
11+
12+
func (c *consoleClient) ListaStacks() (*gqlclient.ListInfrastructureStacks, error) {
13+
return c.client.ListInfrastructureStacks(c.ctx, nil, lo.ToPtr(int64(100)), nil, nil)
14+
}

pkg/test/mocks/Client.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/test/mocks/ConsoleClient.go

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/test/mocks/Kube.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/up/prune.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (ctx *Context) Prune() error {
4343

4444
_ = os.Remove("./terraform/mgmt/console.tf")
4545
_ = os.RemoveAll("./terraform/apps")
46+
_ = os.Remove("./context.yaml")
4647

4748
return git.Sync(repoRoot, "Post-setup resource cleanup", true)
4849
}

0 commit comments

Comments
 (0)