Skip to content

Commit

Permalink
automatically select organisation if there is only one connected to t…
Browse files Browse the repository at this point in the history
…he account

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
  • Loading branch information
inteon committed Mar 8, 2023
1 parent c8067e2 commit 7ad8c81
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/reference/jsctl_auth_login.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jsctl auth login [flags]
### Options

```
--api-url string Base URL of the control-plane API (default "https://platform.jetstack.io")
--credentials string The location of service account credentials file to use instead of the normal oauth login flow
--disconnected Use a disconnected login flow where browser and terminal are not running on the same machine
-h, --help help for login
Expand All @@ -17,7 +18,6 @@ jsctl auth login [flags]
### Options inherited from parent commands

```
--api-url string Base URL of the control-plane API (default "https://platform.jetstack.io")
--config string Location of the user's jsctl config directory (default "HOME or USERPROFILE/.jsctl")
--kubeconfig string Location of the user's kubeconfig file for applying directly to the cluster (default "~/.kube/config")
--stdout If provided, manifests are written to stdout rather than applied to the current cluster
Expand Down
47 changes: 41 additions & 6 deletions internal/command/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"golang.org/x/oauth2"

"github.com/jetstack/jsctl/internal/auth"
"github.com/jetstack/jsctl/internal/client"
"github.com/jetstack/jsctl/internal/command/types"
"github.com/jetstack/jsctl/internal/config"
"github.com/jetstack/jsctl/internal/organization"
)

func Login(run types.RunFunc) *cobra.Command {
var credentials string
var disconnected bool
var apiURL string

cmd := &cobra.Command{
Use: "login",
Expand All @@ -40,17 +43,48 @@ func Login(run types.RunFunc) *cobra.Command {
return fmt.Errorf("failed to save token: %w", err)
}

// update the context with the new token
ctx = auth.TokenToContext(ctx, token)

fmt.Println("Login succeeded")

err = config.Save(ctx, &config.Config{})
cnf := &config.Config{}

// if the user already has an organization selected, we don't need to do anything
cnf, ok := config.FromContext(ctx)
if ok && cnf.Organization != "" {
return nil
}

http := client.New(ctx, apiURL)
organizations, err := organization.List(ctx, http)
if err != nil {
return fmt.Errorf("failed to save configuration: %w", err)
return fmt.Errorf("failed to list organizations: %w", err)
}

cnf, ok := config.FromContext(ctx)
if !ok || cnf.Organization == "" {
fmt.Println("You do not have an organization selected, select one using: \n\n\tjsctl config set organization [name]\n\n" +
"To view organizations you have access to, list them using: \n\n\tjsctl organizations list")
fmt.Println()

if len(organizations) == 1 {
cnf.Organization = organizations[0].ID

fmt.Println("Automatically selected the only organization you have access to: " + organizations[0].ID)
} else {
fmt.Println(
"You do not have an organization selected, select one using:\n" +
"\n" +
"\tjsctl config set organization [name]\n" +
"\n" +
"You have access to the following organizations (run 'jsctl organizations list'):",
)

for _, org := range organizations {
fmt.Println(" - " + org.ID)
}
}

// save the configuration to disk
if err = config.Save(ctx, cnf); err != nil {
return fmt.Errorf("failed to save configuration: %w", err)
}

return nil
Expand All @@ -70,6 +104,7 @@ func Login(run types.RunFunc) *cobra.Command {
false,
"Use a disconnected login flow where browser and terminal are not running on the same machine",
)
flags.StringVar(&apiURL, "api-url", "https://platform.jetstack.io", "Base URL of the control-plane API")

return cmd
}

0 comments on commit 7ad8c81

Please sign in to comment.