From c7494976f0a242c613b17aa4b5e8af58e8425b36 Mon Sep 17 00:00:00 2001 From: Ali AKCA Date: Fri, 4 Nov 2022 13:31:38 +0100 Subject: [PATCH] fix: Print command output if mage is not running in verbose mode (#23) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Default log output for precommit: ``` › mage lint:precommit Trim Trailing Whitespace.................................................Passed Mixed line ending........................................................Passed Don't commit to branch...................................................Passed Check for added large files..............................................Passed Check for case conflicts.................................................Passed Check for merge conflicts................................................Passed Check that executables have shebangs.................(no files to check)Skipped Check for broken symlinks............................(no files to check)Skipped Fix End of Files.........................................................Passed Non-executable shell script filename ends in .sh.....(no files to check)Skipped shellcheck...........................................(no files to check)Skipped ``` --- dagger/precommit/dagger.go | 18 ++++++------------ mage/precommit/mage.go | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/dagger/precommit/dagger.go b/dagger/precommit/dagger.go index 4f710de..92e33cf 100644 --- a/dagger/precommit/dagger.go +++ b/dagger/precommit/dagger.go @@ -2,7 +2,6 @@ package precommit import ( "context" - "dagger.io/dagger" "github.com/mesosphere/daggers/dagger/options" @@ -14,7 +13,7 @@ const ( precommitHomeEnvVar = "PRE_COMMIT_HOME" ) -func Run(ctx context.Context, client *dagger.Client, workdir *dagger.Directory, opts ...Option) error { +func Run(ctx context.Context, client *dagger.Client, workdir *dagger.Directory, opts ...Option) (string, error) { cfg := defaultConfig() for _, o := range opts { cfg = o(cfg) @@ -22,7 +21,7 @@ func Run(ctx context.Context, client *dagger.Client, workdir *dagger.Directory, srcDirID, err := workdir.ID(ctx) if err != nil { - return err + return "", err } // Create a pre-commit container @@ -32,7 +31,7 @@ func Run(ctx context.Context, client *dagger.Client, workdir *dagger.Directory, for _, c := range cfg.containerCustomizers { container, err = c(container, client) if err != nil { - return err + return "", err } } @@ -42,14 +41,14 @@ func Run(ctx context.Context, client *dagger.Client, workdir *dagger.Directory, "/usr/local/bin/pre-commit-2.20.0.pyz", )(container, client) if err != nil { - return err + return "", err } container, err = options.CacheDirectoryWithKeyFromFileHash( ctx, cacheDir, "precommit-hooks-", configFileName, )(container, client) if err != nil { - return err + return "", err } container = container.WithEnvVariable(precommitHomeEnvVar, cacheDir). @@ -62,10 +61,5 @@ func Run(ctx context.Context, client *dagger.Client, workdir *dagger.Directory, }) // Run container and get Exit code - _, err = container.ExitCode(ctx) - if err != nil { - return err - } - - return nil + return container.Stdout().Contents(ctx) } diff --git a/mage/precommit/mage.go b/mage/precommit/mage.go index 29163e1..311d769 100644 --- a/mage/precommit/mage.go +++ b/mage/precommit/mage.go @@ -2,6 +2,7 @@ package precommit import ( "context" + "fmt" "os" "dagger.io/dagger" @@ -43,5 +44,17 @@ func PrecommitWithOptions(ctx context.Context, opts ...precommitdagger.Option) e opts = append([]precommitdagger.Option{precommitdagger.BaseImage(baseImage)}, opts...) } - return precommitdagger.Run(ctx, client, client.Host().Workdir().Read(), opts...) + cmdOut, err := precommitdagger.Run(ctx, client, client.Host().Workdir().Read(), opts...) + + // When verbose flag is false, the output is not printed to the console, only redirected to the log file. + // To work around this, we print the output to the console if the verbose flag is not set. + if !verbose { + fmt.Println(cmdOut) + } + + if err != nil { + return err + } + + return nil }