...

Source file src/github.com/google/go-containerregistry/pkg/v1/remote/transport/useragent.go

Documentation: github.com/google/go-containerregistry/pkg/v1/remote/transport

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package transport
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"runtime/debug"
    21  )
    22  
    23  var (
    24  	// Version can be set via:
    25  	// -ldflags="-X 'github.com/google/go-containerregistry/pkg/v1/remote/transport.Version=$TAG'"
    26  	Version string
    27  
    28  	ggcrVersion = defaultUserAgent
    29  )
    30  
    31  const (
    32  	defaultUserAgent = "go-containerregistry"
    33  	moduleName       = "github.com/google/go-containerregistry"
    34  )
    35  
    36  type userAgentTransport struct {
    37  	inner http.RoundTripper
    38  	ua    string
    39  }
    40  
    41  func init() {
    42  	if v := version(); v != "" {
    43  		ggcrVersion = fmt.Sprintf("%s/%s", defaultUserAgent, v)
    44  	}
    45  }
    46  
    47  func version() string {
    48  	if Version != "" {
    49  		// Version was set via ldflags, just return it.
    50  		return Version
    51  	}
    52  
    53  	info, ok := debug.ReadBuildInfo()
    54  	if !ok {
    55  		return ""
    56  	}
    57  
    58  	// Happens for crane and gcrane.
    59  	if info.Main.Path == moduleName {
    60  		return info.Main.Version
    61  	}
    62  
    63  	// Anything else.
    64  	for _, dep := range info.Deps {
    65  		if dep.Path == moduleName {
    66  			return dep.Version
    67  		}
    68  	}
    69  
    70  	return ""
    71  }
    72  
    73  // NewUserAgent returns an http.Roundtripper that sets the user agent to
    74  // The provided string plus additional go-containerregistry information,
    75  // e.g. if provided "crane/v0.1.4" and this modules was built at v0.1.4:
    76  //
    77  // User-Agent: crane/v0.1.4 go-containerregistry/v0.1.4
    78  func NewUserAgent(inner http.RoundTripper, ua string) http.RoundTripper {
    79  	if ua == "" {
    80  		ua = ggcrVersion
    81  	} else {
    82  		ua = fmt.Sprintf("%s %s", ua, ggcrVersion)
    83  	}
    84  	return &userAgentTransport{
    85  		inner: inner,
    86  		ua:    ua,
    87  	}
    88  }
    89  
    90  // RoundTrip implements http.RoundTripper
    91  func (ut *userAgentTransport) RoundTrip(in *http.Request) (*http.Response, error) {
    92  	in.Header.Set("User-Agent", ut.ua)
    93  	return ut.inner.RoundTrip(in)
    94  }
    95  

View as plain text