Skip to content

Commit

Permalink
Added ExecutionState as struct that passes args and remote config to …
Browse files Browse the repository at this point in the history
…Go code
  • Loading branch information
NicholasLYang committed Apr 21, 2023
1 parent abdff2c commit a161c02
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 577 deletions.
10 changes: 5 additions & 5 deletions cli/cmd/turbo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func main() {
os.Exit(1)
}

argsString := os.Args[1]
var args turbostate.ParsedArgsFromRust
err := json.Unmarshal([]byte(argsString), &args)
executionStateString := os.Args[1]
var executionState turbostate.ExecutionState
err := json.Unmarshal([]byte(executionStateString), &executionState)
if err != nil {
fmt.Printf("Error unmarshalling CLI args: %v\n Arg string: %v\n", err, argsString)
fmt.Printf("Error unmarshalling execution state: %v\n Execution state string: %v\n", err, executionStateString)
os.Exit(1)
}

exitCode := cmd.RunWithArgs(&args, turboVersion)
exitCode := cmd.RunWithExecutionState(&executionState, turboVersion)
os.Exit(exitCode)
}
11 changes: 2 additions & 9 deletions cli/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-retryablehttp"
"github.com/vercel/turbo/cli/internal/ci"
"github.com/vercel/turbo/cli/internal/turbostate"
)

// APIClient is the main interface for making network requests to Vercel
Expand Down Expand Up @@ -47,14 +48,6 @@ func (c *APIClient) SetToken(token string) {
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
}

