Skip to content

Commit

Permalink
Merge branch 'main' into adamb/fix-data-races
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout authored Nov 4, 2024
2 parents 768503a + f86c9a2 commit 5b0027f
Show file tree
Hide file tree
Showing 46 changed files with 1,827 additions and 794 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"--proto_path=/usr/local/include/protoc"
]
},
"go.buildTags": "test_with_docker,test_with_txtar"
"go.buildTags": "test_with_docker,test_with_txtar",
"makefile.configureOnOpen": false
// Uncomment if you want to work on files in ./web.
// "go.buildTags": "js,wasm",
// Uncomment if you want to check compilation errors on Windows.
Expand Down
1 change: 0 additions & 1 deletion cmd/gqltool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stateful/runme/v3/internal/client/graphql"
)

// var tokenDir = flag.String("token-dir", cmd.GetUserConfigHome(), "The directory with tokens")
var apiURL = flag.String("api-url", "http://localhost:4000", "The API base address")

func init() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
golang.org/x/oauth2 v0.23.0
golang.org/x/sys v0.26.0
golang.org/x/term v0.25.0
google.golang.org/api v0.196.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38
google.golang.org/protobuf v1.35.1
mvdan.cc/sh/v3 v3.10.0
Expand Down Expand Up @@ -93,7 +94,6 @@ require (
go.opentelemetry.io/otel/trace v1.31.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/time v0.7.0 // indirect
google.golang.org/api v0.196.0 // indirect
google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
gotest.tools/v3 v3.5.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/beta/beta_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ All commands use the runme.yaml configuration file.`,

return nil
})

// Print the error to stderr but don't return it because error modes
// are neither fully baked yet nor ready for users to consume.
if err != nil {
Expand Down
127 changes: 106 additions & 21 deletions internal/cmd/beta/run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ package beta

import (
"context"
"io"
"os"

"github.com/creack/pty"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/stateful/runme/v3/internal/command"
"github.com/stateful/runme/v3/internal/config/autoconfig"
rcontext "github.com/stateful/runme/v3/internal/runner/context"
"github.com/stateful/runme/v3/internal/runnerv2client"
"github.com/stateful/runme/v3/internal/session"
runnerv2 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/runner/v2"
"github.com/stateful/runme/v3/pkg/document"
"github.com/stateful/runme/v3/pkg/project"
)

func runCmd(*commonFlags) *cobra.Command {
var remote bool

cmd := cobra.Command{
Use: "run [command1 command2 ...]",
Aliases: []string{"exec"},
Expand All @@ -36,6 +42,7 @@ Run all blocks from the "setup" and "teardown" tags:
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
func(
clientFactory autoconfig.ClientFactory,
cmdFactory command.Factory,
filters []project.Filter,
logger *zap.Logger,
Expand Down Expand Up @@ -67,21 +74,57 @@ Run all blocks from the "setup" and "teardown" tags:
return errors.WithStack(err)
}

session, err := session.New(
session.WithOwl(false),
session.WithProject(proj),
session.WithSeedEnv(nil),
)
if err != nil {
return err
}
options := getCommandOptions(cmd, session)
ctx := cmd.Context()

if remote {
client, err := clientFactory()
if err != nil {
return err
}

sessionResp, err := client.CreateSession(
ctx,
&runnerv2.CreateSessionRequest{
Project: &runnerv2.Project{
Root: proj.Root(),
EnvLoadOrder: proj.EnvFilesReadOrder(),
},
},
)
if err != nil {
return errors.WithMessage(err, "failed to create session")
}

for _, t := range tasks {
err := runCodeBlock(cmd.Context(), t.CodeBlock, cmdFactory, options)
for _, t := range tasks {
err := runCodeBlockWithClient(
ctx,
cmd,
client,
t.CodeBlock,
sessionResp.GetSession().GetId(),
)
if err != nil {
return err
}
}
} else {
session, err := session.New(
session.WithOwl(false),
session.WithProject(proj),
session.WithSeedEnv(nil),
)
if err != nil {
return err
}

options := createCommandOptions(cmd, session)

for _, t := range tasks {
err := runCodeBlock(ctx, t.CodeBlock, cmdFactory, options)
if err != nil {
return err
}
}
}

return nil
Expand All @@ -90,10 +133,12 @@ Run all blocks from the "setup" and "teardown" tags:
},
}

cmd.Flags().BoolVarP(&remote, "remote", "r", false, "Run commands on a remote server.")

return &cmd
}

func getCommandOptions(
func createCommandOptions(
cmd *cobra.Command,
sess *session.Session,
) command.CommandOptions {
Expand All @@ -105,12 +150,7 @@ func getCommandOptions(
}
}

func runCodeBlock(
ctx context.Context,
block *document.CodeBlock,
factory command.Factory,
options command.CommandOptions,
) error {
func createProgramConfigFromCodeBlock(block *document.CodeBlock, opts ...command.ConfigBuilderOption) (*command.ProgramConfig, error) {
// TODO(adamb): [command.Config] is generated exclusively from the [document.CodeBlock].
// As we introduce some document- and block-related configs in runme.yaml (root but also nested),
// this [Command.Config] should be further extended.
Expand All @@ -121,7 +161,16 @@ func runCodeBlock(
// the last element of the returned config chain. Finally, [command.Config] should be updated.
// This algorithm should be likely encapsulated in the [internal/config] and [internal/command]
// packages.
cfg, err := command.NewProgramConfigFromCodeBlock(block)
return command.NewProgramConfigFromCodeBlock(block, opts...)
}

func runCodeBlock(
ctx context.Context,
block *document.CodeBlock,
factory command.Factory,
options command.CommandOptions,
) error {
cfg, err := createProgramConfigFromCodeBlock(block)
if err != nil {
return err
}
Expand All @@ -138,9 +187,45 @@ func runCodeBlock(
if err != nil {
return err
}
err = cmd.Start(ctx)
if err != nil {
if err := cmd.Start(ctx); err != nil {
return err
}
return cmd.Wait(ctx)
}

func runCodeBlockWithClient(
ctx context.Context,
cobraCommand *cobra.Command,
client *runnerv2client.Client,
block *document.CodeBlock,
sessionID string,
) error {
cfg, err := createProgramConfigFromCodeBlock(block, command.WithInteractiveLegacy())
if err != nil {
return err
}

opts := runnerv2client.ExecuteProgramOptions{
SessionID: sessionID,
Stdin: io.NopCloser(cobraCommand.InOrStdin()),
Stdout: cobraCommand.OutOrStdout(),
Stderr: cobraCommand.ErrOrStderr(),
StoreStdoutInEnv: true,
}

if stdin, ok := cobraCommand.InOrStdin().(*os.File); ok {
size, err := pty.GetsizeFull(stdin)
if err != nil {
return errors.WithMessage(err, "failed to get terminal size")
}

opts.Winsize = &runnerv2.Winsize{
Rows: uint32(size.Rows),
Cols: uint32(size.Cols),
X: uint32(size.X),
Y: uint32(size.Y),
}
}

return client.ExecuteProgram(ctx, cfg, opts)
}
Loading

0 comments on commit 5b0027f

Please sign in to comment.