Skip to content

Commit

Permalink
Fix complaints using printf from go vet
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Rutkowski <[email protected]>
  • Loading branch information
mrutkows committed Nov 7, 2024
1 parent 86e0414 commit 4f79fec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
27 changes: 14 additions & 13 deletions cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package cmd

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -260,14 +261,14 @@ func processPatchRecords(bomDocument *schema.BOM, patchDocument *IETF6902Documen
// which does not make sense...
if record.Path == "" {
// TODO: make this a declared error type that can be tested
return fmt.Errorf("invalid IETF RFC 6902 patch operation. \"path\" is empty")
return errors.New("invalid IETF RFC 6902 patch operation. \"path\" is empty")
}

var keys []string
jsonMap := bomDocument.GetJSONMap()

if jsonMap == nil {
return fmt.Errorf("invalid json document (nil)")
return errors.New("invalid json document (nil)")
}

if keys, err = parseMapKeysFromPath(record.Path); err != nil {
Expand All @@ -276,14 +277,14 @@ func processPatchRecords(bomDocument *schema.BOM, patchDocument *IETF6902Documen

lengthKeys := len(keys)
if lengthKeys == 0 {
return fmt.Errorf("invalid document path (nil)")
return errors.New("invalid document path (nil)")
}

switch record.Operation {
case IETF_RFC6902_OP_ADD:
if record.Value == nil {
// TODO: make this a declared error type that can be tested
return fmt.Errorf("invalid IETF RFC 6902 patch operation. \"value\" missing")
return errors.New("invalid IETF RFC 6902 patch operation. \"value\" missing")
}
if err = addOrReplaceValue(jsonMap, keys, record.Value, false); err != nil {
return
Expand All @@ -293,7 +294,7 @@ func processPatchRecords(bomDocument *schema.BOM, patchDocument *IETF6902Documen
// the target "key" MUST exist...
if record.Value == nil {
// TODO: make this a declared error type that can be tested
return fmt.Errorf("invalid IETF RFC 6902 patch operation. \"value\" missing")
return errors.New("invalid IETF RFC 6902 patch operation. \"value\" missing")
}
if err = addOrReplaceValue(jsonMap, keys, record.Value, true); err != nil {
return
Expand Down Expand Up @@ -335,7 +336,7 @@ func processPatchRecords(bomDocument *schema.BOM, patchDocument *IETF6902Documen
func parseMapKeysFromPath(path string) (keys []string, err error) {
// first char SHOULD be a forward slash, if not error
if path == "" || path[0] != '/' {
err = fmt.Errorf("invalid path. Path must begin with forward slash")
err = errors.New("invalid path. Path must begin with forward slash")
return
}
// parse out paths ignoring leading forward slash character
Expand Down Expand Up @@ -406,7 +407,7 @@ func testValue(parentMap map[string]interface{}, keys []string, value interface{

switch lengthKeys {
case 0:
err = fmt.Errorf("invalid map key (nil)")
err = errors.New("invalid map key (nil)")
return
case 1: // special case of adding new key/value to document root
nextNode = parentMap
Expand Down Expand Up @@ -438,7 +439,7 @@ func testValue(parentMap map[string]interface{}, keys []string, value interface{
case []interface{}:
if lengthKeys != 2 {
// TODO: create a formal error type for this
err = fmt.Errorf("invalid path. IETF RFC 6901 does not permit paths after array indices")
err = errors.New("invalid path. IETF RFC 6901 does not permit paths after array indices")
return
}
var arrayIndex int
Expand Down Expand Up @@ -516,7 +517,7 @@ func removeValue(parentMap map[string]interface{}, keys []string, value interfac

switch lengthKeys {
case 0:
return fmt.Errorf("invalid map key (nil)")
return errors.New("invalid map key (nil)")
case 1: // special case of adding new key/value to document root
nextNode = parentMap
default: // adding keys/values along document path
Expand All @@ -542,7 +543,7 @@ func removeValue(parentMap map[string]interface{}, keys []string, value interfac
}
case []interface{}:
if lengthKeys != 2 {
err = fmt.Errorf("invalid path. IETF RFC 6901 does not permit paths after array indices")
err = errors.New("invalid path. IETF RFC 6901 does not permit paths after array indices")
return
}

Expand Down Expand Up @@ -595,7 +596,7 @@ func addOrReplaceValue(parentMap map[string]interface{}, keys []string, value in

switch lengthKeys {
case 0:
return fmt.Errorf("invalid map key (nil)")
return errors.New("invalid map key (nil)")
case 1: // special case of adding new key/value to document root
nextNode = parentMap
default: // adding keys/values along document path
Expand All @@ -619,14 +620,14 @@ func addOrReplaceValue(parentMap map[string]interface{}, keys []string, value in
// to the next node's map with the provided value
currentKey := keys[lengthKeys-1]
if _, exists := typedNode[currentKey]; !exists && replace {
err = fmt.Errorf(ERR_PATCH_REPLACE_PATH_EXISTS)
err = errors.New(ERR_PATCH_REPLACE_PATH_EXISTS)
return
}
typedNode[currentKey] = value
}
case []interface{}:
if lengthKeys != 2 {
err = fmt.Errorf("invalid path. IETF RFC 6901 does not permit paths after array indices")
err = errors.New("invalid path. IETF RFC 6901 does not permit paths after array indices")
return
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package cmd
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"strconv"
Expand Down Expand Up @@ -188,18 +189,18 @@ func Query(writer io.Writer, request *common.QueryRequest, response *common.Quer

// Assure we have a map to dereference
if document.GetJSONMap() == nil {
err = fmt.Errorf(ERR_TYPE_INVALID_JSON_MAP)
err = errors.New(ERR_TYPE_INVALID_JSON_MAP)
return
}

// Validate we have query request/response structs
if request == nil {
err = fmt.Errorf(common.MSG_QUERY_INVALID_REQUEST)
err = errors.New(common.MSG_QUERY_INVALID_REQUEST)
return
}

if response == nil {
err = fmt.Errorf(common.MSG_QUERY_INVALID_RESPONSE)
err = errors.New(common.MSG_QUERY_INVALID_RESPONSE)
return
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd

import (
"encoding/csv"
"errors"
"fmt"
"io"
"sort"
Expand Down Expand Up @@ -347,7 +348,7 @@ func DisplayResourceListCSV(bom *schema.BOM, writer io.Writer) (err error) {
// unable to emit an error message into output stream
return getLogger().Errorf("error writing to output (%v): %s", currentRow, err)
}
return fmt.Errorf(currentRow[0])
return errors.New(MSG_OUTPUT_NO_RESOURCES_FOUND)
}

// Sort resources prior to outputting
Expand Down Expand Up @@ -392,7 +393,7 @@ func DisplayResourceListMarkdown(bom *schema.BOM, writer io.Writer) (err error)
// Emit no resource found warning into output
if len(entries) == 0 {
fmt.Fprintf(writer, "%s\n", MSG_OUTPUT_NO_RESOURCES_FOUND)
return fmt.Errorf(MSG_OUTPUT_NO_RESOURCES_FOUND)
return errors.New(MSG_OUTPUT_NO_RESOURCES_FOUND)
}

// Sort resources prior to outputting
Expand Down
5 changes: 3 additions & 2 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd

import (
"encoding/csv"
"errors"
"fmt"
"io"
"sort"
Expand Down Expand Up @@ -286,7 +287,7 @@ func DisplaySchemasMarkdown(writer io.Writer, filteredSchemas []schema.FormatSch
// Emit no schemas found warning into output
if len(filteredSchemas) == 0 {
fmt.Fprintf(writer, "%s\n", MSG_OUTPUT_NO_SCHEMAS_FOUND)
return fmt.Errorf(MSG_OUTPUT_NO_SCHEMAS_FOUND)
return errors.New(MSG_OUTPUT_NO_SCHEMAS_FOUND)
}

// Sort by Format, Version, Variant
Expand Down Expand Up @@ -334,7 +335,7 @@ func DisplaySchemasCSV(writer io.Writer, filteredSchemas []schema.FormatSchemaIn
if err = w.Write(currentRow); err != nil {
return getLogger().Errorf("error writing to output (%v): %s", currentRow, err)
}
return fmt.Errorf(currentRow[0])
return errors.New(MSG_OUTPUT_NO_SCHEMAS_FOUND)
}

// Sort by Format, Version, Variant
Expand Down
3 changes: 2 additions & 1 deletion cmd/vulnerability.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd

import (
"encoding/csv"
"errors"
"fmt"
"io"
"sort"
Expand Down Expand Up @@ -385,7 +386,7 @@ func DisplayVulnListMarkdown(bom *schema.BOM, writer io.Writer, flags utils.Vuln
// Emit no vuln. found warning into output
if len(entries) == 0 {
fmt.Fprintf(writer, "%s\n", MSG_OUTPUT_NO_VULNERABILITIES_FOUND)
return fmt.Errorf(MSG_OUTPUT_NO_VULNERABILITIES_FOUND)
return errors.New(MSG_OUTPUT_NO_VULNERABILITIES_FOUND)
}

// Sort vulnerabilities prior to outputting
Expand Down

0 comments on commit 4f79fec

Please sign in to comment.