Skip to content

Commit

Permalink
feat: add UI
Browse files Browse the repository at this point in the history
  • Loading branch information
kjuulh committed Apr 14, 2023
1 parent c68901f commit 92cf38d
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func getProjectContext(rootCmd *cobra.Command, uii *ui.UI, projectPath string, c
}

ctx := stdcontext.Background()
taskActions, err := executer.List(ctx, fmt.Sprintf("%s/shuttle.yaml", projectContext.ProjectPath), &c)
taskActions, err := executer.List(ctx, uii, fmt.Sprintf("%s/shuttle.yaml", projectContext.ProjectPath), &c)
if err != nil {
return config.ShuttleProjectContext{}, err
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/executors/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/errors"
"github.com/lunarway/shuttle/pkg/ui"
)

type Registry struct {
executors []Matcher
}

type Matcher func(config.ShuttleAction) (Executor, bool)
type Executor func(context.Context, ActionExecutionContext) error
type Executor func(context.Context, *ui.UI, ActionExecutionContext) error

func NewRegistry(executors ...Matcher) *Registry {
return &Registry{
Expand Down Expand Up @@ -63,7 +64,7 @@ func (r *Registry) Execute(ctx context.Context, p config.ShuttleProjectContext,
Action: action,
ActionIndex: actionIndex,
}
err := r.executeAction(ctx, actionContext)
err := r.executeAction(ctx, p.UI, actionContext)
if err != nil {
return err
}
Expand Down Expand Up @@ -176,11 +177,11 @@ func expectedArgumentsHelp(command string, args []config.ShuttleScriptArgs) stri
return s.String()
}

func (r *Registry) executeAction(ctx context.Context, context ActionExecutionContext) error {
func (r *Registry) executeAction(ctx context.Context, ui *ui.UI, context ActionExecutionContext) error {
for _, executor := range r.executors {
handler, ok := executor(context.Action)
if ok {
return handler(ctx, context)
return handler(ctx, ui, context)
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/executors/golang/cmder/cmder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (rc *RootCmd) Execute() {
return err
}

// Prints the commands and args as json to stdout
_, err = fmt.Printf("%s", string(rawJson))
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions pkg/executors/golang/codegen/compilation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package codegen

import (
"context"
"log"
"os/exec"
"path"

"github.com/lunarway/shuttle/pkg/ui"
)

func CompileBinary(ctx context.Context, shuttlelocaldir string) (string, error) {
func CompileBinary(ctx context.Context, ui *ui.UI, shuttlelocaldir string) (string, error) {
cmd := exec.Command("go", "build")
cmd.Dir = path.Join(shuttlelocaldir, "tmp")

output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("%s\n", string(output))
ui.Verboseln("compile-binary output: %s", string(output))
return "", err
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/executors/golang/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package compile

import (
"context"
"log"
"path"

"github.com/lunarway/shuttle/pkg/executors/golang/codegen"
"github.com/lunarway/shuttle/pkg/executors/golang/compile/matcher"
"github.com/lunarway/shuttle/pkg/executors/golang/discover"
"github.com/lunarway/shuttle/pkg/executors/golang/parser"
"github.com/lunarway/shuttle/pkg/executors/golang/shuttlefolder"
"github.com/lunarway/shuttle/pkg/ui"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -37,12 +37,12 @@ type Binaries struct {
// 2.2. Generate main file
//
// 3. Move binary to .shuttle/actions/binary-<hash>
func Compile(ctx context.Context, discovered *discover.Discovered) (*Binaries, error) {
func Compile(ctx context.Context, ui *ui.UI, discovered *discover.Discovered) (*Binaries, error) {
egrp, ctx := errgroup.WithContext(ctx)
binaries := &Binaries{}
if discovered.Local != nil {
egrp.Go(func() error {
path, err := compile(ctx, discovered.Local)
path, err := compile(ctx, ui, discovered.Local)
if err != nil {
return err
}
Expand All @@ -53,7 +53,7 @@ func Compile(ctx context.Context, discovered *discover.Discovered) (*Binaries, e
}
if discovered.Plan != nil {
egrp.Go(func() error {
path, err := compile(ctx, discovered.Plan)
path, err := compile(ctx, ui, discovered.Plan)
if err != nil {
return err
}
Expand All @@ -70,19 +70,19 @@ func Compile(ctx context.Context, discovered *discover.Discovered) (*Binaries, e
return binaries, nil
}

func compile(ctx context.Context, actions *discover.ActionsDiscovered) (string, error) {
func compile(ctx context.Context, ui *ui.UI, actions *discover.ActionsDiscovered) (string, error) {
hash, err := matcher.GetHash(ctx, actions)
if err != nil {
return "", err
}

binaryPath, ok, err := matcher.BinaryMatches(ctx, hash, actions)
binaryPath, ok, err := matcher.BinaryMatches(ctx, ui, hash, actions)
if err != nil {
return "", err
}

if ok && !alwaysBuild {
log.Printf("DEBUG: file already matches continueing\n")
ui.Verboseln("file already matches continueing")
// The binary is the same so we short circuit
return binaryPath, nil
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func compile(ctx context.Context, actions *discover.ActionsDiscovered) (string,
if err = codegen.ModTidy(ctx, shuttlelocaldir); err != nil {
return "", err
}
binarypath, err := codegen.CompileBinary(ctx, shuttlelocaldir)
binarypath, err := codegen.CompileBinary(ctx, ui, shuttlelocaldir)
if err != nil {
return "", err
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/executors/golang/compile/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package compile_test

import (
"context"
"os"
"testing"

"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/executors/golang/compile"
"github.com/lunarway/shuttle/pkg/executors/golang/discover"
"github.com/lunarway/shuttle/pkg/ui"
"github.com/stretchr/testify/assert"
)

Expand All @@ -15,7 +17,9 @@ func TestCompile(t *testing.T) {
discovered, err := discover.Discover(ctx, "testdata/simple/shuttle.yaml", &config.ShuttleProjectContext{})
assert.NoError(t, err)

path, err := compile.Compile(ctx, discovered)
uiout := ui.Create(os.Stdout, os.Stderr)

path, err := compile.Compile(ctx, uiout, discovered)
assert.NoError(t, err)

assert.Contains(t, path.Local.Path, "testdata/simple/.shuttle/actions/binaries/actions-")
Expand Down
4 changes: 3 additions & 1 deletion pkg/executors/golang/compile/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"path"

"github.com/lunarway/shuttle/pkg/executors/golang/discover"
"github.com/lunarway/shuttle/pkg/ui"
"golang.org/x/mod/sumdb/dirhash"
)

func BinaryMatches(
ctx context.Context,
ui *ui.UI,
hash string,
actions *discover.ActionsDiscovered,
) (string, bool, error) {
Expand All @@ -42,7 +44,7 @@ func BinaryMatches(
if binary.Name() == fmt.Sprintf("actions-%s", hex.EncodeToString([]byte(hash)[:16])) {
return path.Join(shuttlebindir, binary.Name()), true, nil
} else {
log.Printf("DEBUG: binary does not match, rebuilding...")
ui.Verboseln("binary does not match, rebuilding...")
return "", false, nil
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/executors/golang/executer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"

"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/ui"
)

type TaskArg struct {
}

func List(ctx context.Context, path string, c *config.ShuttleProjectContext) (map[string]TaskArg, error) {
binaries, err := prepare(ctx, path, c)
func List(ctx context.Context, ui *ui.UI, path string, c *config.ShuttleProjectContext) (map[string]TaskArg, error) {
binaries, err := prepare(ctx, ui, path, c)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/executors/golang/executer/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/executors/golang/compile"
"github.com/lunarway/shuttle/pkg/executors/golang/discover"
"github.com/lunarway/shuttle/pkg/ui"
)

func prepare(ctx context.Context, path string, c *config.ShuttleProjectContext) (*compile.Binaries, error) {
func prepare(ctx context.Context, ui *ui.UI, path string, c *config.ShuttleProjectContext) (*compile.Binaries, error) {
log.SetFlags(log.LstdFlags | log.Lshortfile)

disc, err := discover.Discover(ctx, path, c)
if err != nil {
return nil, fmt.Errorf("failed to fiscover actions: %v", err)
}

binaries, err := compile.Compile(ctx, disc)
binaries, err := compile.Compile(ctx, ui, disc)
if err != nil {
return nil, fmt.Errorf("failed to compile binaries: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/executors/golang/executer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"log"

"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/ui"
)

func Run(ctx context.Context, c *config.ShuttleProjectContext, path string, args ...string) error {
binaries, err := prepare(ctx, path, c)
func Run(ctx context.Context, ui *ui.UI, c *config.ShuttleProjectContext, path string, args ...string) error {
binaries, err := prepare(ctx, ui, path, c)
if err != nil {
log.Printf("failed to run command: %v", err)
return err
Expand Down
9 changes: 6 additions & 3 deletions pkg/executors/golang/executer/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/executors/golang/executer"
"github.com/lunarway/shuttle/pkg/ui"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,13 +19,15 @@ func TestRunVersion(t *testing.T) {

c := &config.ShuttleProjectContext{Config: config.ShuttleConfig{Plan: "something"}}

err := executer.Run(ctx, c, "testdata/child/shuttle.yaml", "version")
ui := ui.Create(os.Stdout, os.Stderr)

err := executer.Run(ctx, ui, c, "testdata/child/shuttle.yaml", "version")
assert.NoError(t, err)

err = executer.Run(ctx, c, "testdata/child/shuttle.yaml", "build")
err = executer.Run(ctx, ui, c, "testdata/child/shuttle.yaml", "build")
assert.NoError(t, err)

err = executer.Run(ctx, c, "testdata/child/shuttle.yaml", "build", "--some-unexisting-arg", "something")
err = executer.Run(ctx, ui, c, "testdata/child/shuttle.yaml", "build", "--some-unexisting-arg", "something")
assert.Error(t, err)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/executors/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
"github.com/go-cmd/cmd"
"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/errors"
"github.com/lunarway/shuttle/pkg/ui"
)

func ShellExecutor(action config.ShuttleAction) (Executor, bool) {
return executeShell, action.Shell != ""
}

// Build builds the docker image from a shuttle plan
func executeShell(ctx context.Context, context ActionExecutionContext) error {
func executeShell(ctx context.Context, ui *ui.UI, context ActionExecutionContext) error {
cmdOptions := cmd.Options{
Buffered: false,
Streaming: true,
Expand Down
6 changes: 4 additions & 2 deletions pkg/executors/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"github.com/go-cmd/cmd"
"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/executors/golang/executer"
"github.com/lunarway/shuttle/pkg/ui"
)

func TaskExecutor(action config.ShuttleAction) (Executor, bool) {
return executeTask, action.Task != ""
}

func executeTask(ctx context.Context, context ActionExecutionContext) error {
func executeTask(ctx context.Context, ui *ui.UI, context ActionExecutionContext) error {

context.ScriptContext.Project.UI.Verboseln("Starting task command: %s", context.Action.Task)

args := make([]string, 0)
Expand All @@ -25,7 +27,7 @@ func executeTask(ctx context.Context, context ActionExecutionContext) error {
args = append(args, value)
}

err := executer.Run(ctx, &context.ScriptContext.Project, "shuttle.yaml", args...)
err := executer.Run(ctx, ui, &context.ScriptContext.Project, "shuttle.yaml", args...)
if err != nil {
return err
}
Expand Down

0 comments on commit 92cf38d

Please sign in to comment.