Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8048)

Unified Diff: cipd/client/cli/main.go

Issue 2746363007: cipd2isolate: Initial CLI parsing boilerplate. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | cipd/client/cmd/cipd2isolate/isolate.go » ('j') | cipd/client/cmd/cipd2isolate/main.go » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cipd/client/cli/main.go
diff --git a/cipd/client/cli/main.go b/cipd/client/cli/main.go
index 7c6f6b178c25e723c4dcdb4b9d9aa77546909eac..373f81ec5e4d322eee79faba852825b3f78144c8 100644
--- a/cipd/client/cli/main.go
+++ b/cipd/client/cli/main.go
@@ -268,29 +268,48 @@ func (opts *clientOptions) makeCipdClient(ctx context.Context, root string) (cip
if err != nil {
return nil, err
}
- ua := cipd.UserAgent
- if prefix := cli.LookupEnv(ctx, CIPDHTTPUserAgentPrefix); prefix.Exists {
- ua = fmt.Sprintf("%s/%s", prefix.Value, ua)
- }
cacheDir := opts.cacheDir
if cacheDir == "" {
- if cacheDirEnv := cli.LookupEnv(ctx, CIPDCacheDir); cacheDirEnv.Exists {
- cacheDir = cacheDirEnv.Value
- if cacheDir != "" && !filepath.IsAbs(cacheDir) {
- return nil, fmt.Errorf("Bad %s: not an absolute path - %s", CIPDCacheDir, cacheDir)
- }
+ cacheDir, err = CacheDir(ctx)
+ if err != nil {
+ return nil, err
}
}
return cipd.NewClient(cipd.ClientOptions{
ServiceURL: opts.serviceURL,
Root: root,
- UserAgent: ua,
+ UserAgent: UserAgent(ctx),
CacheDir: cacheDir,
AuthenticatedClient: client,
AnonymousClient: http.DefaultClient,
})
}
+// UserAgent returns a CIPD user agent string, based on a CLI context.
+//
+// It knows how to use CIPDHTTPUserAgentPrefix env var.
+func UserAgent(ctx context.Context) string {
+ if prefix := cli.LookupEnv(ctx, CIPDHTTPUserAgentPrefix); prefix.Exists {
+ return fmt.Sprintf("%s/%s", prefix.Value, cipd.UserAgent)
+ }
+ return cipd.UserAgent
+}
+
+// CacheDir returns a CIPD cache directory path, based on a CLI context.
+//
+// It knows how to use CIPDCacheDir env var. May return empty string if cache
+// directory is not defined.
+func CacheDir(ctx context.Context) (string, error) {
+ if cacheDirEnv := cli.LookupEnv(ctx, CIPDCacheDir); cacheDirEnv.Exists {
+ cacheDir := cacheDirEnv.Value
+ if cacheDir != "" && !filepath.IsAbs(cacheDir) {
+ return "", fmt.Errorf("bad %s: not an absolute path - %s", CIPDCacheDir, cacheDir)
+ }
+ return cacheDir, nil
+ }
+ return "", nil
+}
+
////////////////////////////////////////////////////////////////////////////////
// inputOptions mixin.
« no previous file with comments | « no previous file | cipd/client/cmd/cipd2isolate/isolate.go » ('j') | cipd/client/cmd/cipd2isolate/main.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698