@@ -12,7 +12,7 @@ import (
12
12
"strings"
13
13
)
14
14
15
- func ValidateUserLogin (ctx context.Context , enforceNais bool ) error {
15
+ func ValidateAndGetUserLogin (ctx context.Context , enforceNais bool ) ( string , error ) {
16
16
args := []string {
17
17
"config" ,
18
18
"list" ,
@@ -26,26 +26,26 @@ func ValidateUserLogin(ctx context.Context, enforceNais bool) error {
26
26
cmd .Stderr = os .Stderr
27
27
err := cmd .Run ()
28
28
if err != nil {
29
- return fmt .Errorf ("%v\n error running '%v' command: %w" , buf .String (), cmd .String (), err )
29
+ return "" , fmt .Errorf ("%v\n error running '%v' command: %w" , buf .String (), cmd .String (), err )
30
30
}
31
31
32
32
user := strings .TrimSpace (buf .String ())
33
33
if user == "" {
34
- return fmt .Errorf ("missing active user, have you logged in with 'gcloud auth login --update-adc'" )
34
+ return "" , fmt .Errorf ("missing active user, have you logged in with 'gcloud auth login --update-adc'" )
35
35
}
36
36
37
37
if enforceNais && ! strings .HasSuffix (user , "@nais.io" ) {
38
- return fmt .Errorf ("active gcloud-user is not a nais.io-user: %v" , user )
38
+ return "" , fmt .Errorf ("active gcloud-user is not a nais.io-user: %v" , user )
39
39
}
40
40
41
41
_ , exists := os .LookupEnv ("GOOGLE_APPLICATION_CREDENTIALS" )
42
42
if exists {
43
- return nil
43
+ return user , nil
44
44
}
45
45
46
46
homedir , err := os .UserHomeDir ()
47
47
if err != nil {
48
- return err
48
+ return "" , err
49
49
}
50
50
homedir += "/.config"
51
51
@@ -56,12 +56,12 @@ func ValidateUserLogin(ctx context.Context, enforceNais bool) error {
56
56
_ , err = os .Stat (filepath .Clean (homedir + "/gcloud/application_default_credentials.json" ))
57
57
if err != nil {
58
58
if errors .Is (err , os .ErrNotExist ) {
59
- return fmt .Errorf ("you are missing Application Default Credentials, run `gcloud auth login --update-adc` first" )
59
+ return "" , fmt .Errorf ("you are missing Application Default Credentials, run `gcloud auth login --update-adc` first" )
60
60
}
61
- return err
61
+ return "" , err
62
62
}
63
63
64
- return nil
64
+ return user , nil
65
65
}
66
66
67
67
func GetActiveUserEmail (ctx context.Context ) (string , error ) {
0 commit comments