diff --git a/pkg/crud_test.go b/pkg/crud_test.go index b1e414f..b103d57 100644 --- a/pkg/crud_test.go +++ b/pkg/crud_test.go @@ -35,7 +35,9 @@ func TestInstall(t *testing.T) { pathBin, err := os.MkdirTemp("", "test-dir") require.Nil(t, err) - defer os.RemoveAll(pathBin) + defer func() { + require.NoError(t, os.RemoveAll(pathBin)) + }() // install first time err = Install(pathBin, tool) @@ -51,7 +53,9 @@ func TestRemove(t *testing.T) { pathBin, err := os.MkdirTemp("", "test-dir") require.Nil(t, err) - defer os.RemoveAll(pathBin) + defer func() { + require.NoError(t, os.RemoveAll(pathBin)) + }() // install the tool err = Install(pathBin, tool) @@ -71,7 +75,9 @@ func TestUpdateSameVersion(t *testing.T) { pathBin, err := os.MkdirTemp("", "test-dir") require.Nil(t, err) - defer os.RemoveAll(pathBin) + defer func() { + require.NoError(t, os.RemoveAll(pathBin)) + }() // install the tool err = Install(pathBin, tool) @@ -87,7 +93,9 @@ func TestUpdateNonExistingTool(t *testing.T) { pathBin, err := os.MkdirTemp("", "test-dir") require.Nil(t, err) - defer os.RemoveAll(pathBin) + defer func() { + require.NoError(t, os.RemoveAll(pathBin)) + }() // updating non existing tool should error err = Update(pathBin, tool, true) @@ -99,7 +107,9 @@ func TestUpdateToolWithoutAssets(t *testing.T) { pathBin, err := os.MkdirTemp("", "test-dir") require.Nil(t, err) - defer os.RemoveAll(pathBin) + defer func() { + require.NoError(t, os.RemoveAll(pathBin)) + }() // install the tool err = Install(pathBin, tool) diff --git a/pkg/install.go b/pkg/install.go index a006cc8..240a556 100644 --- a/pkg/install.go +++ b/pkg/install.go @@ -114,7 +114,11 @@ loop: return "", err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + gologger.Warning().Msgf("Error closing response body: %s", err) + } + }() if resp.StatusCode != 200 { return "", err } @@ -167,7 +171,11 @@ func downloadTar(reader io.Reader, toolName, path string) error { if err != nil { return err } - defer dstFile.Close() + defer func() { + if err := dstFile.Close(); err != nil { + gologger.Warning().Msgf("Error closing file: %s", err) + } + }() // copy the file data from the archive _, err = io.Copy(dstFile, tarReader) if err != nil { @@ -224,8 +232,12 @@ func downloadZip(reader io.Reader, toolName, path string) error { return err } - dstFile.Close() - fileInArchive.Close() + if err := dstFile.Close(); err != nil { + gologger.Warning().Msgf("Error closing file: %s", err) + } + if err := fileInArchive.Close(); err != nil { + gologger.Warning().Msgf("Error closing file in archive: %s", err) + } } return nil } @@ -240,12 +252,12 @@ func printRequirementInfo(tool types.Tool) { continue } if printTitle { - stringBuilder.WriteString(fmt.Sprintf("%s\n", au.Bold(tool.Name+" requirements:").String())) + fmt.Fprintf(stringBuilder, "%s\n", au.Bold(tool.Name+" requirements:").String()) printTitle = false } instruction := getFormattedInstruction(spec) isRequired := getRequirementStatus(spec) - stringBuilder.WriteString(fmt.Sprintf("%s %s\n", isRequired, instruction)) + fmt.Fprintf(stringBuilder, "%s %s\n", isRequired, instruction) } if stringBuilder.Len() > 0 { gologger.Info().Msgf("%s", stringBuilder.String()) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 87f45d0..89cc86a 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -38,7 +38,12 @@ func FetchToolList() ([]types.Tool, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + // Just log warning as we're already returning from function + fmt.Printf("Error closing response body: %s\n", err) + } + }() if resp.StatusCode == http.StatusOK { body, err := io.ReadAll(resp.Body) @@ -62,7 +67,12 @@ func fetchTool(toolName string) (types.Tool, error) { if err != nil { return tool, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + // Just log warning as we're already returning from function + fmt.Printf("Error closing response body: %s\n", err) + } + }() if resp.StatusCode == http.StatusOK { body, err := io.ReadAll(resp.Body)