22package root
33
44import (
5+ "context"
56 "fmt"
6- "time"
77
88 "github.com/MakeNowJust/heredoc"
99 "github.com/spf13/cobra"
10- "golang.org/x/oauth2"
1110
1211 authCmd "github.com/zeabur/cli/internal/cmd/auth"
1312 completionCmd "github.com/zeabur/cli/internal/cmd/completion"
@@ -56,16 +55,13 @@ func NewCmdRoot(f *cmdutil.Factory, version, commit, date string) (*cobra.Comman
5655
5756 var (
5857 tokenString string
59- token * oauth2.Token
6058 err error
6159 )
6260
63- token , err = f .AuthClient .Login ( )
61+ tokenString , err = f .AuthClient .GenerateToken ( context . Background () )
6462 if err != nil {
6563 return fmt .Errorf ("failed to login: %w" , err )
6664 }
67- tokenString = token .AccessToken
68- f .Config .SetToken (token )
6965 f .Config .SetTokenString (tokenString )
7066 }
7167 // set up the client
@@ -82,31 +78,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, commit, date string) (*cobra.Comman
8278 return fmt .Errorf ("failed to save config: %w" , err )
8379 }
8480
85- // refresh the token if the token is from OAuth2 & it's expired
86- // skip help, version and auth commands
87- if cmd .Name () != "help" && cmd .Name () != "version" && cmd .Parent ().Name () != "auth" {
88- if f .AutoRefreshToken && f .LoggedIn () && f .Config .GetToken () != nil {
89- if f .Config .GetToken ().Expiry .Before (time .Now ()) {
90- f .Log .Info ("Token is from OAuth2 and it's expired, refreshing it" )
91-
92- token := f .Config .GetToken ()
93- token .Expiry = time .Now ()
94- newToken , err := f .AuthClient .RefreshToken (token )
95- if err != nil {
96- return fmt .Errorf ("failed to refresh token, it is recommended to logout and login again: %w" , err )
97- }
98- f .Config .SetToken (newToken )
99- f .Config .SetTokenString (newToken .AccessToken )
100- f .Log .Debug ("New token: " , newToken )
101- if err := f .Config .Write (); err != nil {
102- return fmt .Errorf ("failed to save config: %w" , err )
103- }
104-
105- f .Log .Info ("Token refreshed successfully" )
106- }
107- }
108- }
109-
11081 if f .AutoCheckUpdate && ! f .Debug && version != "dev" {
11182 currentVersion := TrimPrefixV (version )
11283
@@ -140,8 +111,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, commit, date string) (*cobra.Comman
140111 // Persistent flags
141112 cmd .PersistentFlags ().BoolVar (& f .Debug , "debug" , false , "Enable debug logging" )
142113 cmd .PersistentFlags ().BoolVarP (& f .Interactive , config .KeyInteractive , "i" , true , "use interactive mode" )
143- cmd .PersistentFlags ().BoolVar (& f .AutoRefreshToken , config .KeyAutoRefreshToken , true ,
144- "automatically refresh token when it's expired, only works when the token is from browser(OAuth2)" )
145114 cmd .PersistentFlags ().BoolVar (& f .AutoCheckUpdate , config .KeyAutoCheckUpdate , true , "automatically check update" )
146115
147116 // Child commands
0 commit comments