Skip to content

Commit

Permalink
fix: Print command output if mage is not running in verbose mode (#23)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
aweris authored Nov 4, 2022
1 parent 86032fb commit c749497
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 6 additions & 12 deletions dagger/precommit/dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package precommit

import (
"context"

"dagger.io/dagger"

"github.com/mesosphere/daggers/dagger/options"
Expand All @@ -14,15 +13,15 @@ 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)
}

srcDirID, err := workdir.ID(ctx)
if err != nil {
return err
return "", err
}

// Create a pre-commit container
Expand All @@ -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
}
}

Expand All @@ -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).
Expand All @@ -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)
}
15 changes: 14 additions & 1 deletion mage/precommit/mage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package precommit

import (
"context"
"fmt"
"os"

"dagger.io/dagger"
Expand Down Expand Up @@ -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
}

0 comments on commit c749497

Please sign in to comment.