-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Move user config and repo config to Viper #1751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
938e7d6
34eec90
a9c4648
38da40f
89e5378
7d25c84
ccb6b84
17d671a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,9 @@ import ( | |
type ApiClient struct { | ||
// The api's base URL | ||
baseUrl string | ||
Token string | ||
token string | ||
turboVersion string | ||
// Number of failed requests before we stop trying to upload/download artifacts to the remote cache | ||
maxRemoteFailCount uint64 | ||
|
||
// Must be used via atomic package | ||
currentFailCount uint64 | ||
// An http client | ||
|
@@ -40,17 +39,33 @@ type ApiClient struct { | |
// ErrTooManyFailures is returned from remote cache API methods after `maxRemoteFailCount` errors have occurred | ||
var ErrTooManyFailures = errors.New("skipping HTTP Request, too many failures have occurred") | ||
|
||
// _maxRemoteFailCount is the number of failed requests before we stop trying to upload/download | ||
// artifacts to the remote cache | ||
const _maxRemoteFailCount = uint64(3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm embarrassed to admit that I just learned that "unsigned int" has nothing to do with cryptography 😬 |
||
|
||
// SetToken updates the ApiClient's Token | ||
func (c *ApiClient) SetToken(token string) { | ||
c.Token = token | ||
c.token = token | ||
} | ||
|
||
// RemoteConfig holds the authentication and endpoint details for the API client | ||
type RemoteConfig struct { | ||
Token string | ||
TeamID string | ||
TeamSlug string | ||
APIURL string | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏🏾 I like it |
||
|
||
// Opts holds values for configuring the behavior of the API client | ||
type Opts struct { | ||
UsePreflight bool | ||
} | ||
|
||
// New creates a new ApiClient | ||
func NewClient(baseURL string, logger hclog.Logger, turboVersion string, teamID string, teamSlug string, maxRemoteFailCount uint64, usePreflight bool) *ApiClient { | ||
func NewClient(remoteConfig RemoteConfig, logger hclog.Logger, turboVersion string, opts Opts) *ApiClient { | ||
client := &ApiClient{ | ||
baseUrl: baseURL, | ||
turboVersion: turboVersion, | ||
maxRemoteFailCount: maxRemoteFailCount, | ||
baseUrl: remoteConfig.APIURL, | ||
turboVersion: turboVersion, | ||
HttpClient: &retryablehttp.Client{ | ||
HTTPClient: &http.Client{ | ||
Timeout: time.Duration(20 * time.Second), | ||
|
@@ -61,17 +76,18 @@ func NewClient(baseURL string, logger hclog.Logger, turboVersion string, teamID | |
Backoff: retryablehttp.DefaultBackoff, | ||
Logger: logger, | ||
}, | ||
teamID: teamID, | ||
teamSlug: teamSlug, | ||
usePreflight: usePreflight, | ||
token: remoteConfig.Token, | ||
teamID: remoteConfig.TeamID, | ||
teamSlug: remoteConfig.TeamSlug, | ||
usePreflight: opts.UsePreflight, | ||
} | ||
client.HttpClient.CheckRetry = client.checkRetry | ||
return client | ||
} | ||
|
||
// IsLoggedIn returns true if this ApiClient has a credential (token) | ||
// IsLoggedIn returns true if this ApiClient has appropriate credentials | ||
func (c *ApiClient) IsLoggedIn() bool { | ||
return c.Token != "" | ||
return c.token != "" && (c.teamID != "" || c.teamSlug != "") | ||
} | ||
|
||
// SetTeamID sets the team parameter used on all requests by this client | ||
|
@@ -133,7 +149,7 @@ func (c *ApiClient) checkRetry(ctx context.Context, resp *http.Response, err err | |
// okToRequest returns nil if it's ok to make a request, and returns the error to | ||
// return to the caller if a request is not allowed | ||
func (c *ApiClient) okToRequest() error { | ||
if atomic.LoadUint64(&c.currentFailCount) < c.maxRemoteFailCount { | ||
if atomic.LoadUint64(&c.currentFailCount) < _maxRemoteFailCount { | ||
return nil | ||
} | ||
return ErrTooManyFailures | ||
|
@@ -153,7 +169,7 @@ func (c *ApiClient) doPreflight(requestURL string, requestMethod string, request | |
req.Header.Set("User-Agent", c.UserAgent()) | ||
req.Header.Set("Access-Control-Request-Method", requestMethod) | ||
req.Header.Set("Access-Control-Request-Headers", requestHeaders) | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
if err != nil { | ||
return nil, requestURL, fmt.Errorf("[WARNING] Invalid cache URL: %w", err) | ||
} | ||
|
@@ -246,7 +262,7 @@ func (c *ApiClient) PutArtifact(hash string, artifactBody []byte, duration int, | |
req.Header.Set("Content-Type", "application/octet-stream") | ||
req.Header.Set("x-artifact-duration", fmt.Sprintf("%v", duration)) | ||
if allowAuth { | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
} | ||
req.Header.Set("User-Agent", c.UserAgent()) | ||
if tag != "" { | ||
|
@@ -295,7 +311,7 @@ func (c *ApiClient) FetchArtifact(hash string) (*http.Response, error) { | |
|
||
req, err := retryablehttp.NewRequest(http.MethodGet, requestURL, nil) | ||
if allowAuth { | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
} | ||
req.Header.Set("User-Agent", c.UserAgent()) | ||
if err != nil { | ||
|
@@ -346,7 +362,7 @@ func (c *ApiClient) RecordAnalyticsEvents(events []map[string]interface{}) error | |
} | ||
req.Header.Set("Content-Type", "application/json") | ||
if allowAuth { | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
} | ||
req.Header.Set("User-Agent", c.UserAgent()) | ||
resp, err := c.HttpClient.Do(req) | ||
|
@@ -409,7 +425,7 @@ func (c *ApiClient) GetTeams() (*TeamsResponse, error) { | |
|
||
req.Header.Set("User-Agent", c.UserAgent()) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
resp, err := c.HttpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -442,7 +458,7 @@ func (c *ApiClient) GetTeam(teamID string) (*Team, error) { | |
} | ||
req.Header.Set("User-Agent", c.UserAgent()) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
resp, err := c.HttpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -494,7 +510,7 @@ func (c *ApiClient) GetUser() (*UserResponse, error) { | |
|
||
req.Header.Set("User-Agent", c.UserAgent()) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
resp, err := c.HttpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -534,7 +550,7 @@ func (c *ApiClient) GetCachingStatus() (util.CachingStatus, error) { | |
} | ||
req.Header.Set("User-Agent", c.UserAgent()) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
resp, err := c.HttpClient.Do(req) | ||
if err != nil { | ||
return util.CachingStatusDisabled, err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,12 @@ func Test_sendToServer(t *testing.T) { | |
})) | ||
defer ts.Close() | ||
|
||
apiClient := NewClient(ts.URL, hclog.Default(), "v1", "", "my-team-slug", 1, false) | ||
apiClient.SetToken("my-token") | ||
remoteConfig := RemoteConfig{ | ||
TeamSlug: "my-team-slug", | ||
APIURL: ts.URL, | ||
Token: "my-token", | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not blocking: may be worth moving to a top level constant since it doesn't change between tests. |
||
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{}) | ||
|
||
myUUID, err := uuid.NewUUID() | ||
if err != nil { | ||
|
@@ -81,8 +85,12 @@ func Test_PutArtifact(t *testing.T) { | |
defer ts.Close() | ||
|
||
// Set up test expected values | ||
apiClient := NewClient(ts.URL+"/hash", hclog.Default(), "v1", "", "my-team-slug", 1, false) | ||
apiClient.SetToken("my-token") | ||
remoteConfig := RemoteConfig{ | ||
TeamSlug: "my-team-slug", | ||
APIURL: ts.URL, | ||
Token: "my-token", | ||
} | ||
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{}) | ||
expectedArtifactBody := []byte("My string artifact") | ||
|
||
// Test Put Artifact | ||
|
@@ -103,8 +111,12 @@ func Test_PutWhenCachingDisabled(t *testing.T) { | |
defer ts.Close() | ||
|
||
// Set up test expected values | ||
apiClient := NewClient(ts.URL+"/hash", hclog.Default(), "v1", "", "my-team-slug", 1, false) | ||
apiClient.SetToken("my-token") | ||
remoteConfig := RemoteConfig{ | ||
TeamSlug: "my-team-slug", | ||
APIURL: ts.URL, | ||
Token: "my-token", | ||
} | ||
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{}) | ||
expectedArtifactBody := []byte("My string artifact") | ||
// Test Put Artifact | ||
err := apiClient.PutArtifact("hash", expectedArtifactBody, 500, "") | ||
|
@@ -126,8 +138,12 @@ func Test_FetchWhenCachingDisabled(t *testing.T) { | |
defer ts.Close() | ||
|
||
// Set up test expected values | ||
apiClient := NewClient(ts.URL+"/hash", hclog.Default(), "v1", "", "my-team-slug", 1, false) | ||
apiClient.SetToken("my-token") | ||
remoteConfig := RemoteConfig{ | ||
TeamSlug: "my-team-slug", | ||
APIURL: ts.URL, | ||
Token: "my-token", | ||
} | ||
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{}) | ||
// Test Put Artifact | ||
resp, err := apiClient.FetchArtifact("hash") | ||
cd := &util.CacheDisabledError{} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beginner Go question: I know uppercasing is how you export from a package, does lowercasing a struct property have any related effects in terms of public/private? I see you're using
.token
for both getters and setters later, so there probably isn't, but then I'm curious why it was originally uppercased, and why you changed it here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has the same effect as struct and function names.
.token
is now only visible in theconfig
package. All of the other usages, including the tests, are in theconfig
package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. I thought it would be private to the struct, rather than private to the package. Thanks!