From 85cff22da4c0663f6e3440fbaa1e4fc87eb22e17 Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Sun, 21 Apr 2024 23:11:17 -0700 Subject: [PATCH] fix lint errors --- cmd/cli/main.go | 22 +++++++++++++++++----- internal/resource/document.go | 5 ++++- internal/resource/file_test.go | 3 ++- internal/signature/ident.go | 4 +++- internal/source/http.go | 18 ++++++------------ 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 5555146..0c0a3c9 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -83,7 +83,11 @@ func LoadSourceURI(uri string) []*resource.Document { func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) { ImportResource = cmd.String("resource", "", "(uri) Add a resource to the document.") ImportMerge = cmd.Bool("merge", false, "Merge resources into a single document.") - cmd.Parse(os.Args[2:]) + e := cmd.Parse(os.Args[2:]) + if e != nil { // returns ErrHelp + return e + } + var encoder resource.Encoder merged := resource.NewDocument() documents := make([]*resource.Document, 0, 100) @@ -117,7 +121,9 @@ func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) { if *GlobalQuiet { for _, dr := range d.Resources() { - output.Write([]byte(dr.Resource().URI())) + if _,e := output.Write([]byte(dr.Resource().URI())); e != nil { + return e + } } } else { if *ImportMerge { @@ -140,7 +146,9 @@ func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) { } func ApplySubCommand(cmd *flag.FlagSet, output io.Writer) (err error) { - cmd.Parse(os.Args[2:]) + if e := cmd.Parse(os.Args[2:]); e != nil { + return e + } var encoder resource.Encoder documents := make([]*resource.Document, 0, 100) for _,source := range cmd.Args() { @@ -165,7 +173,9 @@ func ApplySubCommand(cmd *flag.FlagSet, output io.Writer) (err error) { } if *GlobalQuiet { for _, dr := range d.Resources() { - output.Write([]byte(dr.Resource().URI())) + if _,e := output.Write([]byte(dr.Resource().URI())); e != nil { + return e + } } } else { if documentGenerateErr := encoder.Encode(d); documentGenerateErr != nil { @@ -177,7 +187,9 @@ func ApplySubCommand(cmd *flag.FlagSet, output io.Writer) (err error) { } func DiffSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) { - cmd.Parse(os.Args[2:]) + if e := cmd.Parse(os.Args[2:]); e != nil { + return e + } leftSource := cmd.Arg(0) rightSource := cmd.Arg(1) leftDocuments := make([]*resource.Document, 0, 100) diff --git a/internal/resource/document.go b/internal/resource/document.go index b384fbe..462e492 100644 --- a/internal/resource/document.go +++ b/internal/resource/document.go @@ -137,7 +137,10 @@ func (d *Document) Diff(with *Document, output io.Writer) (string, error) { for _,diff := range yamldiff.Do(yamlDiff, withDiff, opts...) { slog.Info("Diff()", "diff", diff) fmt.Printf("yaml %#v with %#v\n", yamlDiff, withDiff) - output.Write([]byte(diff.Dump())) + _,e := output.Write([]byte(diff.Dump())) + if e != nil { + return "", e + } } slog.Info("Document.Diff() ", "document.yaml", ydata, "with.yaml", wdata) if stringOutput, ok := output.(*strings.Builder); ok { diff --git a/internal/resource/file_test.go b/internal/resource/file_test.go index 326562b..a25e04e 100644 --- a/internal/resource/file_test.go +++ b/internal/resource/file_test.go @@ -215,7 +215,8 @@ func TestFileUpdateAttributesFromFileInfo(t *testing.T) { info, e := os.Lstat(TempDir) assert.Nil(t, e) - f.UpdateAttributesFromFileInfo(info) + updateAttributesErr := f.UpdateAttributesFromFileInfo(info) + assert.Nil(t, updateAttributesErr) assert.Equal(t, DirectoryFile, f.FileType) } diff --git a/internal/signature/ident.go b/internal/signature/ident.go index c31f37a..0f16a2a 100644 --- a/internal/signature/ident.go +++ b/internal/signature/ident.go @@ -25,7 +25,9 @@ type Ident struct { func NewIdent() *Ident { i := &Ident{} i.authorized = []string{ "*" } - i.Generate() + if e := i.Generate(); e != nil { + return nil + } return i } diff --git a/internal/source/http.go b/internal/source/http.go index 436b3a5..07c5cc2 100644 --- a/internal/source/http.go +++ b/internal/source/http.go @@ -39,7 +39,6 @@ func (d *HTTP) Type() string { return "http" } func (h *HTTP) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) { documents := make([]*resource.Document, 0, 100) - documents = append(documents, resource.NewDocument()) resp, err := http.Get(h.Endpoint) if err != nil { @@ -67,12 +66,9 @@ func (h *HTTP) ExtractResources(filter ResourceSelector) ([]*resource.Document, decoder := resource.NewYAMLDecoder(sumReadData) index := 0 for { - doc := documents[index] + doc := resource.NewDocument() e := decoder.Decode(doc) if errors.Is(e, io.EOF) { - if len(documents) > 1 { - documents[index] = nil - } break } if e != nil { @@ -81,18 +77,16 @@ func (h *HTTP) ExtractResources(filter ResourceSelector) ([]*resource.Document, if validationErr := doc.Validate(); validationErr != nil { return documents, validationErr } -/* - if applyErr := doc.Apply(); applyErr != nil { - return documents, applyErr - } -*/ - documents = append(documents, resource.NewDocument()) + documents = append(documents, doc) index++ } if documentSignature != "" { sig := &signature.Ident{} - sig.VerifySum(hash.Sum(nil), []byte(documentSignature)) + sigErr := sig.VerifySum(hash.Sum(nil), []byte(documentSignature)) + if sigErr != nil { + return documents, sigErr + } } return documents, nil