From 2d4ced90908895706d41ed5784587a30ec25cf3f Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Fri, 9 Aug 2019 15:02:42 -0400 Subject: [PATCH] Moving monocular client to internal and adding user agent to version pkg Signed-off-by: Matt Farina --- {pkg => internal}/monocular/client.go | 10 ++-------- {pkg => internal}/monocular/client_test.go | 8 -------- {pkg => internal}/monocular/doc.go | 0 {pkg => internal}/monocular/search.go | 22 +++++++++++++--------- {pkg => internal}/monocular/search_test.go | 0 internal/version/version.go | 6 ++++++ 6 files changed, 21 insertions(+), 25 deletions(-) rename {pkg => internal}/monocular/client.go (86%) rename {pkg => internal}/monocular/client_test.go (80%) rename {pkg => internal}/monocular/doc.go (100%) rename {pkg => internal}/monocular/search.go (84%) rename {pkg => internal}/monocular/search_test.go (100%) diff --git a/pkg/monocular/client.go b/internal/monocular/client.go similarity index 86% rename from pkg/monocular/client.go rename to internal/monocular/client.go index 1d4ad3f30..88a2564b9 100644 --- a/pkg/monocular/client.go +++ b/internal/monocular/client.go @@ -19,9 +19,6 @@ package monocular import ( "errors" "net/url" - "strings" - - "helm.sh/helm/internal/version" ) // ErrHostnameNotProvided indicates the url is missing a hostname @@ -29,8 +26,6 @@ var ErrHostnameNotProvided = errors.New("no hostname provided") // Client represents a client capable of communicating with the Monocular API. type Client struct { - // The user agent to identify as when making requests - UserAgent string // The base URL for requests BaseURL string @@ -48,9 +43,8 @@ func New(u string) (*Client, error) { } return &Client{ - UserAgent: "Helm/" + strings.TrimPrefix(version.GetVersion(), "v"), - BaseURL: u, - Log: nopLogger, + BaseURL: u, + Log: nopLogger, }, nil } diff --git a/pkg/monocular/client_test.go b/internal/monocular/client_test.go similarity index 80% rename from pkg/monocular/client_test.go rename to internal/monocular/client_test.go index 2f9e7e22f..abf914ef5 100644 --- a/pkg/monocular/client_test.go +++ b/internal/monocular/client_test.go @@ -17,10 +17,7 @@ limitations under the License. package monocular import ( - "strings" "testing" - - "helm.sh/helm/internal/version" ) func TestNew(t *testing.T) { @@ -31,9 +28,4 @@ func TestNew(t *testing.T) { if c.BaseURL != "https://hub.helm.sh" { t.Errorf("incorrect BaseURL. Expected \"https://hub.helm.sh\" but got %q", c.BaseURL) } - - ua := "Helm/" + strings.TrimPrefix(version.GetVersion(), "v") - if c.UserAgent != ua { - t.Errorf("incorrect user agent. Expected %q but got %q", ua, c.UserAgent) - } } diff --git a/pkg/monocular/doc.go b/internal/monocular/doc.go similarity index 100% rename from pkg/monocular/doc.go rename to internal/monocular/doc.go diff --git a/pkg/monocular/search.go b/internal/monocular/search.go similarity index 84% rename from pkg/monocular/search.go rename to internal/monocular/search.go index 40d8d2ed7..3f448394c 100644 --- a/pkg/monocular/search.go +++ b/internal/monocular/search.go @@ -24,23 +24,27 @@ import ( "path" "time" + "helm.sh/helm/internal/version" "helm.sh/helm/pkg/chart" ) // The structs below represent the structure of the response from the monocular -// search API. +// search API. The structs were not imported from monocular because monocular +// imports from Helm v2 (avoiding circular version dependency) and the mappings +// are slightly different (monocular search results do not directly reflect +// the struct definitions). // SearchResult represents an individual chart result type SearchResult struct { ID string `json:"id"` Type string `json:"type"` - Attributes Attributes `json:"attributes"` + Attributes Chart `json:"attributes"` Links Links `json:"links"` Relationships Relationships `json:"relationships"` } -// Attributes is the attributes for the chart -type Attributes struct { +// Chart is the attributes for the chart +type Chart struct { Name string `json:"name"` Repo Repo `json:"repo"` Description string `json:"description"` @@ -69,12 +73,12 @@ type Relationships struct { // LatestChartVersion provides the details on the latest version of the chart type LatestChartVersion struct { - Data Data `json:"data"` - Links Links `json:"links"` + Data ChartVersion `json:"data"` + Links Links `json:"links"` } -// Data provides the specific data on the chart version -type Data struct { +// ChartVersion provides the specific data on the chart version +type ChartVersion struct { Version string `json:"version"` AppVersion string `json:"app_version"` Created time.Time `json:"created"` @@ -108,7 +112,7 @@ func (c *Client) Search(term string) ([]SearchResult, error) { // Set the user agent so that monocular can identify where the request // is coming from - req.Header.Set("User-Agent", c.UserAgent) + req.Header.Set("User-Agent", version.GetUserAgent()) res, err := http.DefaultClient.Do(req) if err != nil { diff --git a/pkg/monocular/search_test.go b/internal/monocular/search_test.go similarity index 100% rename from pkg/monocular/search_test.go rename to internal/monocular/search_test.go diff --git a/internal/version/version.go b/internal/version/version.go index d7caa11f5..394476039 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -19,6 +19,7 @@ package version // import "helm.sh/helm/internal/version" import ( "flag" "runtime" + "strings" ) var ( @@ -59,6 +60,11 @@ func GetVersion() string { return version + "+" + metadata } +// GetUserAgent returns a user agent for user with an HTTP client +func GetUserAgent() string { + return "Helm/" + strings.TrimPrefix(GetVersion(), "v") +} + // Get returns build info func Get() BuildInfo { v := BuildInfo{