Skip to content

Commit b4564ed

Browse files
Fallback to reinstall in cd clusters bootstrap command (#512)
It's kind of annoying having to know to either run `plural cd bootstrap` or `plural cd reinstall` etc. Trying to move it all into a functional single-command flow here.
1 parent c34318c commit b4564ed

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

cmd/plural/cd_clusters.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
gqlclient "github.com/pluralsh/console-client-go"
99
"github.com/pluralsh/plural-cli/pkg/cd"
1010
"github.com/pluralsh/plural-cli/pkg/console"
11+
"github.com/pluralsh/plural-cli/pkg/console/errors"
1112
"github.com/pluralsh/plural-cli/pkg/kubernetes/config"
1213
"github.com/pluralsh/plural-cli/pkg/utils"
1314
"github.com/pluralsh/polly/containers"
@@ -390,7 +391,12 @@ func (p *Plural) handleClusterReinstall(c *cli.Context) error {
390391
return err
391392
}
392393

393-
deployToken, err := p.ConsoleClient.GetDeployToken(getIdAndName(c.Args().Get(0)))
394+
id, name := getIdAndName(c.Args().Get(0))
395+
return p.reinstallOperator(c, id, name)
396+
}
397+
398+
func (p *Plural) reinstallOperator(c *cli.Context, id, handle *string) error {
399+
deployToken, err := p.ConsoleClient.GetDeployToken(id, handle)
394400
if err != nil {
395401
return err
396402
}
@@ -425,6 +431,14 @@ func (p *Plural) handleClusterBootstrap(c *cli.Context) error {
425431

426432
existing, err := p.ConsoleClient.CreateCluster(attrs)
427433
if err != nil {
434+
if errors.Like(err, "handle") && affirm("Do you want to reinstall the deployment operator?", "PLURAL_INSTALL_AGENT_CONFIRM_IF_EXISTS") {
435+
handle := lo.ToPtr(attrs.Name)
436+
if attrs.Handle != nil {
437+
handle = attrs.Handle
438+
}
439+
return p.reinstallOperator(c, nil, handle)
440+
}
441+
428442
return err
429443
}
430444

pkg/console/errors/like.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package errors
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
client "github.com/Yamashou/gqlgenc/clientv2"
8+
)
9+
10+
func Like(err error, msg string) bool {
11+
if err == nil {
12+
return false
13+
}
14+
15+
errorResponse := new(client.ErrorResponse)
16+
ok := errors.As(err, &errorResponse)
17+
if !ok {
18+
return false
19+
}
20+
21+
return isLike(errorResponse, msg)
22+
}
23+
24+
func isLike(err *client.ErrorResponse, msg string) bool {
25+
for _, g := range *err.GqlErrors {
26+
if strings.Contains(g.Message, msg) {
27+
return true
28+
}
29+
}
30+
31+
return false
32+
}

0 commit comments

Comments
 (0)