From fbd4357afed9f581d4029de325f4f6fde15d752c Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 10 Sep 2022 13:09:39 -0500 Subject: [PATCH] Added deferred fd close for checkIfImage, and deferred getting filenames until after checking if file is an image --- cmd/files.go | 8 +++----- cmd/version.go | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index f2f6174..fcff148 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -19,6 +19,7 @@ func checkIfImage(path string) (bool, error) { if err != nil { return false, err } + defer file.Close() head := make([]byte, 261) file.Read(head) @@ -102,17 +103,14 @@ func pickFile(fileList []string) (string, string, error) { rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] }) - var filePath string - var fileName string - for i := 0; i < len(fileList); i++ { - filePath = fileList[i] - fileName = filepath.Base(filePath) + filePath := fileList[i] isImage, err := checkIfImage(filePath) if err != nil { return "", "", err } if isImage { + fileName := filepath.Base(filePath) return fileName, filePath, nil } } diff --git a/cmd/version.go b/cmd/version.go index 50fd336..36e8470 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.3.0" +var Version = "0.3.1" func init() { rootCmd.AddCommand(versionCmd)