Skip to content

Commit

Permalink
fix: Close output files
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthb committed Jun 28, 2024
1 parent 82ec012 commit 814ced5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
3 changes: 2 additions & 1 deletion cmd/bf/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ var caIssueCmd = &cli.Command{
return cli.Exit("Error issuing certificate", 1)
}

out, err := getOutputWriter()
out, cls, err := getOutputWriter()
if err != nil {
slog.ErrorContext(ctx, "error getting output writer", "error", err)
return cli.Exit("Error getting output writer", 1)
}
defer cls()

block := &pem.Block{
Type: "CERTIFICATE",
Expand Down
8 changes: 5 additions & 3 deletions cmd/bf/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ var (
}
)

func getOutputWriter() (io.Writer, error) {
func getOutputWriter() (io.WriteCloser, func() error, error) {
if outputFile == "" || outputFile == "-" {
return os.Stdout, nil
return os.Stdout, func() error { return nil }, nil
}
return os.Create(outputFile)

f, err := os.Create(outputFile)
return f, f.Close, err
}
33 changes: 20 additions & 13 deletions cmd/bf/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ var newCmd = &cli.Command{
outputFlag,
},
Action: func(_ context.Context, _ *cli.Command) error {
out, err := getOutputWriter()
out, cls, err := getOutputWriter()
if err != nil {
return err
}
fmt.Fprintln(out, uuid.New())
return nil
defer cls()

_, err = out.Write([]byte(uuid.New().String() + "\n"))
return err
},
},
{
Expand All @@ -48,16 +50,20 @@ var newCmd = &cli.Command{
if err != nil {
return err
}

keyText, err := key.MarshalText()
if err != nil {
return err
}
out, err := getOutputWriter()

out, cls, err := getOutputWriter()
if err != nil {
return err
}
fmt.Fprint(out, string(keyText))
return nil
defer cls()

_, err = out.Write(keyText)
return err
},
},
{
Expand Down Expand Up @@ -89,18 +95,18 @@ var newCmd = &cli.Command{
return err
}

out, err := getOutputWriter()
out, cls, err := getOutputWriter()
if err != nil {
return err
}
defer cls()

block := &pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csr,
}
fmt.Fprint(out, string(pem.EncodeToMemory(block)))

return nil
_, err = out.Write(pem.EncodeToMemory(block))
return err
},
},
{
Expand Down Expand Up @@ -148,18 +154,19 @@ var newCmd = &cli.Command{
return err
}

out, err := getOutputWriter()
out, cls, err := getOutputWriter()
if err != nil {
return err
}
defer cls()

block := &pem.Block{
Type: "CERTIFICATE",
Bytes: certDer,
}
fmt.Fprint(out, string(pem.EncodeToMemory(block)))

return nil
_, err = out.Write(pem.EncodeToMemory(block))
return err
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion cmd/bf/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ var requestCmd = &cli.Command{
Bytes: cert.Raw,
}

out, err := getOutputWriter()
out, cls, err := getOutputWriter()
if err != nil {
slog.ErrorContext(ctx, "error opening output file", "error", err)
return cli.Exit("Failed to open output file", 1)
}
defer cls()

if err := pem.Encode(out, block); err != nil {
slog.ErrorContext(ctx, "error writing certificate", "error", err)
Expand Down

0 comments on commit 814ced5

Please sign in to comment.