Fix all golangci-lint errcheck and unused warnings
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-tests Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful

- 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
This commit is contained in:
2026-03-25 17:40:40 +11:00
parent 0ba9a3da09
commit c4cdfd2cc1
2 changed files with 27 additions and 21 deletions
+4 -4
View File
@@ -150,7 +150,7 @@ func queryPuppetDB(puppetDBURL, query string) ([]fact, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("request failed: %w", err) return nil, fmt.Errorf("request failed: %w", err)
} }
defer resp.Body.Close() defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
@@ -176,7 +176,7 @@ func valueString(raw json.RawMessage) string {
func valueAny(raw json.RawMessage) interface{} { func valueAny(raw json.RawMessage) interface{} {
var v interface{} var v interface{}
json.Unmarshal(raw, &v) _ = json.Unmarshal(raw, &v)
return v return v
} }
@@ -280,7 +280,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
enc := json.NewEncoder(os.Stdout) enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ") enc.SetIndent("", " ")
enc.SetEscapeHTML(false) enc.SetEscapeHTML(false)
enc.Encode(hostFactMap) _ = enc.Encode(hostFactMap)
case count: case count:
values := stdinLines values := stdinLines
@@ -299,7 +299,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
"all": map[string]interface{}{"hosts": hosts}, "all": map[string]interface{}{"hosts": hosts},
} }
b, _ := yaml.Marshal(inventory) b, _ := yaml.Marshal(inventory)
os.Stdout.Write(b) _, _ = os.Stdout.Write(b)
case nodeOnly: case nodeOnly:
for _, line := range returnData { for _, line := range returnData {
+23 -17
View File
@@ -12,19 +12,11 @@ import (
// ---- helpers ---------------------------------------------------------------- // ---- 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 { func newTestServer(t *testing.T, facts []fact) *httptest.Server {
t.Helper() t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") 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) { func TestQueryPuppetDB_BadJSON(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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() defer srv.Close()
@@ -243,8 +235,12 @@ func TestLoadConfig_FileOverride(t *testing.T) {
t.Setenv("NODE_LOOKUP_ROLE_FACT", "") t.Setenv("NODE_LOOKUP_ROLE_FACT", "")
cfgDir := filepath.Join(dir, appName) cfgDir := filepath.Join(dir, appName)
os.MkdirAll(cfgDir, 0o755) if err := os.MkdirAll(cfgDir, 0o755); err != nil {
os.WriteFile(filepath.Join(cfgDir, configFileName), []byte("puppetdb_url: http://file:8080/facts\nrole_fact: file_role\n"), 0o644) 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() cfg, err := loadConfig()
if err != nil { if err != nil {
@@ -265,8 +261,12 @@ func TestLoadConfig_EnvOverridesFile(t *testing.T) {
t.Setenv("NODE_LOOKUP_ROLE_FACT", "") t.Setenv("NODE_LOOKUP_ROLE_FACT", "")
cfgDir := filepath.Join(dir, appName) cfgDir := filepath.Join(dir, appName)
os.MkdirAll(cfgDir, 0o755) if err := os.MkdirAll(cfgDir, 0o755); err != nil {
os.WriteFile(filepath.Join(cfgDir, configFileName), []byte("puppetdb_url: http://file:8080/facts\n"), 0o644) 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() cfg, err := loadConfig()
if err != nil { if err != nil {
@@ -284,8 +284,12 @@ func TestLoadConfig_InvalidYAML(t *testing.T) {
t.Setenv("NODE_LOOKUP_ROLE_FACT", "") t.Setenv("NODE_LOOKUP_ROLE_FACT", "")
cfgDir := filepath.Join(dir, appName) cfgDir := filepath.Join(dir, appName)
os.MkdirAll(cfgDir, 0o755) if err := os.MkdirAll(cfgDir, 0o755); err != nil {
os.WriteFile(filepath.Join(cfgDir, configFileName), []byte(":\tinvalid: yaml:\n"), 0o644) t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(cfgDir, configFileName), []byte(":\tinvalid: yaml:\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := loadConfig() _, err := loadConfig()
if err == nil { if err == nil {
@@ -315,7 +319,9 @@ func TestWriteDefaultConfig_AlreadyExists(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir) t.Setenv("XDG_CONFIG_HOME", dir)
writeDefaultConfig() if err := writeDefaultConfig(); err != nil {
t.Fatal(err)
}
err := writeDefaultConfig() err := writeDefaultConfig()
if err == nil { if err == nil {
t.Fatal("expected error when config already exists") t.Fatal("expected error when config already exists")