mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-02 12:17:39 -06:00
4c254cc2be
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package httpclient
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform/version"
|
|
)
|
|
|
|
const userAgentFormat = "Terraform/%s"
|
|
const uaEnvVar = "TF_APPEND_USER_AGENT"
|
|
|
|
// Deprecated: Use UserAgent(version) instead
|
|
func UserAgentString() string {
|
|
ua := fmt.Sprintf(userAgentFormat, version.Version)
|
|
|
|
if add := os.Getenv(uaEnvVar); add != "" {
|
|
add = strings.TrimSpace(add)
|
|
if len(add) > 0 {
|
|
ua += " " + add
|
|
log.Printf("[DEBUG] Using modified User-Agent: %s", ua)
|
|
}
|
|
}
|
|
|
|
return ua
|
|
}
|
|
|
|
type userAgentRoundTripper struct {
|
|
inner http.RoundTripper
|
|
userAgent string
|
|
}
|
|
|
|
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
if _, ok := req.Header["User-Agent"]; !ok {
|
|
req.Header.Set("User-Agent", rt.userAgent)
|
|
}
|
|
log.Printf("[TRACE] HTTP client %s request to %s", req.Method, req.URL.String())
|
|
return rt.inner.RoundTrip(req)
|
|
}
|
|
|
|
func TerraformUserAgent(version string) string {
|
|
ua := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io)", version)
|
|
|
|
if add := os.Getenv(uaEnvVar); add != "" {
|
|
add = strings.TrimSpace(add)
|
|
if len(add) > 0 {
|
|
ua += " " + add
|
|
log.Printf("[DEBUG] Using modified User-Agent: %s", ua)
|
|
}
|
|
}
|
|
|
|
return ua
|
|
}
|