Skip to content

Commit

Permalink
fix defer closing write closer in function instead of at the place of…
Browse files Browse the repository at this point in the history
… creation
  • Loading branch information
Skarlso committed Oct 14, 2024
1 parent c884d59 commit 27b7d13
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
11 changes: 5 additions & 6 deletions cmd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func runGenerate(_ *cobra.Command, _ []string) error {
}

var w io.WriteCloser
defer func() {
if err := w.Close(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to close output file: %s", err.Error())
}
}()

if crdArgs.format == FormatHTML {
if crdArgs.stdOut {
Expand All @@ -87,12 +92,6 @@ func runGenerate(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}

defer func() {
if err := w.Close(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to close output file: %s", err.Error())
}
}()
}

return pkg.RenderContent(w, crds, crdArgs.comments, crdArgs.minimal)
Expand Down
7 changes: 0 additions & 7 deletions pkg/create_html_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pkg
import (
"bytes"
"embed"
"errors"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -65,12 +64,6 @@ func LoadTemplates() error {

// RenderContent creates an HTML website from the CRD content.
func RenderContent(w io.WriteCloser, crds []*SchemaType, comments, minimal bool) (err error) {
defer func() {
if cerr := w.Close(); cerr != nil {
err = errors.Join(err, cerr)
}
}()

allViews := make([]ViewPage, 0, len(crds))

for _, crd := range crds {
Expand Down
7 changes: 0 additions & 7 deletions pkg/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"errors"
"fmt"
"io"
"regexp"
Expand All @@ -19,12 +18,6 @@ var RootRequiredFields = []string{"apiVersion", "kind", "spec", "metadata"}

// Generate takes a CRD content and path, and outputs.
func Generate(crd *SchemaType, w io.WriteCloser, enableComments, minimal, skipRandom bool) (err error) {
defer func() {
if cerr := w.Close(); cerr != nil {
err = errors.Join(err, cerr)
}
}()

parser := NewParser(crd.Group, crd.Kind, enableComments, minimal, skipRandom)
for i, version := range crd.Versions {
if err := parser.ParseProperties(version.Name, w, version.Schema.Properties); err != nil {
Expand Down

0 comments on commit 27b7d13

Please sign in to comment.