Skip to content

Commit

Permalink
Default shell blocks to sh runner (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik authored Oct 17, 2022
1 parent 836370d commit 21efe1c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
33 changes: 9 additions & 24 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,15 @@ func runCmd() *cobra.Command {
}

func newExecutable(cmd *cobra.Command, block *document.CodeBlock) (runner.Executable, error) {
switch block.Executable() {
case "sh":
return &runner.Shell{
Cmds: block.Lines(),
Base: runner.Base{
Dir: chdir,
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
},
}, nil
case "go":
return &runner.Go{
Source: block.Content(),
Base: runner.Base{
Dir: chdir,
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
},
}, nil
default:
return nil, errors.Errorf("unknown executable: %q", block.Executable())
}
return runner.New(
block,
&runner.Base{
Dir: chdir,
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
},
)
}

func sigCtxCancel(ctx context.Context) (context.Context, context.CancelFunc) {
Expand Down
20 changes: 20 additions & 0 deletions internal/runner/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package runner
import (
"context"
"io"

"github.com/pkg/errors"
"github.com/stateful/runme/internal/document"
)

type Executable interface {
Expand All @@ -16,3 +19,20 @@ type Base struct {
Stdout io.Writer
Stderr io.Writer
}

func New(block *document.CodeBlock, base *Base) (Executable, error) {
switch block.Executable() {
case "sh", "shell":
return &Shell{
Cmds: block.Lines(),
Base: base,
}, nil
case "go":
return &Go{
Source: block.Content(),
Base: base,
}, nil
default:
return nil, errors.Errorf("unknown executable: %q", block.Executable())
}
}
2 changes: 1 addition & 1 deletion internal/runner/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type Go struct {
Base
*Base
Source string
}

Expand Down
2 changes: 1 addition & 1 deletion internal/runner/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

type Shell struct {
Base
*Base
Cmds []string
}

Expand Down

0 comments on commit 21efe1c

Please sign in to comment.