Moving monocular client to internal and adding user agent to version pkg

Signed-off-by: Matt Farina <matt@mattfarina.com>
pull/6187/head
Matt Farina 6 years ago
parent 2613c3cda3
commit 2d4ced9090
No known key found for this signature in database
GPG Key ID: 9436E80BFBA46909

@ -19,9 +19,6 @@ package monocular
import ( import (
"errors" "errors"
"net/url" "net/url"
"strings"
"helm.sh/helm/internal/version"
) )
// ErrHostnameNotProvided indicates the url is missing a hostname // 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. // Client represents a client capable of communicating with the Monocular API.
type Client struct { type Client struct {
// The user agent to identify as when making requests
UserAgent string
// The base URL for requests // The base URL for requests
BaseURL string BaseURL string
@ -48,7 +43,6 @@ func New(u string) (*Client, error) {
} }
return &Client{ return &Client{
UserAgent: "Helm/" + strings.TrimPrefix(version.GetVersion(), "v"),
BaseURL: u, BaseURL: u,
Log: nopLogger, Log: nopLogger,
}, nil }, nil

@ -17,10 +17,7 @@ limitations under the License.
package monocular package monocular
import ( import (
"strings"
"testing" "testing"
"helm.sh/helm/internal/version"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
@ -31,9 +28,4 @@ func TestNew(t *testing.T) {
if c.BaseURL != "https://hub.helm.sh" { if c.BaseURL != "https://hub.helm.sh" {
t.Errorf("incorrect BaseURL. Expected \"https://hub.helm.sh\" but got %q", c.BaseURL) 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)
}
} }

@ -24,23 +24,27 @@ import (
"path" "path"
"time" "time"
"helm.sh/helm/internal/version"
"helm.sh/helm/pkg/chart" "helm.sh/helm/pkg/chart"
) )
// The structs below represent the structure of the response from the monocular // 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 // SearchResult represents an individual chart result
type SearchResult struct { type SearchResult struct {
ID string `json:"id"` ID string `json:"id"`
Type string `json:"type"` Type string `json:"type"`
Attributes Attributes `json:"attributes"` Attributes Chart `json:"attributes"`
Links Links `json:"links"` Links Links `json:"links"`
Relationships Relationships `json:"relationships"` Relationships Relationships `json:"relationships"`
} }
// Attributes is the attributes for the chart // Chart is the attributes for the chart
type Attributes struct { type Chart struct {
Name string `json:"name"` Name string `json:"name"`
Repo Repo `json:"repo"` Repo Repo `json:"repo"`
Description string `json:"description"` Description string `json:"description"`
@ -69,12 +73,12 @@ type Relationships struct {
// LatestChartVersion provides the details on the latest version of the chart // LatestChartVersion provides the details on the latest version of the chart
type LatestChartVersion struct { type LatestChartVersion struct {
Data Data `json:"data"` Data ChartVersion `json:"data"`
Links Links `json:"links"` Links Links `json:"links"`
} }
// Data provides the specific data on the chart version // ChartVersion provides the specific data on the chart version
type Data struct { type ChartVersion struct {
Version string `json:"version"` Version string `json:"version"`
AppVersion string `json:"app_version"` AppVersion string `json:"app_version"`
Created time.Time `json:"created"` 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 // Set the user agent so that monocular can identify where the request
// is coming from // is coming from
req.Header.Set("User-Agent", c.UserAgent) req.Header.Set("User-Agent", version.GetUserAgent())
res, err := http.DefaultClient.Do(req) res, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {

@ -19,6 +19,7 @@ package version // import "helm.sh/helm/internal/version"
import ( import (
"flag" "flag"
"runtime" "runtime"
"strings"
) )
var ( var (
@ -59,6 +60,11 @@ func GetVersion() string {
return version + "+" + metadata 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 // Get returns build info
func Get() BuildInfo { func Get() BuildInfo {
v := BuildInfo{ v := BuildInfo{

Loading…
Cancel
Save