@@ -80,15 +80,15 @@ type Remote struct {
80
80
}
81
81
82
82
// EnableDistributionSpecFlag set distribution specification flag as applicable.
83
- func (opts * Remote ) EnableDistributionSpecFlag () {
84
- opts .applyDistributionSpec = true
83
+ func (remo * Remote ) EnableDistributionSpecFlag () {
84
+ remo .applyDistributionSpec = true
85
85
}
86
86
87
87
// ApplyFlags applies flags to a command flag set.
88
- func (opts * Remote ) ApplyFlags (fs * pflag.FlagSet ) {
89
- opts .ApplyFlagsWithPrefix (fs , "" , "" )
90
- fs .BoolVar (& opts .secretFromStdin , passwordFromStdinFlag , false , "read password from stdin" )
91
- fs .BoolVar (& opts .secretFromStdin , identityTokenFromStdinFlag , false , "read identity token from stdin" )
88
+ func (remo * Remote ) ApplyFlags (fs * pflag.FlagSet ) {
89
+ remo .ApplyFlagsWithPrefix (fs , "" , "" )
90
+ fs .BoolVar (& remo .secretFromStdin , passwordFromStdinFlag , false , "read password from stdin" )
91
+ fs .BoolVar (& remo .secretFromStdin , identityTokenFromStdinFlag , false , "read identity token from stdin" )
92
92
}
93
93
94
94
func applyPrefix (prefix , description string ) (flagPrefix , notePrefix string ) {
@@ -100,7 +100,7 @@ func applyPrefix(prefix, description string) (flagPrefix, notePrefix string) {
100
100
101
101
// ApplyFlagsWithPrefix applies flags to a command flag set with a prefix string.
102
102
// Commonly used for non-unary remote targets.
103
- func (opts * Remote ) ApplyFlagsWithPrefix (fs * pflag.FlagSet , prefix , description string ) {
103
+ func (remo * Remote ) ApplyFlagsWithPrefix (fs * pflag.FlagSet , prefix , description string ) {
104
104
var (
105
105
shortUser string
106
106
shortPassword string
@@ -111,26 +111,26 @@ func (opts *Remote) ApplyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description
111
111
shortUser , shortPassword = "u" , "p"
112
112
shortHeader = "H"
113
113
}
114
- opts .flagPrefix , notePrefix = applyPrefix (prefix , description )
114
+ remo .flagPrefix , notePrefix = applyPrefix (prefix , description )
115
115
116
- if opts .applyDistributionSpec {
117
- opts .DistributionSpec .ApplyFlagsWithPrefix (fs , prefix , description )
116
+ if remo .applyDistributionSpec {
117
+ remo .DistributionSpec .ApplyFlagsWithPrefix (fs , prefix , description )
118
118
}
119
- fs .StringVarP (& opts .Username , opts .flagPrefix + usernameFlag , shortUser , "" , notePrefix + "registry username" )
120
- fs .StringVarP (& opts .Secret , opts .flagPrefix + passwordFlag , shortPassword , "" , notePrefix + "registry password or identity token" )
121
- fs .StringVar (& opts .Secret , opts .flagPrefix + identityTokenFlag , "" , notePrefix + "registry identity token" )
122
- fs .BoolVar (& opts .Insecure , opts .flagPrefix + "insecure" , false , "allow connections to " + notePrefix + "SSL registry without certs" )
123
- plainHTTPFlagName := opts .flagPrefix + "plain-http"
119
+ fs .StringVarP (& remo .Username , remo .flagPrefix + usernameFlag , shortUser , "" , notePrefix + "registry username" )
120
+ fs .StringVarP (& remo .Secret , remo .flagPrefix + passwordFlag , shortPassword , "" , notePrefix + "registry password or identity token" )
121
+ fs .StringVar (& remo .Secret , remo .flagPrefix + identityTokenFlag , "" , notePrefix + "registry identity token" )
122
+ fs .BoolVar (& remo .Insecure , remo .flagPrefix + "insecure" , false , "allow connections to " + notePrefix + "SSL registry without certs" )
123
+ plainHTTPFlagName := remo .flagPrefix + "plain-http"
124
124
plainHTTP := fs .Bool (plainHTTPFlagName , false , "allow insecure connections to " + notePrefix + "registry without SSL check" )
125
- opts .plainHTTP = func () (bool , bool ) {
125
+ remo .plainHTTP = func () (bool , bool ) {
126
126
return * plainHTTP , fs .Changed (plainHTTPFlagName )
127
127
}
128
- fs .StringVar (& opts .CACertFilePath , opts .flagPrefix + caFileFlag , "" , "server certificate authority file for the remote " + notePrefix + "registry" )
129
- fs .StringVarP (& opts .CertFilePath , opts .flagPrefix + certFileFlag , "" , "" , "client certificate file for the remote " + notePrefix + "registry" )
130
- fs .StringVarP (& opts .KeyFilePath , opts .flagPrefix + keyFileFlag , "" , "" , "client private key file for the remote " + notePrefix + "registry" )
131
- fs .StringArrayVar (& opts .resolveFlag , opts .flagPrefix + "resolve" , nil , "customized DNS for " + notePrefix + "registry, formatted in `host:port:address[:address_port]`" )
132
- fs .StringArrayVar (& opts .Configs , opts .flagPrefix + "registry-config" , nil , "`path` of the authentication file for " + notePrefix + "registry" )
133
- fs .StringArrayVarP (& opts .headerFlags , opts .flagPrefix + "header" , shortHeader , nil , "add custom headers to " + notePrefix + "requests" )
128
+ fs .StringVar (& remo .CACertFilePath , remo .flagPrefix + caFileFlag , "" , "server certificate authority file for the remote " + notePrefix + "registry" )
129
+ fs .StringVarP (& remo .CertFilePath , remo .flagPrefix + certFileFlag , "" , "" , "client certificate file for the remote " + notePrefix + "registry" )
130
+ fs .StringVarP (& remo .KeyFilePath , remo .flagPrefix + keyFileFlag , "" , "" , "client private key file for the remote " + notePrefix + "registry" )
131
+ fs .StringArrayVar (& remo .resolveFlag , remo .flagPrefix + "resolve" , nil , "customized DNS for " + notePrefix + "registry, formatted in `host:port:address[:address_port]`" )
132
+ fs .StringArrayVar (& remo .Configs , remo .flagPrefix + "registry-config" , nil , "`path` of the authentication file for " + notePrefix + "registry" )
133
+ fs .StringArrayVarP (& remo .headerFlags , remo .flagPrefix + "header" , shortHeader , nil , "add custom headers to " + notePrefix + "requests" )
134
134
}
135
135
136
136
// CheckStdinConflict checks if PasswordFromStdin or IdentityTokenFromStdin of a
@@ -146,10 +146,10 @@ func CheckStdinConflict(flags *pflag.FlagSet) error {
146
146
}
147
147
148
148
// Parse tries to read password with optional cmd prompt.
149
- func (opts * Remote ) Parse (cmd * cobra.Command ) error {
150
- usernameAndIdTokenFlags := []string {opts .flagPrefix + usernameFlag , opts .flagPrefix + identityTokenFlag }
151
- passwordAndIdTokenFlags := []string {opts .flagPrefix + passwordFlag , opts .flagPrefix + identityTokenFlag }
152
- certFileAndKeyFileFlags := []string {opts .flagPrefix + certFileFlag , opts .flagPrefix + keyFileFlag }
149
+ func (remo * Remote ) Parse (cmd * cobra.Command ) error {
150
+ usernameAndIdTokenFlags := []string {remo .flagPrefix + usernameFlag , remo .flagPrefix + identityTokenFlag }
151
+ passwordAndIdTokenFlags := []string {remo .flagPrefix + passwordFlag , remo .flagPrefix + identityTokenFlag }
152
+ certFileAndKeyFileFlags := []string {remo .flagPrefix + certFileFlag , remo .flagPrefix + keyFileFlag }
153
153
if cmd .Flags ().Lookup (identityTokenFromStdinFlag ) != nil {
154
154
usernameAndIdTokenFlags = append (usernameAndIdTokenFlags , identityTokenFromStdinFlag )
155
155
passwordAndIdTokenFlags = append (passwordAndIdTokenFlags , identityTokenFromStdinFlag )
@@ -163,45 +163,45 @@ func (opts *Remote) Parse(cmd *cobra.Command) error {
163
163
if err := oerrors .CheckMutuallyExclusiveFlags (cmd .Flags (), passwordAndIdTokenFlags ... ); err != nil {
164
164
return err
165
165
}
166
- if err := opts .parseCustomHeaders (); err != nil {
166
+ if err := remo .parseCustomHeaders (); err != nil {
167
167
return err
168
168
}
169
169
if err := oerrors .CheckRequiredTogetherFlags (cmd .Flags (), certFileAndKeyFileFlags ... ); err != nil {
170
170
return err
171
171
}
172
- return opts .readSecret (cmd )
172
+ return remo .readSecret (cmd )
173
173
}
174
174
175
175
// readSecret tries to read password or identity token with
176
176
// optional cmd prompt.
177
- func (opts * Remote ) readSecret (cmd * cobra.Command ) (err error ) {
177
+ func (remo * Remote ) readSecret (cmd * cobra.Command ) (err error ) {
178
178
if cmd .Flags ().Changed (identityTokenFlag ) {
179
179
_ , _ = fmt .Fprintln (cmd .ErrOrStderr (), "WARNING! Using --identity-token via the CLI is insecure. Use --identity-token-stdin." )
180
180
} else if cmd .Flags ().Changed (passwordFlag ) {
181
181
_ , _ = fmt .Fprintln (cmd .ErrOrStderr (), "WARNING! Using --password via the CLI is insecure. Use --password-stdin." )
182
- } else if opts .secretFromStdin {
182
+ } else if remo .secretFromStdin {
183
183
// Prompt for credential
184
184
secret , err := io .ReadAll (os .Stdin )
185
185
if err != nil {
186
186
return err
187
187
}
188
- opts .Secret = strings .TrimSuffix (string (secret ), "\n " )
189
- opts .Secret = strings .TrimSuffix (opts .Secret , "\r " )
188
+ remo .Secret = strings .TrimSuffix (string (secret ), "\n " )
189
+ remo .Secret = strings .TrimSuffix (remo .Secret , "\r " )
190
190
}
191
191
return nil
192
192
}
193
193
194
194
// parseResolve parses resolve flag.
195
- func (opts * Remote ) parseResolve (baseDial onet.DialFunc ) (onet.DialFunc , error ) {
196
- if len (opts .resolveFlag ) == 0 {
195
+ func (remo * Remote ) parseResolve (baseDial onet.DialFunc ) (onet.DialFunc , error ) {
196
+ if len (remo .resolveFlag ) == 0 {
197
197
return baseDial , nil
198
198
}
199
199
200
200
formatError := func (param , message string ) error {
201
201
return fmt .Errorf ("failed to parse resolve flag %q: %s" , param , message )
202
202
}
203
203
var dialer onet.Dialer
204
- for _ , r := range opts .resolveFlag {
204
+ for _ , r := range remo .resolveFlag {
205
205
parts := strings .SplitN (r , ":" , 4 )
206
206
length := len (parts )
207
207
if length < 3 {
@@ -231,19 +231,19 @@ func (opts *Remote) parseResolve(baseDial onet.DialFunc) (onet.DialFunc, error)
231
231
}
232
232
233
233
// tlsConfig assembles the tls config.
234
- func (opts * Remote ) tlsConfig () (* tls.Config , error ) {
234
+ func (remo * Remote ) tlsConfig () (* tls.Config , error ) {
235
235
config := & tls.Config {
236
- InsecureSkipVerify : opts .Insecure ,
236
+ InsecureSkipVerify : remo .Insecure ,
237
237
}
238
- if opts .CACertFilePath != "" {
238
+ if remo .CACertFilePath != "" {
239
239
var err error
240
- config .RootCAs , err = crypto .LoadCertPool (opts .CACertFilePath )
240
+ config .RootCAs , err = crypto .LoadCertPool (remo .CACertFilePath )
241
241
if err != nil {
242
242
return nil , err
243
243
}
244
244
}
245
- if opts .CertFilePath != "" && opts .KeyFilePath != "" {
246
- cert , err := tls .LoadX509KeyPair (opts .CertFilePath , opts .KeyFilePath )
245
+ if remo .CertFilePath != "" && remo .KeyFilePath != "" {
246
+ cert , err := tls .LoadX509KeyPair (remo .CertFilePath , remo .KeyFilePath )
247
247
if err != nil {
248
248
return nil , err
249
249
}
@@ -253,14 +253,14 @@ func (opts *Remote) tlsConfig() (*tls.Config, error) {
253
253
}
254
254
255
255
// authClient assembles a oras auth client.
256
- func (opts * Remote ) authClient (registry string , debug bool ) (client * auth.Client , err error ) {
257
- config , err := opts .tlsConfig ()
256
+ func (remo * Remote ) authClient (registry string , debug bool ) (client * auth.Client , err error ) {
257
+ config , err := remo .tlsConfig ()
258
258
if err != nil {
259
259
return nil , err
260
260
}
261
261
baseTransport := http .DefaultTransport .(* http.Transport ).Clone ()
262
262
baseTransport .TLSClientConfig = config
263
- dialContext , err := opts .parseResolve (baseTransport .DialContext )
263
+ dialContext , err := remo .parseResolve (baseTransport .DialContext )
264
264
if err != nil {
265
265
return nil , err
266
266
}
@@ -272,44 +272,44 @@ func (opts *Remote) authClient(registry string, debug bool) (client *auth.Client
272
272
Transport : retry .NewTransport (baseTransport ),
273
273
},
274
274
Cache : auth .NewCache (),
275
- Header : opts .headers ,
275
+ Header : remo .headers ,
276
276
}
277
277
client .SetUserAgent ("oras/" + version .GetVersion ())
278
278
if debug {
279
279
client .Client .Transport = trace .NewTransport (client .Client .Transport )
280
280
}
281
281
282
- cred := opts .Credential ()
282
+ cred := remo .Credential ()
283
283
if cred != auth .EmptyCredential {
284
284
client .Credential = func (ctx context.Context , s string ) (auth.Credential , error ) {
285
285
return cred , nil
286
286
}
287
287
} else {
288
288
var err error
289
- opts .store , err = credential .NewStore (opts .Configs ... )
289
+ remo .store , err = credential .NewStore (remo .Configs ... )
290
290
if err != nil {
291
291
return nil , err
292
292
}
293
- client .Credential = credentials .Credential (opts .store )
293
+ client .Credential = credentials .Credential (remo .store )
294
294
}
295
295
return
296
296
}
297
297
298
298
// ConfigPath returns the config path of the credential store.
299
- func (opts * Remote ) ConfigPath () (string , error ) {
300
- if opts .store == nil {
299
+ func (remo * Remote ) ConfigPath () (string , error ) {
300
+ if remo .store == nil {
301
301
return "" , errors .New ("no credential store initialized" )
302
302
}
303
- if ds , ok := opts .store .(* credentials.DynamicStore ); ok {
303
+ if ds , ok := remo .store .(* credentials.DynamicStore ); ok {
304
304
return ds .ConfigPath (), nil
305
305
}
306
306
return "" , errors .New ("store doesn't support getting config path" )
307
307
}
308
308
309
- func (opts * Remote ) parseCustomHeaders () error {
310
- if len (opts .headerFlags ) != 0 {
309
+ func (remo * Remote ) parseCustomHeaders () error {
310
+ if len (remo .headerFlags ) != 0 {
311
311
headers := map [string ][]string {}
312
- for _ , h := range opts .headerFlags {
312
+ for _ , h := range remo .headerFlags {
313
313
name , value , found := strings .Cut (h , ":" )
314
314
if ! found || strings .TrimSpace (name ) == "" {
315
315
// In conformance to the RFC 2616 specification
@@ -318,24 +318,24 @@ func (opts *Remote) parseCustomHeaders() error {
318
318
}
319
319
headers [name ] = append (headers [name ], value )
320
320
}
321
- opts .headers = headers
321
+ remo .headers = headers
322
322
}
323
323
return nil
324
324
}
325
325
326
326
// Credential returns a credential based on the remote options.
327
- func (opts * Remote ) Credential () auth.Credential {
328
- return credential .Credential (opts .Username , opts .Secret )
327
+ func (remo * Remote ) Credential () auth.Credential {
328
+ return credential .Credential (remo .Username , remo .Secret )
329
329
}
330
330
331
- func (opts * Remote ) handleWarning (registry string , logger logrus.FieldLogger ) func (warning remote.Warning ) {
332
- if opts .warned == nil {
333
- opts .warned = make (map [string ]* sync.Map )
331
+ func (remo * Remote ) handleWarning (registry string , logger logrus.FieldLogger ) func (warning remote.Warning ) {
332
+ if remo .warned == nil {
333
+ remo .warned = make (map [string ]* sync.Map )
334
334
}
335
- warned := opts .warned [registry ]
335
+ warned := remo .warned [registry ]
336
336
if warned == nil {
337
337
warned = & sync.Map {}
338
- opts .warned [registry ] = warned
338
+ remo .warned [registry ] = warned
339
339
}
340
340
logger = logger .WithField ("registry" , registry )
341
341
return func (warning remote.Warning ) {
@@ -346,22 +346,22 @@ func (opts *Remote) handleWarning(registry string, logger logrus.FieldLogger) fu
346
346
}
347
347
348
348
// NewRegistry assembles a oras remote registry.
349
- func (opts * Remote ) NewRegistry (registry string , common Common , logger logrus.FieldLogger ) (reg * remote.Registry , err error ) {
349
+ func (remo * Remote ) NewRegistry (registry string , common Common , logger logrus.FieldLogger ) (reg * remote.Registry , err error ) {
350
350
reg , err = remote .NewRegistry (registry )
351
351
if err != nil {
352
352
return nil , err
353
353
}
354
354
registry = reg .Reference .Registry
355
- reg .PlainHTTP = opts .isPlainHttp (registry )
356
- reg .HandleWarning = opts .handleWarning (registry , logger )
357
- if reg .Client , err = opts .authClient (registry , common .Debug ); err != nil {
355
+ reg .PlainHTTP = remo .isPlainHttp (registry )
356
+ reg .HandleWarning = remo .handleWarning (registry , logger )
357
+ if reg .Client , err = remo .authClient (registry , common .Debug ); err != nil {
358
358
return nil , err
359
359
}
360
360
return
361
361
}
362
362
363
363
// NewRepository assembles a oras remote repository.
364
- func (opts * Remote ) NewRepository (reference string , common Common , logger logrus.FieldLogger ) (repo * remote.Repository , err error ) {
364
+ func (remo * Remote ) NewRepository (reference string , common Common , logger logrus.FieldLogger ) (repo * remote.Repository , err error ) {
365
365
repo , err = remote .NewRepository (reference )
366
366
if err != nil {
367
367
if errors .Unwrap (err ) == errdef .ErrInvalidReference {
@@ -370,23 +370,23 @@ func (opts *Remote) NewRepository(reference string, common Common, logger logrus
370
370
return nil , err
371
371
}
372
372
registry := repo .Reference .Registry
373
- repo .PlainHTTP = opts .isPlainHttp (registry )
374
- repo .HandleWarning = opts .handleWarning (registry , logger )
375
- if repo .Client , err = opts .authClient (registry , common .Debug ); err != nil {
373
+ repo .PlainHTTP = remo .isPlainHttp (registry )
374
+ repo .HandleWarning = remo .handleWarning (registry , logger )
375
+ if repo .Client , err = remo .authClient (registry , common .Debug ); err != nil {
376
376
return nil , err
377
377
}
378
378
repo .SkipReferrersGC = true
379
- if opts .ReferrersAPI != nil {
380
- if err := repo .SetReferrersCapability (* opts .ReferrersAPI ); err != nil {
379
+ if remo .ReferrersAPI != nil {
380
+ if err := repo .SetReferrersCapability (* remo .ReferrersAPI ); err != nil {
381
381
return nil , err
382
382
}
383
383
}
384
384
return
385
385
}
386
386
387
387
// isPlainHttp returns the plain http flag for a given registry.
388
- func (opts * Remote ) isPlainHttp (registry string ) bool {
389
- plainHTTP , enforced := opts .plainHTTP ()
388
+ func (remo * Remote ) isPlainHttp (registry string ) bool {
389
+ plainHTTP , enforced := remo .plainHTTP ()
390
390
if enforced {
391
391
return plainHTTP
392
392
}
@@ -399,11 +399,11 @@ func (opts *Remote) isPlainHttp(registry string) bool {
399
399
}
400
400
401
401
// Modify modifies error during cmd execution.
402
- func (opts * Remote ) Modify (cmd * cobra.Command , err error ) (error , bool ) {
402
+ func (remo * Remote ) Modify (cmd * cobra.Command , err error ) (error , bool ) {
403
403
var errResp * errcode.ErrorResponse
404
404
405
405
if errors .Is (err , auth .ErrBasicCredentialNotFound ) {
406
- return opts .DecorateCredentialError (err ), true
406
+ return remo .DecorateCredentialError (err ), true
407
407
}
408
408
409
409
if errors .As (err , & errResp ) {
@@ -416,9 +416,9 @@ func (opts *Remote) Modify(cmd *cobra.Command, err error) (error, bool) {
416
416
}
417
417
418
418
// DecorateCredentialError decorate error with recommendation.
419
- func (opts * Remote ) DecorateCredentialError (err error ) * oerrors.Error {
419
+ func (remo * Remote ) DecorateCredentialError (err error ) * oerrors.Error {
420
420
configPath := " "
421
- if path , pathErr := opts .ConfigPath (); pathErr == nil {
421
+ if path , pathErr := remo .ConfigPath (); pathErr == nil {
422
422
configPath += fmt .Sprintf ("at %q " , path )
423
423
}
424
424
return & oerrors.Error {
0 commit comments