@@ -75,32 +75,22 @@ func NewRootCmd(streams command.Streams, hubClient *hub.Client, store credential
75
75
return err
76
76
}
77
77
78
- if cmd .Annotations ["sudo" ] == "true" && ac .Username != "" {
79
- return requireTwoFactorCode (cmd .Context (), streams , hubClient , store )
80
- }
81
-
82
78
if ac .Username == "" {
83
79
log .Fatal (ansi .Error (`You need to be logged in to Docker Hub to use this tool.
84
80
Please login to Docker Hub using the "hub-tool login" command.` ))
85
81
}
86
82
87
- if ! ac .TokenExpired () {
83
+ if cmd .Annotations ["sudo" ] == "true" {
84
+ if err := tryLogin (cmd .Context (), streams , hubClient , ac , store ); err != nil {
85
+ return err
86
+ }
88
87
return nil
89
88
}
90
89
91
- token , refreshToken , err := hubClient .Login (ac .Username , ac .Password , func () (string , error ) {
92
- return "" , nil
93
- })
94
- if err != nil {
95
- return err
90
+ if ac .TokenExpired () {
91
+ return tryLogin (cmd .Context (), streams , hubClient , ac , store )
96
92
}
97
-
98
- return store .Store (credentials.Auth {
99
- Username : ac .Username ,
100
- Password : ac .Password ,
101
- Token : token ,
102
- RefreshToken : refreshToken ,
103
- })
93
+ return nil
104
94
},
105
95
RunE : func (cmd * cobra.Command , args []string ) error {
106
96
if flags .showVersion {
@@ -149,16 +139,8 @@ func newVersionCmd(streams command.Streams) *cobra.Command {
149
139
}
150
140
}
151
141
152
- func requireTwoFactorCode (ctx context.Context , streams command.Streams , hubClient * hub.Client , store credentials.Store ) error {
153
- ac , err := store .GetAuth ()
154
- if err != nil {
155
- return err
156
- }
157
- if ! ac .TokenExpired () {
158
- return nil
159
- }
160
-
161
- token , refreshToken , err := login .VerifyTwoFactorCode (ctx , streams , hubClient , ac .Username , ac .Password )
142
+ func tryLogin (ctx context.Context , streams command.Streams , hubClient * hub.Client , ac * credentials.Auth , store credentials.Store ) error {
143
+ token , refreshToken , err := login .Login (ctx , streams , hubClient , ac .Username , ac .Password )
162
144
if err != nil {
163
145
return err
164
146
}
0 commit comments