Skip to content

Commit

Permalink
Renamed RemoteConfig to APIClientConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Apr 24, 2023
1 parent 6102eb8 commit 489bf83
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 49 deletions.
14 changes: 7 additions & 7 deletions cli/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ func (c *APIClient) SetToken(token string) {
}

// NewClient creates a new APIClient
func NewClient(remoteConfig turbostate.RemoteConfig, logger hclog.Logger, turboVersion string) *APIClient {
func NewClient(config turbostate.APIClientConfig, logger hclog.Logger, turboVersion string) *APIClient {
client := &APIClient{
baseURL: remoteConfig.APIURL,
baseURL: config.APIURL,
turboVersion: turboVersion,
HTTPClient: &retryablehttp.Client{
HTTPClient: &http.Client{
Timeout: time.Duration(remoteConfig.Timeout) * time.Second,
Timeout: time.Duration(config.Timeout) * time.Second,
},
RetryWaitMin: 2 * time.Second,
RetryWaitMax: 10 * time.Second,
RetryMax: 2,
Backoff: retryablehttp.DefaultBackoff,
Logger: logger,
},
token: remoteConfig.Token,
teamID: remoteConfig.TeamID,
teamSlug: remoteConfig.TeamSlug,
usePreflight: remoteConfig.UsePreflight,
token: config.Token,
teamID: config.TeamID,
teamSlug: config.TeamSlug,
usePreflight: config.UsePreflight,
}
client.HTTPClient.CheckRetry = client.checkRetry
return client
Expand Down
16 changes: 8 additions & 8 deletions cli/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func Test_sendToServer(t *testing.T) {
}))
defer ts.Close()

remoteConfig := turbostate.RemoteConfig{
apiClientConfig := turbostate.APIClientConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})
apiClient := NewClient(apiClientConfig, hclog.Default(), "v1")

myUUID, err := uuid.NewUUID()
if err != nil {
Expand Down Expand Up @@ -86,12 +86,12 @@ func Test_PutArtifact(t *testing.T) {
defer ts.Close()

// Set up test expected values
remoteConfig := turbostate.RemoteConfig{
apiClientConfig := turbostate.APIClientConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})
apiClient := NewClient(apiClientConfig, hclog.Default(), "v1")
expectedArtifactBody := []byte("My string artifact")

// Test Put Artifact
Expand All @@ -112,12 +112,12 @@ func Test_PutWhenCachingDisabled(t *testing.T) {
defer ts.Close()

// Set up test expected values
remoteConfig := turbostate.RemoteConfig{
apiClientConfig := turbostate.APIClientConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})
apiClient := NewClient(apiClientConfig, hclog.Default(), "v1")
expectedArtifactBody := []byte("My string artifact")
// Test Put Artifact
err := apiClient.PutArtifact("hash", expectedArtifactBody, 500, "")
Expand All @@ -139,12 +139,12 @@ func Test_FetchWhenCachingDisabled(t *testing.T) {
defer ts.Close()

// Set up test expected values
remoteConfig := turbostate.RemoteConfig{
apiClientConfig := turbostate.APIClientConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})
apiClient := NewClient(apiClientConfig, hclog.Default(), "v1")
// Test Put Artifact
resp, err := apiClient.FetchArtifact("hash")
cd := &util.CacheDisabledError{}
Expand Down
16 changes: 2 additions & 14 deletions cli/internal/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,10 @@ func (h *Helper) GetCmdBase(executionState *turbostate.ExecutionState) (*CmdBase
if err != nil {
return nil, err
}
remoteConfig := executionState.RemoteConfig
if remoteConfig.Token == "" && ui.IsCI {
vercelArtifactsToken := os.Getenv("VERCEL_ARTIFACTS_TOKEN")
vercelArtifactsOwner := os.Getenv("VERCEL_ARTIFACTS_OWNER")
if vercelArtifactsToken != "" {
remoteConfig.Token = vercelArtifactsToken
}
if vercelArtifactsOwner != "" {
remoteConfig.TeamID = vercelArtifactsOwner
}
}
apiClientConfig := executionState.APIClientConfig

apiClient := client.NewClient(
remoteConfig,
apiClientConfig,
logger,
h.TurboVersion,
)
Expand All @@ -175,7 +165,6 @@ func (h *Helper) GetCmdBase(executionState *turbostate.ExecutionState) (*CmdBase
Logger: logger,
RepoRoot: repoRoot,
APIClient: apiClient,
RemoteConfig: remoteConfig,
TurboVersion: h.TurboVersion,
}, nil
}
Expand All @@ -186,7 +175,6 @@ type CmdBase struct {
Logger hclog.Logger
RepoRoot turbopath.AbsoluteSystemPath
APIClient *client.APIClient
RemoteConfig turbostate.RemoteConfig
TurboVersion string
}

Expand Down
14 changes: 9 additions & 5 deletions cli/internal/cmdutil/cmdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import (
"testing"
"time"

"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/turbostate"
"gotest.tools/v3/assert"
)

func TestRemoteCacheTimeoutFlag(t *testing.T) {
args := turbostate.ParsedArgsFromRust{
CWD: "",
RemoteCacheTimeout: 599,
CWD: "",
}

executionState := turbostate.ExecutionState{
APIClientConfig: turbostate.APIClientConfig{
Timeout: 599,
},
CLIArgs: args,
}

Expand All @@ -38,10 +40,12 @@ func TestRemoteCacheTimeoutPrimacy(t *testing.T) {
_ = os.Unsetenv(key)
})
args := turbostate.ParsedArgsFromRust{
CWD: "",
RemoteCacheTimeout: 1,
CWD: "",
}
executionState := turbostate.ExecutionState{
APIClientConfig: turbostate.APIClientConfig{
Timeout: 1,
},
CLIArgs: args,
}
h := NewHelper("test-version", &args)
Expand Down
15 changes: 4 additions & 11 deletions cli/internal/turbostate/turbostate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"github.com/vercel/turbo/cli/internal/util"
)

