From 0cdd07711463084ad3c11255ebf3baad57c16526 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 10 Feb 2016 15:15:11 -0700 Subject: [PATCH] fix(*): minor codebase cleanups Cleaned up some of the code we inherited from DM. --- cmd/helm/helm.go | 2 +- dm/client.go | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 693e192eb..84ce287e0 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -29,7 +29,7 @@ func main() { Name: "host,u", Usage: "The URL of the DM server.", EnvVar: "HELM_HOST", - Value: "https://localhost:8181/FIXME_NOT_RIGHT", + Value: "https://localhost:8000/", }, cli.IntFlag{ Name: "timeout", diff --git a/dm/client.go b/dm/client.go index e03989538..acb824ffd 100644 --- a/dm/client.go +++ b/dm/client.go @@ -85,6 +85,10 @@ func (c *Client) url(rawurl string) (string, error) { return c.baseURL.ResolveReference(u).String(), nil } +func (c *Client) agent() string { + return fmt.Sprintf("helm/%s", "0.0.1") +} + // CallService is a low-level function for making an API call. // // This calls the service and then unmarshals the returned data into dest. @@ -109,29 +113,28 @@ func (c *Client) callHTTP(path, method, action string, reader io.ReadCloser) (st request, err := http.NewRequest(method, path, reader) // TODO: dynamically set version - request.Header.Set("User-Agent", "helm/0.0.1") + request.Header.Set("User-Agent", c.agent()) request.Header.Add("Content-Type", "application/json") - client := http.Client{ - Timeout: time.Duration(time.Duration(DefaultHTTPTimeout) * time.Second), + client := &http.Client{ + Timeout: c.HTTPTimeout, Transport: c.transport(), } response, err := client.Do(request) if err != nil { - return "", fmt.Errorf("cannot %s: %s\n", action, err) + return "", err } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) if err != nil { - return "", fmt.Errorf("cannot %s: %s\n", action, err) + return "", err } - if response.StatusCode < http.StatusOK || - response.StatusCode >= http.StatusMultipleChoices { - message := fmt.Sprintf("status code: %d status: %s : %s", response.StatusCode, response.Status, body) - return "", fmt.Errorf("cannot %s: %s\n", action, message) + s := response.StatusCode + if s < http.StatusOK || s >= http.StatusMultipleChoices { + return "", fmt.Errorf("request '%s %s' failed with %d: %s\n", action, path, s, body) } return string(body), nil @@ -194,9 +197,10 @@ func (c *Client) DeployChart(filename, deployname string) error { request.Header.Add("Content-Encoding", "gzip") request.Header.Add("X-Deployment-Name", deployname) request.Header.Add("X-Chart-Name", filepath.Base(filename)) + request.Header.Set("User-Agent", c.agent()) - client := http.Client{ - Timeout: time.Duration(time.Duration(DefaultHTTPTimeout) * time.Second), + client := &http.Client{ + Timeout: c.HTTPTimeout, Transport: c.transport(), }