Skip to content

Commit

Permalink
Refactor internal/command by removing normalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed May 21, 2024
1 parent 3d637c0 commit 07cac7b
Show file tree
Hide file tree
Showing 33 changed files with 1,355 additions and 1,164 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ test/execute: RUN ?= .*
test/execute: RACE ?= false
test/execute: TAGS ?= "" # e.g. TAGS="test_with_docker"
test/execute: build test/prep-git-project
TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -run="$(RUN)" -tags="$(TAGS)" -timeout=60s -race=$(RACE) -covermode=atomic -coverprofile=cover.out -coverpkg=./... $(PKGS)
TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -run="$(RUN)" -tags="$(TAGS)" -timeout=60s -race=$(RACE) $(PKGS)

.PHONY: test/coverage
test/coverage: PKGS ?= "./..."
test/coverage: RUN ?= .*
test/coverage: TAGS ?= "" # e.g. TAGS="test_with_docker"
test/coverage: build test/prep-git-project
TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -run="$(RUN)" -tags="$(TAGS)" -timeout=90s -covermode=atomic -coverprofile=cover.out -coverpkg=./github.com/stateful/runme/v3 $(PKGS)

.PHONY: test/prep-git-project
test/prep-git-project:
Expand Down
73 changes: 33 additions & 40 deletions internal/cmd/beta/run_cmd.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package beta

import (
"context"

"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"
"github.com/stateful/runme/v3/internal/document"
"github.com/stateful/runme/v3/internal/project"
)

func runCmd(*commonFlags) *cobra.Command {
var kernelName string

cmd := cobra.Command{
Use: "run [command1 command2 ...]",
Aliases: []string{"exec"},
Expand All @@ -32,8 +33,9 @@ Run all blocks from the "setup" and "teardown" categories:
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.Invoke(
func(
cmdFactory command.Factory,
filters []project.Filter,
kernelGetter autoconfig.KernelGetter,
kernel command.Kernel,
logger *zap.Logger,
proj *project.Project,
session *command.Session,
Expand Down Expand Up @@ -64,13 +66,15 @@ Run all blocks from the "setup" and "teardown" categories:
return errors.WithStack(err)
}

kernel, err := kernelGetter(kernelName)
if err != nil {
return err
}
options := getCommandOptions(
cmd,
kernel,
session,
logger,
)

for _, t := range tasks {
err := runCodeBlock(t, cmd, kernel, session, logger)
err := runCodeBlock(cmd.Context(), t.CodeBlock, cmdFactory, options)
if err != nil {
return err
}
Expand All @@ -82,49 +86,38 @@ Run all blocks from the "setup" and "teardown" categories:
},
}

cmd.Flags().StringVar(&kernelName, "kernel", "", "Kernel name or index to use for command execution.")

return &cmd
}

func runCodeBlock(
task project.Task,
func getCommandOptions(
cmd *cobra.Command,
kernel command.Kernel,
sess *command.Session,
logger *zap.Logger,
) command.Options {
return command.Options{
Kernel: kernel,
Logger: logger,
Session: sess,
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
}
}

func runCodeBlock(
ctx context.Context,
block *document.CodeBlock,
factory command.Factory,
options command.Options,
) 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.
//
// The way to do it is to use [config.Loader] and calling [config.Loader.FindConfigChain] with
// task's document path. It will produce all the configs that are relevant to the document.
// Next, they should be merged into a single [config.Config] in a correct order, starting from
// 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.NewConfigFromCodeBlock(task.CodeBlock)
cfg, err := command.NewProgramConfigFromCodeBlock(block)
if err != nil {
return err
}

kernelCmd := kernel.Command(
cfg,
command.Options{
Kernel: kernel,
Logger: logger,
Session: sess,
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
},
)

err = kernelCmd.Start(cmd.Context())
if err != nil {
cmd := factory.Build(cfg, options)
if err := cmd.Start(ctx); err != nil {
return err
}

return kernelCmd.Wait()
return cmd.Wait()
}
2 changes: 1 addition & 1 deletion internal/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The kernel is used to run long running processes like shells and interacting wit
}
runnerv1.RegisterRunnerServiceServer(server, runnerServicev1)

runnerServicev2, err := runnerv2service.NewRunnerService(logger)
runnerServicev2, err := runnerv2service.NewRunnerService()
if err != nil {
return err
}
Expand Down
34 changes: 34 additions & 0 deletions internal/command/bulk_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package command

import (
"io"

"github.com/pkg/errors"
)

type bulkWriter struct {
io.Writer
n int
err error
}

func (w *bulkWriter) Done() (int, error) {
return w.n, w.err
}

func (w *bulkWriter) Write(d []byte) {
if w.err != nil {
return
}
n, err := w.Writer.Write(d)
w.n += n
w.err = errors.WithStack(err)
}

func (w *bulkWriter) WriteByte(c byte) {
w.Write([]byte{c})
}

func (w *bulkWriter) WriteString(s string) {
w.Write([]byte(s))
}
Loading

0 comments on commit 07cac7b

Please sign in to comment.