// Opts holds values for configuring the behavior of the API client
type Opts struct {
UsePreflight bool
Expand All @@ -65,7 +58,7 @@ type Opts struct {
const ClientTimeout uint64 = 20

// NewClient creates a new APIClient
func NewClient(remoteConfig RemoteConfig, logger hclog.Logger, turboVersion string, opts Opts) *APIClient {
func NewClient(remoteConfig turbostate.RemoteConfig, logger hclog.Logger, turboVersion string, opts Opts) *APIClient {
client := &APIClient{
baseURL: remoteConfig.APIURL,
turboVersion: turboVersion,
Expand Down
9 changes: 5 additions & 4 deletions cli/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/uuid"
"github.com/hashicorp/go-hclog"
"github.com/vercel/turbo/cli/internal/turbostate"
"github.com/vercel/turbo/cli/internal/util"
)

Expand All @@ -30,7 +31,7 @@ func Test_sendToServer(t *testing.T) {
}))
defer ts.Close()

remoteConfig := RemoteConfig{
remoteConfig := turbostate.RemoteConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Expand Down Expand Up @@ -85,7 +86,7 @@ func Test_PutArtifact(t *testing.T) {
defer ts.Close()

// Set up test expected values
remoteConfig := RemoteConfig{
remoteConfig := turbostate.RemoteConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Expand All @@ -111,7 +112,7 @@ func Test_PutWhenCachingDisabled(t *testing.T) {
defer ts.Close()

// Set up test expected values
remoteConfig := RemoteConfig{
remoteConfig := turbostate.RemoteConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Expand All @@ -138,7 +139,7 @@ func Test_FetchWhenCachingDisabled(t *testing.T) {
defer ts.Close()

// Set up test expected values
remoteConfig := RemoteConfig{
remoteConfig := turbostate.RemoteConfig{
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Expand Down
18 changes: 9 additions & 9 deletions cli/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,31 @@ func initializeOutputFiles(helper *cmdutil.Helper, parsedArgs *turbostate.Parsed
return nil
}

// RunWithArgs runs turbo with the ParsedArgsFromRust that is passed from the Rust side.
func RunWithArgs(args *turbostate.ParsedArgsFromRust, turboVersion string) int {
// RunWithExecutionState runs turbo with the ParsedArgsFromRust that is passed from the Rust side.
func RunWithExecutionState(executionState *turbostate.ExecutionState, turboVersion string) int {
util.InitPrintf()
// TODO: replace this with a context
signalWatcher := signals.NewWatcher()
helper := cmdutil.NewHelper(turboVersion, args)
helper := cmdutil.NewHelper(turboVersion, &executionState.CLIArgs)
ctx := context.Background()

err := initializeOutputFiles(helper, args)
err := initializeOutputFiles(helper, &executionState.CLIArgs)
if err != nil {
fmt.Printf("%v", err)
return 1
}
defer helper.Cleanup(args)
defer helper.Cleanup(&executionState.CLIArgs)

doneCh := make(chan struct{})
var execErr error
go func() {
command := args.Command
command := executionState.CLIArgs.Command
if command.Daemon != nil {
execErr = daemon.ExecuteDaemon(ctx, helper, signalWatcher, args)
execErr = daemon.ExecuteDaemon(ctx, helper, signalWatcher, executionState)
} else if command.Prune != nil {
execErr = prune.ExecutePrune(helper, args)
execErr = prune.ExecutePrune(helper, executionState)
} else if command.Run != nil {
execErr = run.ExecuteRun(ctx, helper, signalWatcher, args)
execErr = run.ExecuteRun(ctx, helper, signalWatcher, executionState)
} else {
execErr = fmt.Errorf("unknown command: %v", command)
}
Expand Down
30 changes: 9 additions & 21 deletions cli/internal/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func (h *Helper) Cleanup(cliConfig *turbostate.ParsedArgsFromRust) {
}
}

func (h *Helper) getUI(cliConfig *turbostate.ParsedArgsFromRust) cli.Ui {
func (h *Helper) getUI(cliArgs *turbostate.ParsedArgsFromRust) cli.Ui {
colorMode := ui.GetColorModeFromEnv()
if cliConfig.GetNoColor() {
if cliArgs.NoColor {
colorMode = ui.ColorModeSuppressed
}
if cliConfig.GetColor() {
if cliArgs.Color {
colorMode = ui.ColorModeForced
}
return ui.BuildColoredUi(colorMode)
Expand Down Expand Up @@ -134,15 +134,15 @@ func NewHelper(turboVersion string, args *turbostate.ParsedArgsFromRust) *Helper

// GetCmdBase returns a CmdBase instance configured with values from this helper.
// It additionally returns a mechanism to set an error, so
func (h *Helper) GetCmdBase(cliConfig *turbostate.ParsedArgsFromRust) (*CmdBase, error) {
func (h *Helper) GetCmdBase(executionState *turbostate.ExecutionState) (*CmdBase, error) {
// terminal is for color/no-color output
terminal := h.getUI(cliConfig)
terminal := h.getUI(&executionState.CLIArgs)
// logger is configured with verbosity level using --verbosity flag from end users
logger, err := h.getLogger()
if err != nil {
return nil, err
}
cwdRaw, err := cliConfig.GetCwd()
cwdRaw := executionState.CLIArgs.CWD
if err != nil {
return nil, err
}
Expand All @@ -155,15 +155,7 @@ func (h *Helper) GetCmdBase(cliConfig *turbostate.ParsedArgsFromRust) (*CmdBase,
if err != nil {
return nil, err
}
repoConfig, err := config.ReadRepoConfigFile(config.GetRepoConfigPath(repoRoot), cliConfig)
if err != nil {
return nil, err
}
userConfig, err := config.ReadUserConfigFile(h.UserConfigPath, cliConfig)
if err != nil {
return nil, err
}
remoteConfig := repoConfig.GetRemoteConfig(userConfig.Token())
remoteConfig := executionState.RemoteConfig
if remoteConfig.Token == "" && ui.IsCI {
vercelArtifactsToken := os.Getenv("VERCEL_ARTIFACTS_TOKEN")
vercelArtifactsOwner := os.Getenv("VERCEL_ARTIFACTS_OWNER")
Expand All @@ -176,7 +168,7 @@ func (h *Helper) GetCmdBase(cliConfig *turbostate.ParsedArgsFromRust) (*CmdBase,
}

// Primacy: Arg > Env
timeout, err := cliConfig.GetRemoteCacheTimeout()
timeout, err := executionState.CLIArgs.GetRemoteCacheTimeout()
if err == nil {
h.clientOpts.Timeout = timeout
} else {
Expand All @@ -201,8 +193,6 @@ func (h *Helper) GetCmdBase(cliConfig *turbostate.ParsedArgsFromRust) (*CmdBase,
Logger: logger,
RepoRoot: repoRoot,
APIClient: apiClient,
RepoConfig: repoConfig,
UserConfig: userConfig,
RemoteConfig: remoteConfig,
TurboVersion: h.TurboVersion,
}, nil
Expand All @@ -214,9 +204,7 @@ type CmdBase struct {
Logger hclog.Logger
RepoRoot turbopath.AbsoluteSystemPath
APIClient *client.APIClient
RepoConfig *config.RepoConfig
UserConfig *config.UserConfig
RemoteConfig client.RemoteConfig
RemoteConfig turbostate.RemoteConfig
TurboVersion string
}

Expand Down
109 changes: 0 additions & 109 deletions cli/internal/cmdutil/cmdutil_test.go

This file was deleted.

0 comments on commit a161c02

Please sign in to comment.