@@ -17,15 +17,19 @@ import (
1717 "github.com/stainless-api/stainless-api-go/option"
1818 "github.com/tidwall/gjson"
1919 "github.com/urfave/cli/v3"
20+ "golang.org/x/text/cases"
21+ "golang.org/x/text/language"
2022)
2123
24+ const defaultClientID = "stl_client_0001u04Vo1IWoSe0Mwinw2SVuuO3hTkvL"
25+
2226var authLogin = cli.Command {
2327 Name : "login" ,
2428 Usage : "Authenticate with Stainless API" ,
2529 Flags : []cli.Flag {
2630 & cli.StringFlag {
2731 Name : "client-id" ,
28- Value : "stl_client_0001u04Vo1IWoSe0Mwinw2SVuuO3hTkvL" ,
32+ Value : defaultClientID ,
2933 Usage : "OAuth client ID" ,
3034 },
3135 },
@@ -47,13 +51,9 @@ var authStatus = cli.Command{
4751 HideHelpCommand : true ,
4852}
4953
50- func handleAuthLogin (ctx context.Context , cmd * cli.Command ) error {
51- cc := getAPICommandContext (cmd )
52- clientID := cmd .String ("client-id" )
53- scope := "openapi:read project:write project:read"
54- authResult , err := startDeviceFlow (ctx , cmd , cc .client , clientID , scope )
55- if err != nil {
56- return err
54+ func authenticate (ctx context.Context , cmd * cli.Command , forceAuthentication bool ) error {
55+ if apiKey := os .Getenv ("STAINLESS_API_KEY" ); apiKey != "" && ! forceAuthentication {
56+ return nil
5757 }
5858
5959 config , err := NewAuthConfig ()
@@ -62,6 +62,20 @@ func handleAuthLogin(ctx context.Context, cmd *cli.Command) error {
6262 return fmt .Errorf ("authentication failed" )
6363 }
6464
65+ if ! forceAuthentication {
66+ if found , err := config .Find (); err == nil && found && config .AccessToken != "" {
67+ return nil
68+ }
69+ }
70+
71+ cc := getAPICommandContext (cmd )
72+ clientID := cmd .String ("client-id" )
73+ scope := "openapi:read project:write project:read"
74+ authResult , err := startDeviceFlow (ctx , cmd , cc .client , clientID , scope )
75+ if err != nil {
76+ return err
77+ }
78+
6579 config .AccessToken = authResult .AccessToken
6680 config .RefreshToken = authResult .RefreshToken
6781 config .TokenType = authResult .TokenType
@@ -70,10 +84,14 @@ func handleAuthLogin(ctx context.Context, cmd *cli.Command) error {
7084 Error ("Failed to save authentication: %v" , err )
7185 return fmt .Errorf ("authentication failed" )
7286 }
73- Success ("Authentication successful! Your credentials have been saved to " + config .ConfigPath )
87+ Success ("Authentication successful! Your credentials have been saved to %s" , config .ConfigPath )
7488 return nil
7589}
7690
91+ func handleAuthLogin (ctx context.Context , cmd * cli.Command ) error {
92+ return authenticate (ctx , cmd , true )
93+ }
94+
7795func handleAuthLogout (ctx context.Context , cmd * cli.Command ) error {
7896 config := & AuthConfig {}
7997 found , err := config .Find ()
@@ -153,8 +171,7 @@ func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Cli
153171 ok , _ , err := group .Confirm (cmd , "browser" , "Open browser?" , "" , true )
154172 if err != nil {
155173 return nil , err
156- }
157- if ok {
174+ } else if ok {
158175 if err := browser .OpenURL (deviceResponse .VerificationURIComplete ); err == nil {
159176 group .Info ("Opening browser..." )
160177 } else {
@@ -244,7 +261,7 @@ func getDeviceName() string {
244261 case "linux" :
245262 osName = "Linux"
246263 default :
247- osName = strings .Title (osName )
264+ osName = cases .Title ( language . English ). String (osName )
248265 }
249266
250267 if username != "" {
0 commit comments