From 78317836ec1f105f0ba3875ccf808a8a778a7df2 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 18 May 2015 16:29:38 -0700 Subject: [PATCH] Use AddUserAgent function and make appropriate changes in mc --- Godeps/Godeps.json | 2 +- .../github.com/minio/objectstorage-go/api.go | 19 ++++++++++++------- .../objectstorage-go/low_level_request.go | 4 +++- common-methods.go | 5 +++++ pkg/client/s3/s3.go | 4 ++++ 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 0a08ca37..53041105 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -36,7 +36,7 @@ }, { "ImportPath": "github.com/minio/objectstorage-go", - "Rev": "cb9c9c50825bf5c0758ed9e2f8b8a1414d21b3d2" + "Rev": "e0670aef1a63a46d0dc5dc2dba2088f55b73b4b5" }, { "ImportPath": "github.com/shiena/ansicolor", diff --git a/Godeps/_workspace/src/github.com/minio/objectstorage-go/api.go b/Godeps/_workspace/src/github.com/minio/objectstorage-go/api.go index 546ff1db..276db7cb 100644 --- a/Godeps/_workspace/src/github.com/minio/objectstorage-go/api.go +++ b/Godeps/_workspace/src/github.com/minio/objectstorage-go/api.go @@ -20,7 +20,6 @@ import ( "errors" "io" "net/http" - "runtime" "sort" "strings" "time" @@ -133,8 +132,10 @@ type Config struct { // Advanced options AcceptType string // specify this to get server response in non XML style if server supports it - UserAgent string // user override useful when objectstorage-go is used with in your application Transport http.RoundTripper // custom transport usually for debugging, by default its nil + // internal + // use AddUserAgent append to default, useful when objectstorage-go is used with in your application + userAgent string } // MustGetEndpoint makes sure that a valid endpoint is provided all the time, even with false regions it will fall @@ -163,18 +164,22 @@ func (c *Config) MustGetEndpoint() string { return getEndpoint(c.Region) } +// AddUserAgent - append to a default user agent +func (c *Config) AddUserAgent(name string, version string, comments ...string) { + if name != "" && version != "" { + newUserAgent := name + "/" + version + " (" + strings.Join(comments, ", ") + ") " + c.userAgent = c.userAgent + " " + newUserAgent + } +} + // Global constants const ( - LibraryName = "objectstorage-go/" + LibraryName = "objectstorage-go" LibraryVersion = "0.1" ) // New - instantiate a new minio api client func New(config *Config) API { - // if not UserAgent provided set it to default - if strings.TrimSpace(config.UserAgent) == "" { - config.UserAgent = LibraryName + " (" + LibraryVersion + "; " + runtime.GOOS + "; " + runtime.GOARCH + ")" - } return &api{&lowLevelAPI{config}} } diff --git a/Godeps/_workspace/src/github.com/minio/objectstorage-go/low_level_request.go b/Godeps/_workspace/src/github.com/minio/objectstorage-go/low_level_request.go index 57a76095..e15dd0c6 100644 --- a/Godeps/_workspace/src/github.com/minio/objectstorage-go/low_level_request.go +++ b/Godeps/_workspace/src/github.com/minio/objectstorage-go/low_level_request.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "net/http" "net/url" + "runtime" "sort" "strings" "time" @@ -76,7 +77,8 @@ func newRequest(op *operation, config *Config, body io.ReadSeeker) (*request, er } // set UserAgent - req.Header.Set("User-Agent", config.UserAgent) + config.userAgent = config.userAgent + LibraryName + "/" + LibraryVersion + " (" + runtime.GOOS + ", " + runtime.GOARCH + ") " + req.Header.Set("User-Agent", config.userAgent) // set Accept header for response encoding style, if available if config.AcceptType != "" { diff --git a/common-methods.go b/common-methods.go index 8ef9fe2f..90fabb40 100644 --- a/common-methods.go +++ b/common-methods.go @@ -20,6 +20,8 @@ import ( "encoding/base64" "encoding/hex" "io" + "os" + "runtime" "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/client/fs" @@ -68,6 +70,9 @@ func getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Clie s3Config := new(s3.Config) s3Config.AccessKeyID = auth.AccessKeyID s3Config.SecretAccessKey = auth.SecretAccessKey + s3Config.AppName = "Minio" + s3Config.AppVersion = Version + s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH} s3Config.HostURL = urlStr s3Config.Debug = debug return s3.New(s3Config), nil diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index bf55ca19..59446b4f 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -34,6 +34,9 @@ type Config struct { AccessKeyID string SecretAccessKey string HostURL string + AppName string + AppVersion string + AppComments []string Debug bool // Used for SSL transport layer @@ -88,6 +91,7 @@ func New(config *Config) client.Client { objectstorageConf.AccessKeyID = config.AccessKeyID objectstorageConf.SecretAccessKey = config.SecretAccessKey objectstorageConf.Transport = transport + objectstorageConf.AddUserAgent(config.AppName, config.AppVersion, config.AppComments...) objectstorageConf.Region = getRegion(u.Host) objectstorageConf.Endpoint = u.Scheme + "://" + u.Host api := objectstorage.New(objectstorageConf)