Skip to content

Commit

Permalink
Fix output-file flag. (#1264)
Browse files Browse the repository at this point in the history
The writer was getting closed prematurely in the PreRun command. We need to save
this as a temp variable at the command level and close the writer in PostRun.

Signed-off-by: Dan Lorenc <lorenc.d@gmail.com>
  • Loading branch information
dlorenc committed Jan 2, 2022
1 parent 9a27e1f commit d89eb8e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
19 changes: 13 additions & 6 deletions cmd/cosign/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ var (
)

func New() *cobra.Command {
var (
out, stdout *os.File
)

cmd := &cobra.Command{
Use: "cosign",
DisableAutoGenTag: true,
SilenceUsage: true, // Don't show usage on errors
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if ro.OutputFile != "" {
out, err := os.Create(ro.OutputFile)
var err error
out, err = os.Create(ro.OutputFile)
if err != nil {
return errors.Wrapf(err, "Error creating output file %s", ro.OutputFile)
}
stdout := os.Stdout
defer func() {
os.Stdout = stdout
_ = out.Close()
}()
stdout = os.Stdout
os.Stdout = out // TODO: don't do this.
cmd.SetOut(out)
}
Expand All @@ -54,6 +55,12 @@ func New() *cobra.Command {
}
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if out != nil {
_ = out.Close()
}
os.Stdout = stdout
},
}
ro.AddFlags(cmd)

Expand Down
5 changes: 5 additions & 0 deletions test/e2e_test_secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ crane cp gcr.io/distroless/base $multiarch_img
# `initialize`
./cosign initialize

## Generate (also test output redirection
./cosign generate $img > payload1
./cosign generate --output-file=payload2 $img
diff payload1 payload2

## sign/verify
./cosign sign --key ${signing_key} $img
./cosign verify --key ${verification_key} $img
Expand Down

0 comments on commit d89eb8e

Please sign in to comment.