diff --git a/cmd/command/cd/cd.go b/cmd/command/cd/cd.go index d55226ccd..2e36a6b74 100644 --- a/cmd/command/cd/cd.go +++ b/cmd/command/cd/cd.go @@ -31,6 +31,10 @@ type Plural struct { HelmConfiguration *action.Configuration } +func SetConsoleURL(url string) { + consoleURL = url +} + func Command(clients client.Plural, helmConfiguration *action.Configuration) cli.Command { return cli.Command{ Name: "deployments", @@ -234,7 +238,10 @@ func (p *Plural) HandleCdLogin(c *cli.Context) (err error) { } } - url := c.String("url") + url := consoleURL + if url == "" { + url = c.String("url") + } if url == "" { url, err = utils.ReadLine("Enter the url of your console: ") if err != nil { diff --git a/cmd/command/up/up.go b/cmd/command/up/up.go index 6ac608bed..1400aa5c5 100644 --- a/cmd/command/up/up.go +++ b/cmd/command/up/up.go @@ -6,8 +6,10 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/pluralsh/plural-cli/cmd/command/cd" + cdpkg "github.com/pluralsh/plural-cli/cmd/command/cd" "github.com/pluralsh/plural-cli/pkg/client" "github.com/pluralsh/plural-cli/pkg/common" + "github.com/pluralsh/plural-cli/pkg/provider" "github.com/pluralsh/plural-cli/pkg/up" "github.com/pluralsh/plural-cli/pkg/utils" "github.com/pluralsh/plural-cli/pkg/utils/git" @@ -54,14 +56,24 @@ func Command(clients client.Plural) cli.Command { func (p *Plural) handleUp(c *cli.Context) error { // provider.IgnoreProviders([]string{"GENERIC", "KIND"}) - if err := p.HandleInit(c); err != nil { + if err := common.HandleLogin(c); err != nil { return err } p.InitPluralClient() - cd := &cd.Plural{Plural: p.Plural} + cd := &cdpkg.Plural{Plural: p.Plural} + + var name, url string + var err error if c.Bool("cloud") { + name, url, err = p.choseCluster() + if err != nil { + return err + } + + cdpkg.SetConsoleURL(url) + provider.SetClusterFlag(name) if err := cd.HandleCdLogin(c); err != nil { return err } @@ -71,6 +83,10 @@ func (p *Plural) handleUp(c *cli.Context) error { } } + if err := p.HandleInit(c); err != nil { + return err + } + repoRoot, err := git.Root() if err != nil { return err @@ -82,10 +98,11 @@ func (p *Plural) handleUp(c *cli.Context) error { } if c.Bool("cloud") { - id, name, err := getCluster(cd) + id, err := getCluster(cd) if err != nil { return err } + ctx.ImportCluster = lo.ToPtr(id) ctx.CloudCluster = name } @@ -120,21 +137,18 @@ func (p *Plural) handleUp(c *cli.Context) error { return nil } -func getCluster(cd *cd.Plural) (id string, name string, err error) { - if cd == nil { - return "", "", fmt.Errorf("your CLI is not logged into Plural, try running `plural login` to generate local credentials") - } - clusters, err := cd.ListClusters() +func (p *Plural) choseCluster() (name, url string, err error) { + instances, err := p.GetConsoleInstances() if err != nil { - return "", "", err + return } clusterNames := []string{} clusterMap := map[string]string{} - for _, cluster := range clusters { - clusterNames = append(clusterNames, cluster.Node.Name) - clusterMap[cluster.Node.Name] = cluster.Node.ID + for _, cluster := range instances { + clusterNames = append(clusterNames, cluster.Name) + clusterMap[cluster.Name] = cluster.URL } prompt := &survey.Select{ @@ -144,6 +158,27 @@ func getCluster(cd *cd.Plural) (id string, name string, err error) { if err = survey.AskOne(prompt, &name, survey.WithValidator(survey.Required)); err != nil { return } - id = clusterMap[name] + url = clusterMap[name] + return +} + +func getCluster(cd *cd.Plural) (id string, err error) { + if cd == nil { + err = fmt.Errorf("your CLI is not logged into Plural, try running `plural login` to generate local credentials") + return + } + + clusters, err := cd.ListClusters() + if err != nil { + return + } + + for _, cluster := range clusters { + if *cluster.Node.Handle == "mgmt" { + return cluster.Node.ID, nil + } + } + + err = fmt.Errorf("could not find the management cluster in your Plural cloud instance, contact support for assistance") return } diff --git a/pkg/client/plural.go b/pkg/client/plural.go index 79078fb31..14c9fd8cf 100644 --- a/pkg/client/plural.go +++ b/pkg/client/plural.go @@ -111,6 +111,7 @@ func (p *Plural) HandleInit(c *cli.Context) error { if err := common.HandleLogin(c); err != nil { return err } + p.InitPluralClient() me, err := p.Me() diff --git a/pkg/common/common.go b/pkg/common/common.go index cfdf799ae..e147e3e22 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -23,6 +23,10 @@ import ( "github.com/pluralsh/plural-cli/pkg/utils/git" ) +var ( + loggedIn = false +) + func AppReadme(name string, dryRun bool) error { repoRoot, err := git.Root() if err != nil { @@ -74,6 +78,13 @@ func DoBuild(client api.Client, installation *api.Installation, force bool) erro } func HandleLogin(c *cli.Context) error { + if loggedIn { + return nil + } + defer func() { + loggedIn = true + }() + conf := &config.Config{} conf.Token = "" conf.Endpoint = c.String("endpoint") diff --git a/pkg/provider/aws.go b/pkg/provider/aws.go index 9fad90528..da7b248ad 100644 --- a/pkg/provider/aws.go +++ b/pkg/provider/aws.go @@ -67,21 +67,21 @@ var ( } ) -var awsSurvey = []*survey.Question{ - { - Name: "cluster", - Prompt: &survey.Input{Message: "Enter the name of your cluster:"}, - Validate: validCluster, - }, - { - Name: "region", - Prompt: &survey.Select{Message: "What region will you deploy to?", Default: "us-east-2", Options: awsRegions}, - Validate: survey.Required, - }, -} - func mkAWS(conf config.Config) (provider *AWSProvider, err error) { provider = &AWSProvider{} + var awsSurvey = []*survey.Question{ + { + Name: "cluster", + Prompt: &survey.Input{Message: "Enter the name of your cluster:", Default: clusterFlag}, + Validate: validCluster, + }, + { + Name: "region", + Prompt: &survey.Select{Message: "What region will you deploy to?", Default: "us-east-2", Options: awsRegions}, + Validate: survey.Required, + }, + } + if err = survey.Ask(awsSurvey, provider); err != nil { return } diff --git a/pkg/provider/azure.go b/pkg/provider/azure.go index fc83c024c..8893246b6 100644 --- a/pkg/provider/azure.go +++ b/pkg/provider/azure.go @@ -116,29 +116,6 @@ var ( } ) -var azureSurvey = []*survey.Question{ - { - Name: "cluster", - Prompt: &survey.Input{Message: "Enter the name of your cluster:"}, - Validate: validCluster, - }, - { - Name: "storage", - Prompt: &survey.Input{Message: "Enter the name of the storage account to use for your stage, must be globally unique or already owned by your subscription: "}, - Validate: utils.ValidateStorageAccountName, - }, - { - Name: "region", - Prompt: &survey.Select{Message: "Enter the region you want to deploy to:", Default: "eastus", Options: azureRegions}, - Validate: survey.Required, - }, - { - Name: "resource", - Prompt: &survey.Input{Message: "Enter the name of the resource group to use as default: "}, - Validate: utils.ValidateResourceGroupName, - }, -} - func mkAzure(conf config.Config) (prov *AzureProvider, err error) { var resp struct { Cluster string @@ -146,6 +123,29 @@ func mkAzure(conf config.Config) (prov *AzureProvider, err error) { Region string Resource string } + var azureSurvey = []*survey.Question{ + { + Name: "cluster", + Prompt: &survey.Input{Message: "Enter the name of your cluster:", Default: clusterFlag}, + Validate: validCluster, + }, + { + Name: "storage", + Prompt: &survey.Input{Message: "Enter the name of the storage account to use for your stage, must be globally unique or already owned by your subscription: "}, + Validate: utils.ValidateStorageAccountName, + }, + { + Name: "region", + Prompt: &survey.Select{Message: "Enter the region you want to deploy to:", Default: "eastus", Options: azureRegions}, + Validate: survey.Required, + }, + { + Name: "resource", + Prompt: &survey.Input{Message: "Enter the name of the resource group to use as default: "}, + Validate: utils.ValidateResourceGroupName, + }, + } + err = survey.Ask(azureSurvey, &resp) if err != nil { return diff --git a/pkg/provider/gcp.go b/pkg/provider/gcp.go index bbbdb24e9..1e521ea81 100644 --- a/pkg/provider/gcp.go +++ b/pkg/provider/gcp.go @@ -90,7 +90,7 @@ func getGCPSurvey() []*survey.Question { return []*survey.Question{ { Name: "cluster", - Prompt: &survey.Input{Message: "Enter the name of your cluster"}, + Prompt: &survey.Input{Message: "Enter the name of your cluster", Default: clusterFlag}, Validate: validCluster, }, { diff --git a/pkg/provider/linode.go b/pkg/provider/linode.go index bb2c879ff..088b4991b 100644 --- a/pkg/provider/linode.go +++ b/pkg/provider/linode.go @@ -47,7 +47,7 @@ func getLinodeSurvey() (surveys []*survey.Question, err error) { surveys = []*survey.Question{ { Name: "cluster", - Prompt: &survey.Input{Message: "Enter the name of your cluster"}, + Prompt: &survey.Input{Message: "Enter the name of your cluster", Default: clusterFlag}, Validate: validCluster, }, { diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index 4bb5d07a0..96d4905ba 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -17,6 +17,7 @@ import ( ) var cloudFlag bool +var clusterFlag string type Provider interface { Name() string @@ -106,6 +107,10 @@ func SetCloudFlag(cloud bool) { cloudFlag = cloud } +func SetClusterFlag(cluster string) { + clusterFlag = cluster +} + func FromManifest(man *manifest.ProjectManifest) (Provider, error) { switch man.Provider { case api.ProviderGCP: