github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/httpclient/useragent.go (about) 1 package httpclient 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "os" 8 "strings" 9 10 "github.com/hashicorp/terraform/version" 11 ) 12 13 const userAgentFormat = "Terraform/%s" 14 const uaEnvVar = "TF_APPEND_USER_AGENT" 15 16 // Deprecated: Use TerraformUserAgent(version) instead 17 func UserAgentString() string { 18 ua := fmt.Sprintf(userAgentFormat, version.Version) 19 20 if add := os.Getenv(uaEnvVar); add != "" { 21 add = strings.TrimSpace(add) 22 if len(add) > 0 { 23 ua += " " + add 24 log.Printf("[DEBUG] Using modified User-Agent: %s", ua) 25 } 26 } 27 28 return ua 29 } 30 31 type userAgentRoundTripper struct { 32 inner http.RoundTripper 33 userAgent string 34 } 35 36 func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { 37 if _, ok := req.Header["User-Agent"]; !ok { 38 req.Header.Set("User-Agent", rt.userAgent) 39 } 40 log.Printf("[TRACE] HTTP client %s request to %s", req.Method, req.URL.String()) 41 return rt.inner.RoundTrip(req) 42 } 43 44 func TerraformUserAgent(version string) string { 45 ua := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io)", version) 46 47 if add := os.Getenv(uaEnvVar); add != "" { 48 add = strings.TrimSpace(add) 49 if len(add) > 0 { 50 ua += " " + add 51 log.Printf("[DEBUG] Using modified User-Agent: %s", ua) 52 } 53 } 54 55 return ua 56 }