// RepoState is the state for repository. Consists of the root for the repo
// along with the mode (single package or multi package)
type RepoState struct {
Root string `json:"root"`
Mode string `json:"mode"`
}

// DaemonPayload is the extra flags and command that are
// passed for the `daemon` subcommand
type DaemonPayload struct {
Expand Down Expand Up @@ -97,12 +90,12 @@ type ParsedArgsFromRust struct {

// ExecutionState is the entire state of a turbo execution that is passed from the Rust shim.
type ExecutionState struct {
RemoteConfig RemoteConfig `json:"remote_config"`
CLIArgs ParsedArgsFromRust `json:"cli_args"`
APIClientConfig APIClientConfig `json:"remote_config"`
CLIArgs ParsedArgsFromRust `json:"cli_args"`
}

// RemoteConfig holds the authentication and endpoint details for the API client
type RemoteConfig struct {
// APIClientConfig holds the authentication and endpoint details for the API client
type APIClientConfig struct {
Token string `json:"token"`
TeamID string `json:"team_id"`
TeamSlug string `json:"team_slug"`
Expand Down
6 changes: 6 additions & 0 deletions crates/turborepo-lib/src/config/repo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
env,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -164,6 +165,11 @@ impl RepoConfigLoader {
config.team_id = None;
}

// We don't set this above because it's specific to team_id
if let Ok(vercel_artifacts_owner) = env::var("VERCEL_ARTIFACTS_OWNER") {
config.team_id = Some(vercel_artifacts_owner);
}

Ok(RepoConfig {
disk_config,
config,
Expand Down
8 changes: 4 additions & 4 deletions crates/turborepo-lib/src/execution_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::{cli::Args, commands::CommandBase};

#[derive(Debug, Serialize)]
pub struct ExecutionState<'a> {
pub remote_config: RemoteConfig<'a>,
pub api_client_config: APIClientConfig<'a>,
pub cli_args: &'a Args,
}

#[derive(Debug, Serialize, Default)]
pub struct RemoteConfig<'a> {
pub struct APIClientConfig<'a> {
pub token: Option<&'a str>,
pub team_id: Option<&'a str>,
pub team_slug: Option<&'a str>,
Expand All @@ -27,7 +27,7 @@ impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {
let client_config = base.client_config()?;
let args = base.args();

let remote_config = RemoteConfig {
let remote_config = APIClientConfig {
token: user_config.token(),
team_id: repo_config.team_id(),
team_slug: repo_config.team_slug(),
Expand All @@ -37,7 +37,7 @@ impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {
};

Ok(ExecutionState {
remote_config,
api_client_config: remote_config,
cli_args: base.args(),
})
}
Expand Down

0 comments on commit 489bf83

Please sign in to comment.