|
|
@@ -56,7 +56,6 @@ var logoutCmd = &cobra.Command{
|
|
|
},
|
|
|
}
|
|
|
|
|
|
-var token string = ""
|
|
|
var manual bool = false
|
|
|
|
|
|
func init() {
|
|
|
@@ -80,18 +79,40 @@ func login() error {
|
|
|
user, _ := client.AuthCheck(context.Background())
|
|
|
|
|
|
if user != nil {
|
|
|
+ // set the token if the user calls login with the --token flag or the PORTER_TOKEN env
|
|
|
if config.Token != "" {
|
|
|
- // set the token if the user calls login with the --token flag
|
|
|
config.SetToken(config.Token)
|
|
|
color.New(color.FgGreen).Println("Successfully logged in!")
|
|
|
|
|
|
- projID, err := api.GetProjectIDFromToken(config.Token)
|
|
|
+ projID, exists, err := api.GetProjectIDFromToken(config.Token)
|
|
|
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- config.SetProject(projID)
|
|
|
+ // if project ID does not exist for the token, this is a user-issued CLI token, so the project
|
|
|
+ // ID should be queried
|
|
|
+ if !exists {
|
|
|
+ err = setProjectForUser(client, user.ID)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // if the project ID does exist for the token, this is a project-issued token, and
|
|
|
+ // the project should be set automatically
|
|
|
+ err = config.SetProject(projID)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = setProjectCluster(client, projID)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
color.Yellow("You are already logged in. If you'd like to log out, run \"porter auth logout\".")
|
|
|
}
|
|
|
@@ -104,70 +125,50 @@ func login() error {
|
|
|
return loginManual()
|
|
|
}
|
|
|
|
|
|
- // check for a token
|
|
|
- var err error
|
|
|
-
|
|
|
- if token == "" {
|
|
|
- token, err = loginBrowser.Login(config.Host)
|
|
|
+ // log the user in
|
|
|
+ token, err := loginBrowser.Login(config.Host)
|
|
|
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- // set the token in config
|
|
|
- err = config.SetToken(token)
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- client := api.NewClientWithToken(config.Host+"/api", token)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- user, err := client.AuthCheck(context.Background())
|
|
|
+ // set the token in config
|
|
|
+ err = config.SetToken(token)
|
|
|
|
|
|
- if user == nil {
|
|
|
- color.Red("Invalid token.")
|
|
|
- return err
|
|
|
- }
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- color.New(color.FgGreen).Println("Successfully logged in!")
|
|
|
+ client = api.NewClientWithToken(config.Host+"/api", token)
|
|
|
|
|
|
- // get a list of projects, and set the current project
|
|
|
- projects, err := client.ListUserProjects(context.Background(), user.ID)
|
|
|
+ user, err = client.AuthCheck(context.Background())
|
|
|
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- if len(projects) > 0 {
|
|
|
- config.SetProject(projects[0].ID)
|
|
|
- }
|
|
|
- } else {
|
|
|
- // set the token in config
|
|
|
- err = config.SetToken(token)
|
|
|
+ if user == nil {
|
|
|
+ color.Red("Invalid token.")
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+ color.New(color.FgGreen).Println("Successfully logged in!")
|
|
|
|
|
|
- client := api.NewClientWithToken(config.Host+"/api", token)
|
|
|
+ return setProjectForUser(client, user.ID)
|
|
|
+}
|
|
|
|
|
|
- user, err := client.AuthCheck(context.Background())
|
|
|
+func setProjectForUser(client *api.Client, userID uint) error {
|
|
|
+ // get a list of projects, and set the current project
|
|
|
+ projects, err := client.ListUserProjects(context.Background(), userID)
|
|
|
|
|
|
- if user == nil {
|
|
|
- color.Red("Invalid token.")
|
|
|
- return err
|
|
|
- }
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- color.New(color.FgGreen).Println("Successfully logged in!")
|
|
|
+ if len(projects) > 0 {
|
|
|
+ config.SetProject(projects[0].ID)
|
|
|
|
|
|
- projID, err := api.GetProjectIDFromToken(token)
|
|
|
+ err = setProjectCluster(client, projects[0].ID)
|
|
|
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
-
|
|
|
- config.SetProject(projID)
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
@@ -215,6 +216,12 @@ func loginManual() error {
|
|
|
|
|
|
if len(projects) > 0 {
|
|
|
config.SetProject(projects[0].ID)
|
|
|
+
|
|
|
+ err = setProjectCluster(client, projects[0].ID)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return nil
|