...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package transport
16
17 import (
18 "fmt"
19 "net/http"
20 "runtime/debug"
21 )
22
23 var (
24
25
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
50 return Version
51 }
52
53 info, ok := debug.ReadBuildInfo()
54 if !ok {
55 return ""
56 }
57
58
59 if info.Main.Path == moduleName {
60 return info.Main.Version
61 }
62
63
64 for _, dep := range info.Deps {
65 if dep.Path == moduleName {
66 return dep.Version
67 }
68 }
69
70 return ""
71 }
72
73
74
75
76
77
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
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