From c4cdfd2cc11b518804fb8302cadacc761fdef0e5 Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Wed, 25 Mar 2026 17:40:40 +1100 Subject: [PATCH] Fix all golangci-lint errcheck and unused warnings - Wrap defer resp.Body.Close() to capture error - Ignore json.Unmarshal error in valueAny (best-effort) - Check enc.Encode and os.Stdout.Write return values - Check json.NewEncoder.Encode and w.Write in test helpers - Check os.MkdirAll and os.WriteFile errors in tests - Check first writeDefaultConfig() call in AlreadyExists test - Remove unused mustMarshal helper --- main.go | 8 ++++---- main_test.go | 40 +++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index b6ff5a1..1595493 100644 --- a/main.go +++ b/main.go @@ -150,7 +150,7 @@ func queryPuppetDB(puppetDBURL, query string) ([]fact, error) { if err != nil { return nil, fmt.Errorf("request failed: %w", err) } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) @@ -176,7 +176,7 @@ func valueString(raw json.RawMessage) string { func valueAny(raw json.RawMessage) interface{} { var v interface{} - json.Unmarshal(raw, &v) + _ = json.Unmarshal(raw, &v) return v } @@ -280,7 +280,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") enc.SetEscapeHTML(false) - enc.Encode(hostFactMap) + _ = enc.Encode(hostFactMap) case count: values := stdinLines @@ -299,7 +299,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n "all": map[string]interface{}{"hosts": hosts}, } b, _ := yaml.Marshal(inventory) - os.Stdout.Write(b) + _, _ = os.Stdout.Write(b) case nodeOnly: for _, line := range returnData { diff --git a/main_test.go b/main_test.go index 49cc8fe..ca6df71 100644 --- a/main_test.go +++ b/main_test.go @@ -12,19 +12,11 @@ import ( // ---- helpers ---------------------------------------------------------------- -func mustMarshal(v interface{}) []byte { - b, err := json.Marshal(v) - if err != nil { - panic(err) - } - return b -} - func newTestServer(t *testing.T, facts []fact) *httptest.Server { t.Helper() return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(facts) + _ = json.NewEncoder(w).Encode(facts) })) } @@ -183,7 +175,7 @@ func TestQueryPuppetDB_HTTPError(t *testing.T) { func TestQueryPuppetDB_BadJSON(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("not json")) + _, _ = w.Write([]byte("not json")) })) defer srv.Close() @@ -243,8 +235,12 @@ func TestLoadConfig_FileOverride(t *testing.T) { t.Setenv("NODE_LOOKUP_ROLE_FACT", "") cfgDir := filepath.Join(dir, appName) - os.MkdirAll(cfgDir, 0o755) - os.WriteFile(filepath.Join(cfgDir, configFileName), []byte("puppetdb_url: http://file:8080/facts\nrole_fact: file_role\n"), 0o644) + if err := os.MkdirAll(cfgDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(cfgDir, configFileName), []byte("puppetdb_url: http://file:8080/facts\nrole_fact: file_role\n"), 0o644); err != nil { + t.Fatal(err) + } cfg, err := loadConfig() if err != nil { @@ -265,8 +261,12 @@ func TestLoadConfig_EnvOverridesFile(t *testing.T) { t.Setenv("NODE_LOOKUP_ROLE_FACT", "") cfgDir := filepath.Join(dir, appName) - os.MkdirAll(cfgDir, 0o755) - os.WriteFile(filepath.Join(cfgDir, configFileName), []byte("puppetdb_url: http://file:8080/facts\n"), 0o644) + if err := os.MkdirAll(cfgDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(cfgDir, configFileName), []byte("puppetdb_url: http://file:8080/facts\n"), 0o644); err != nil { + t.Fatal(err) + } cfg, err := loadConfig() if err != nil { @@ -284,8 +284,12 @@ func TestLoadConfig_InvalidYAML(t *testing.T) { t.Setenv("NODE_LOOKUP_ROLE_FACT", "") cfgDir := filepath.Join(dir, appName) - os.MkdirAll(cfgDir, 0o755) - os.WriteFile(filepath.Join(cfgDir, configFileName), []byte(":\tinvalid: yaml:\n"), 0o644) + if err := os.MkdirAll(cfgDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(cfgDir, configFileName), []byte(":\tinvalid: yaml:\n"), 0o644); err != nil { + t.Fatal(err) + } _, err := loadConfig() if err == nil { @@ -315,7 +319,9 @@ func TestWriteDefaultConfig_AlreadyExists(t *testing.T) { dir := t.TempDir() t.Setenv("XDG_CONFIG_HOME", dir) - writeDefaultConfig() + if err := writeDefaultConfig(); err != nil { + t.Fatal(err) + } err := writeDefaultConfig() if err == nil { t.Fatal("expected error when config already exists")