| OLD | NEW |
| 1 // Copyright 2014 The LUCI Authors. All rights reserved. | 1 // Copyright 2014 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package cli | 5 package cli |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "crypto/sha1" | 8 "crypto/sha1" |
| 9 "encoding/hex" | 9 "encoding/hex" |
| 10 "encoding/json" | 10 "encoding/json" |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 261 |
| 262 func (opts *clientOptions) makeCipdClient(ctx context.Context, root string) (cip
d.Client, error) { | 262 func (opts *clientOptions) makeCipdClient(ctx context.Context, root string) (cip
d.Client, error) { |
| 263 authOpts, err := opts.authFlags.Options() | 263 authOpts, err := opts.authFlags.Options() |
| 264 if err != nil { | 264 if err != nil { |
| 265 return nil, err | 265 return nil, err |
| 266 } | 266 } |
| 267 client, err := auth.NewAuthenticator(ctx, auth.OptionalLogin, authOpts).
Client() | 267 client, err := auth.NewAuthenticator(ctx, auth.OptionalLogin, authOpts).
Client() |
| 268 if err != nil { | 268 if err != nil { |
| 269 return nil, err | 269 return nil, err |
| 270 } | 270 } |
| 271 ua := cipd.UserAgent | |
| 272 if prefix := cli.LookupEnv(ctx, CIPDHTTPUserAgentPrefix); prefix.Exists
{ | |
| 273 ua = fmt.Sprintf("%s/%s", prefix.Value, ua) | |
| 274 } | |
| 275 cacheDir := opts.cacheDir | 271 cacheDir := opts.cacheDir |
| 276 if cacheDir == "" { | 272 if cacheDir == "" { |
| 277 » » if cacheDirEnv := cli.LookupEnv(ctx, CIPDCacheDir); cacheDirEnv.
Exists { | 273 » » cacheDir, err = CacheDir(ctx) |
| 278 » » » cacheDir = cacheDirEnv.Value | 274 » » if err != nil { |
| 279 » » » if cacheDir != "" && !filepath.IsAbs(cacheDir) { | 275 » » » return nil, err |
| 280 » » » » return nil, fmt.Errorf("Bad %s: not an absolute
path - %s", CIPDCacheDir, cacheDir) | |
| 281 » » » } | |
| 282 } | 276 } |
| 283 } | 277 } |
| 284 return cipd.NewClient(cipd.ClientOptions{ | 278 return cipd.NewClient(cipd.ClientOptions{ |
| 285 ServiceURL: opts.serviceURL, | 279 ServiceURL: opts.serviceURL, |
| 286 Root: root, | 280 Root: root, |
| 287 » » UserAgent: ua, | 281 » » UserAgent: UserAgent(ctx), |
| 288 CacheDir: cacheDir, | 282 CacheDir: cacheDir, |
| 289 AuthenticatedClient: client, | 283 AuthenticatedClient: client, |
| 290 AnonymousClient: http.DefaultClient, | 284 AnonymousClient: http.DefaultClient, |
| 291 }) | 285 }) |
| 292 } | 286 } |
| 293 | 287 |
| 288 // UserAgent returns a CIPD user agent string, based on a CLI context. |
| 289 // |
| 290 // It knows how to use CIPDHTTPUserAgentPrefix env var. |
| 291 func UserAgent(ctx context.Context) string { |
| 292 if prefix := cli.LookupEnv(ctx, CIPDHTTPUserAgentPrefix); prefix.Exists
{ |
| 293 return fmt.Sprintf("%s/%s", prefix.Value, cipd.UserAgent) |
| 294 } |
| 295 return cipd.UserAgent |
| 296 } |
| 297 |
| 298 // CacheDir returns a CIPD cache directory path, based on a CLI context. |
| 299 // |
| 300 // It knows how to use CIPDCacheDir env var. May return empty string if cache |
| 301 // directory is not defined. |
| 302 func CacheDir(ctx context.Context) (string, error) { |
| 303 if cacheDirEnv := cli.LookupEnv(ctx, CIPDCacheDir); cacheDirEnv.Exists { |
| 304 cacheDir := cacheDirEnv.Value |
| 305 if cacheDir != "" && !filepath.IsAbs(cacheDir) { |
| 306 return "", fmt.Errorf("bad %s: not an absolute path - %s
", CIPDCacheDir, cacheDir) |
| 307 } |
| 308 return cacheDir, nil |
| 309 } |
| 310 return "", nil |
| 311 } |
| 312 |
| 294 //////////////////////////////////////////////////////////////////////////////// | 313 //////////////////////////////////////////////////////////////////////////////// |
| 295 // inputOptions mixin. | 314 // inputOptions mixin. |
| 296 | 315 |
| 297 // packageVars holds array of '-pkg-var' command line options. | 316 // packageVars holds array of '-pkg-var' command line options. |
| 298 type packageVars map[string]string | 317 type packageVars map[string]string |
| 299 | 318 |
| 300 func (vars *packageVars) String() string { | 319 func (vars *packageVars) String() string { |
| 301 // String() for empty vars used in -help output. | 320 // String() for empty vars used in -help output. |
| 302 if len(*vars) == 0 { | 321 if len(*vars) == 0 { |
| 303 return "key:value" | 322 return "key:value" |
| (...skipping 1925 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2229 // subcommand invocations look more natural. | 2248 // subcommand invocations look more natural. |
| 2230 // | 2249 // |
| 2231 // Compare: | 2250 // Compare: |
| 2232 // * Default: cipd set-ref -ref=abc -version=def package/name | 2251 // * Default: cipd set-ref -ref=abc -version=def package/name |
| 2233 // * Improved: cipd set-ref package/name -ref=abc -version=def | 2252 // * Improved: cipd set-ref package/name -ref=abc -version=def |
| 2234 // | 2253 // |
| 2235 // Much better. | 2254 // Much better. |
| 2236 func Main(params Parameters, args []string) int { | 2255 func Main(params Parameters, args []string) int { |
| 2237 return subcommands.Run(GetApplication(params), fixFlagsPosition(args)) | 2256 return subcommands.Run(GetApplication(params), fixFlagsPosition(args)) |
| 2238 } | 2257 } |
| OLD | NEW |