From 29953f18362be01851b29839541009487666abc6 Mon Sep 17 00:00:00 2001 From: Lucas Roesler Date: Mon, 24 Oct 2022 20:12:08 +0200 Subject: [PATCH] fix: set the client user agent The code already existed for setting the UserAgent header, but a value was never passed to the client. This change will now set a string matching the following format ``` faas-cli/ ``` For example: ``` faas-cli/0.14.6-5-ga9c2b10f ``` Signed-off-by: Lucas Roesler --- proxy/client.go | 15 +++++++++------ version/version.go | 2 -- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/proxy/client.go b/proxy/client.go index b5df3600..2d9ab088 100644 --- a/proxy/client.go +++ b/proxy/client.go @@ -11,9 +11,11 @@ import ( gopath "path" "strings" "time" + + "github.com/openfaas/faas-cli/version" ) -//Client an API client to perform all operations +// Client an API client to perform all operations type Client struct { httpClient *http.Client //ClientAuth a type implementing ClientAuth interface for client authentication @@ -24,13 +26,13 @@ type Client struct { UserAgent string } -//ClientAuth an interface for client authentication. +// ClientAuth an interface for client authentication. // to add authentication to the client implement this interface type ClientAuth interface { Set(req *http.Request) error } -//NewClient initializes a new API client +// NewClient initializes a new API client func NewClient(auth ClientAuth, gatewayURL string, transport http.RoundTripper, timeout *time.Duration) (*Client, error) { gatewayURL = strings.TrimRight(gatewayURL, "/") baseURL, err := url.Parse(gatewayURL) @@ -51,10 +53,11 @@ func NewClient(auth ClientAuth, gatewayURL string, transport http.RoundTripper, ClientAuth: auth, httpClient: client, GatewayURL: baseURL, + UserAgent: fmt.Sprintf("faas-cli/%s", version.BuildVersion()), }, nil } -//newRequest create a new HTTP request with authentication +// newRequest create a new HTTP request with authentication func (c *Client) newRequest(method, path string, query url.Values, body io.Reader) (*http.Request, error) { // deep copy gateway url and then add the supplied path and args to the copy so that @@ -85,7 +88,7 @@ func (c *Client) newRequest(method, path string, query url.Values, body io.Reade return req, err } -//doRequest perform an HTTP request with context +// doRequest perform an HTTP request with context func (c *Client) doRequest(ctx context.Context, req *http.Request) (*http.Response, error) { req = req.WithContext(ctx) @@ -123,7 +126,7 @@ func addQueryParams(u string, params map[string]string) (string, error) { return parsedURL.String(), nil } -//AddCheckRedirect add CheckRedirect to the client +// AddCheckRedirect add CheckRedirect to the client func (c *Client) AddCheckRedirect(checkRedirect func(*http.Request, []*http.Request) error) { c.httpClient.CheckRedirect = checkRedirect } diff --git a/version/version.go b/version/version.go index 62911040..19253fa9 100644 --- a/version/version.go +++ b/version/version.go @@ -1,7 +1,5 @@ package version -const UserAgent = "OpenFaaS CLI" - var ( Version, GitCommit string )