Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions pkg/crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 18 additions & 6 deletions pkg/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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())
Expand Down
14 changes: 12 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading