Skip to content

Commit 058793f

Browse files
authored
auto-detect correct console instance with plural up --cloud (#676)
* auto-detect correct console instance with plural up --cloud * don't ask for the cluster
1 parent 616f8d9 commit 058793f

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

cmd/command/cd/cd.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cd
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/urfave/cli"
89
"helm.sh/helm/v3/pkg/action"
@@ -188,11 +189,13 @@ func confirmCluster(url, token string) (bool, string, error) {
188189
func (p *Plural) HandleCdLogin(c *cli.Context) (err error) {
189190
prior := console.ReadConfig()
190191
if prior.Url != "" {
191-
if common.Affirm(
192-
fmt.Sprintf("You've already configured your console at %s, continue using those credentials?", prior.Url),
193-
"PLURAL_CD_USE_EXISTING_CREDENTIALS",
194-
) {
195-
return
192+
if !strings.EqualFold(common.GetHostnameFromURL(prior.Url), common.GetHostnameFromURL(consoleURL)) {
193+
if common.Affirm(
194+
fmt.Sprintf("You've already configured your console at %s, continue using those credentials?", prior.Url),
195+
"PLURAL_CD_USE_EXISTING_CREDENTIALS",
196+
) {
197+
return
198+
}
196199
}
197200
}
198201

cmd/command/up/up.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/AlecAivazis/survey/v2"
1010
"github.com/pluralsh/plural-cli/pkg/api"
11+
"github.com/pluralsh/plural-cli/pkg/console"
1112
"github.com/samber/lo"
1213
"github.com/urfave/cli"
1314

@@ -165,6 +166,7 @@ func (p *Plural) handleUp(c *cli.Context) error {
165166
}
166167

167168
func (p *Plural) choseCluster() (name, url string, err error) {
169+
prior := console.ReadConfig()
168170
instances, err := p.GetConsoleInstances()
169171
if err != nil {
170172
return
@@ -174,6 +176,11 @@ func (p *Plural) choseCluster() (name, url string, err error) {
174176
clusterMap := map[string]string{}
175177

176178
for _, cluster := range instances {
179+
if prior.Url != "" && strings.EqualFold(common.GetHostnameFromURL(prior.Url), common.GetHostnameFromURL(cluster.URL)) {
180+
name = cluster.Name
181+
url = cluster.URL
182+
return
183+
}
177184
clusterNames = append(clusterNames, cluster.Name)
178185
clusterMap[cluster.Name] = cluster.URL
179186
}

pkg/common/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"fmt"
5+
"net/url"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -192,3 +193,17 @@ func GetIdAndName(input string) (id, name *string) {
192193
}
193194
return
194195
}
196+
197+
func GetHostnameFromURL(u string) string {
198+
parsed, err := url.Parse(u)
199+
if err != nil {
200+
return ""
201+
}
202+
if parsed.Scheme == "" && parsed.Host == "" {
203+
if parsed, err = url.Parse("//" + u); err != nil {
204+
return ""
205+
}
206+
}
207+
hostname := parsed.Hostname()
208+
return hostname
209+
}

0 commit comments

Comments
 (0)