这是indexloc提供的服务,不要输入任何密码
Skip to content

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

Merged
merged 8 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/cmd/turbo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
c.ErrorWriter = os.Stderr
// Parse and validate cmd line flags and env vars
// Note that cf can be nil
cf, err := config.ParseAndValidate(c.Args, ui, turboVersion)
cf, err := config.ParseAndValidate(c.Args, ui, turboVersion, config.DefaultUserConfigPath())
if err != nil {
ui.Error(fmt.Sprintf("%s %s", uiPkg.ERROR_PREFIX, color.RedString(err.Error())))
os.Exit(1)
Expand Down
28 changes: 19 additions & 9 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ require (
github.com/mattn/go-isatty v0.0.14
github.com/mitchellh/cli v1.1.2
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.4.3
github.com/mitchellh/mapstructure v1.5.0
github.com/nightlyone/lockfile v1.0.0
github.com/pkg/errors v0.9.1
github.com/pyr-sh/dag v1.0.0
github.com/sabhiram/go-gitignore v0.0.0-20201211210132-54b8a0bf510f
github.com/schollz/progressbar/v3 v3.9.0
github.com/spf13/cobra v1.3.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.7.2
github.com/yookoala/realpath v1.0.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.46.0
google.golang.org/grpc v1.46.2
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.2.0
gotest.tools/v3 v3.3.0
muzzammil.xyz/jsonc v1.0.0
)

Expand All @@ -49,30 +50,39 @@ require (
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
64 changes: 52 additions & 12 deletions cli/go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cli/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func newSyncCache(opts Opts, config *config.Config, client client, recorder anal

if useHTTPCache {
fmt.Println(ui.Dim("• Remote computation caching enabled"))
implementation := newHTTPCache(opts, config, client, recorder, config.Cwd)
implementation := newHTTPCache(opts, config.RemoteConfig.TeamID, client, recorder, config.Cwd)
cacheImplementations = append(cacheImplementations, implementation)
}

Expand Down
5 changes: 2 additions & 3 deletions cli/internal/cache/cache_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"time"

"github.com/vercel/turborepo/cli/internal/analytics"
"github.com/vercel/turborepo/cli/internal/config"
"github.com/vercel/turborepo/cli/internal/fs"
)

Expand Down Expand Up @@ -329,7 +328,7 @@ func (cache *httpCache) CleanAll() {

func (cache *httpCache) Shutdown() {}

func newHTTPCache(opts Opts, config *config.Config, client client, recorder analytics.Recorder, repoRoot fs.AbsolutePath) *httpCache {
func newHTTPCache(opts Opts, teamID string, client client, recorder analytics.Recorder, repoRoot fs.AbsolutePath) *httpCache {
return &httpCache{
writable: true,
client: client,
Expand All @@ -338,7 +337,7 @@ func newHTTPCache(opts Opts, config *config.Config, client client, recorder anal
signerVerifier: &ArtifactSignatureAuthentication{
// TODO(Gaspar): this should use RemoteCacheOptions.TeamId once we start
// enforcing team restrictions for repositories.
teamId: config.TeamId,
teamId: teamID,
enabled: opts.RemoteCacheOpts.Signature,
},
repoRoot: repoRoot,
Expand Down
60 changes: 38 additions & 22 deletions cli/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
type ApiClient struct {
// The api's base URL
baseUrl string
Token string
token string
Copy link
Contributor

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?

Copy link
Contributor Author

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 the config package. All of the other usages, including the tests, are in the config package.

Copy link
Contributor

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!

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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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),
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 24 additions & 8 deletions cli/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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
Expand All @@ -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, "")
Expand All @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/cmd/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func LogoutCmd(ch *LogoutCommand) *cobra.Command {
Use: "logout",
Short: "Logout of your Vercel account",
RunE: func(cmd *cobra.Command, args []string) error {
if err := config.DeleteUserConfigFile(); err != nil {
if err := ch.Config.UserConfig.Delete(); err != nil {
return ch.logError("could not logout. Something went wrong: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion cli/internal/cmd/auth/unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func UnlinkCmd(ch *UnlinkCommand) *cobra.Command {
Use: "unlink",
Short: "Unlink the current directory from your Vercel organization and disable Remote Caching",
RunE: func(cmd *cobra.Command, args []string) error {
if err := config.WriteRepoConfigFile(ch.Config.Cwd, &config.TurborepoConfig{}); err != nil {
if err := ch.Config.RepoConfig.Delete(); err != nil {
return ch.logError("could not unlink. Something went wrong: %w", err)
}

Expand Down
Loading