From 92cf38d7f78f7b8fb624747c191bb8577c8cb55d Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 14 Apr 2023 15:29:24 +0200 Subject: [PATCH] feat: add UI --- cmd/cmd.go | 2 +- pkg/executors/executor.go | 9 +++++---- pkg/executors/golang/cmder/cmder.go | 1 + pkg/executors/golang/codegen/compilation.go | 7 ++++--- pkg/executors/golang/compile/compile.go | 16 ++++++++-------- pkg/executors/golang/compile/compile_test.go | 6 +++++- pkg/executors/golang/compile/matcher/matcher.go | 4 +++- pkg/executors/golang/executer/list.go | 5 +++-- pkg/executors/golang/executer/prepare.go | 5 +++-- pkg/executors/golang/executer/run.go | 5 +++-- pkg/executors/golang/executer/run_test.go | 9 ++++++--- pkg/executors/shell.go | 3 ++- pkg/executors/task.go | 6 ++++-- 13 files changed, 48 insertions(+), 30 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index dcdbab5..6b3cca9 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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 } diff --git a/pkg/executors/executor.go b/pkg/executors/executor.go index 6f50ecd..7d519ac 100644 --- a/pkg/executors/executor.go +++ b/pkg/executors/executor.go @@ -8,6 +8,7 @@ import ( "github.com/lunarway/shuttle/pkg/config" "github.com/lunarway/shuttle/pkg/errors" + "github.com/lunarway/shuttle/pkg/ui" ) type Registry struct { @@ -15,7 +16,7 @@ type Registry struct { } 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{ @@ -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 } @@ -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) } } diff --git a/pkg/executors/golang/cmder/cmder.go b/pkg/executors/golang/cmder/cmder.go index ed4d308..9a3d259 100644 --- a/pkg/executors/golang/cmder/cmder.go +++ b/pkg/executors/golang/cmder/cmder.go @@ -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 diff --git a/pkg/executors/golang/codegen/compilation.go b/pkg/executors/golang/codegen/compilation.go index 3916b19..a40d80a 100644 --- a/pkg/executors/golang/codegen/compilation.go +++ b/pkg/executors/golang/codegen/compilation.go @@ -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 } diff --git a/pkg/executors/golang/compile/compile.go b/pkg/executors/golang/compile/compile.go index aaf7732..512340f 100644 --- a/pkg/executors/golang/compile/compile.go +++ b/pkg/executors/golang/compile/compile.go @@ -2,7 +2,6 @@ package compile import ( "context" - "log" "path" "github.com/lunarway/shuttle/pkg/executors/golang/codegen" @@ -10,6 +9,7 @@ import ( "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" ) @@ -37,12 +37,12 @@ type Binaries struct { // 2.2. Generate main file // // 3. Move binary to .shuttle/actions/binary- -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/executors/golang/compile/compile_test.go b/pkg/executors/golang/compile/compile_test.go index 84417aa..ab94f6b 100644 --- a/pkg/executors/golang/compile/compile_test.go +++ b/pkg/executors/golang/compile/compile_test.go @@ -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" ) @@ -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-") diff --git a/pkg/executors/golang/compile/matcher/matcher.go b/pkg/executors/golang/compile/matcher/matcher.go index 120eec7..57ffdab 100644 --- a/pkg/executors/golang/compile/matcher/matcher.go +++ b/pkg/executors/golang/compile/matcher/matcher.go @@ -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) { @@ -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 } } diff --git a/pkg/executors/golang/executer/list.go b/pkg/executors/golang/executer/list.go index ff416b3..911208d 100644 --- a/pkg/executors/golang/executer/list.go +++ b/pkg/executors/golang/executer/list.go @@ -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 } diff --git a/pkg/executors/golang/executer/prepare.go b/pkg/executors/golang/executer/prepare.go index 8af9496..a4b0d07 100644 --- a/pkg/executors/golang/executer/prepare.go +++ b/pkg/executors/golang/executer/prepare.go @@ -8,9 +8,10 @@ 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) @@ -18,7 +19,7 @@ func prepare(ctx context.Context, path string, c *config.ShuttleProjectContext) 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) } diff --git a/pkg/executors/golang/executer/run.go b/pkg/executors/golang/executer/run.go index 38dab95..525cf76 100644 --- a/pkg/executors/golang/executer/run.go +++ b/pkg/executors/golang/executer/run.go @@ -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 diff --git a/pkg/executors/golang/executer/run_test.go b/pkg/executors/golang/executer/run_test.go index 77f0e15..935e97f 100644 --- a/pkg/executors/golang/executer/run_test.go +++ b/pkg/executors/golang/executer/run_test.go @@ -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" ) @@ -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) } diff --git a/pkg/executors/shell.go b/pkg/executors/shell.go index 0c9c0e9..dcb5a7a 100644 --- a/pkg/executors/shell.go +++ b/pkg/executors/shell.go @@ -11,6 +11,7 @@ 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) { @@ -18,7 +19,7 @@ func ShellExecutor(action config.ShuttleAction) (Executor, bool) { } // 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, diff --git a/pkg/executors/task.go b/pkg/executors/task.go index 841b178..a75c04a 100644 --- a/pkg/executors/task.go +++ b/pkg/executors/task.go @@ -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) @@ -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 }