Skip to content

Commit

Permalink
Add ability to open the console URL in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
cblecker committed Sep 7, 2019
1 parent 679fa47 commit 8cd19a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
34 changes: 21 additions & 13 deletions cmd/ocm/cluster/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import (
"strings"

"github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift-online/ocm-cli/pkg/config"
)

var args struct {
user string
user string
console bool
}

const ClustersPageSize = 50
Expand All @@ -47,13 +49,8 @@ var Cmd = &cobra.Command{

func init() {
flags := Cmd.Flags()
flags.StringVarP(
&args.user,
"username",
"u",
"",
"Username, will prompt if not provided",
)
flags.StringVarP(&args.user, "username", "u", "", "Username, will prompt if not provided")
flags.BoolVarP(&args.console, "console", "", false, "Open the OpenShift console for the cluster")

}
func run(cmd *cobra.Command, argv []string) error {
Expand Down Expand Up @@ -106,30 +103,41 @@ func run(cmd *cobra.Command, argv []string) error {
total, argv[0], len(clusters),
)
}
var clusterid, clusterName, url string
var clusterid, clusterName, apiURL, consoleURL string
if len(clusters) == 1 {
for _, v := range clusters {
clusterid = v.ID()
clusterName = v.Name()
}
url = clusters[0].API().URL()
apiURL = clusters[0].API().URL()
consoleURL = clusters[0].Console().URL()
fmt.Printf("Only one cluster match the args, will login to cluster:\n Name: %s\n ID: %s\n", clusterName, clusterid)
} else {
cluster, err := doSurvey(clusters)
if err != nil {
return fmt.Errorf("Can't find clusters: %v", err)
}
url = cluster.API().URL()
apiURL = cluster.API().URL()
consoleURL = cluster.Console().URL()
clusterid = cluster.ID()
clusterName = cluster.Name()
}

if len(url) == 0 {
if args.console {
if len(consoleURL) == 0 {
return fmt.Errorf("Cannot find the console url for cluster: %s", clusterName)
}

// Open the console url in the broswer, return any errors
return browser.OpenURL(consoleURL)
}

if len(apiURL) == 0 {
return fmt.Errorf("Cannot find the api url for cluster: %s", clusterName)
}
fmt.Printf("Will login to cluster:\n Name: %s\n ID: %s\n", clusterName, clusterid)
ocArgs := []string{}
ocArgs = append(ocArgs, "login", url)
ocArgs = append(ocArgs, "login", apiURL)
if args.user != "" {
ocArgs = append(ocArgs, "--username="+args.user)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
github.com/openshift-online/ocm-sdk-go v0.1.30
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/prometheus/common v0.6.0 // indirect
github.com/prometheus/procfs v0.0.3 // indirect
github.com/spf13/cobra v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/openshift-online/ocm-sdk-go v0.1.30 h1:v/xD1v3LqNm0f1+aBMPDEOeTs96pTSiMfVe731GB/HQ=
github.com/openshift-online/ocm-sdk-go v0.1.30/go.mod h1:rUF3jK/T4pieeafrKdBZ2TLnjqAUt7MxgvsmCdzXVQw=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down

0 comments on commit 8cd19a5

Please sign in to comment.