From 86f8e8a20184bb249e8afcd403a7054d1f4f416b Mon Sep 17 00:00:00 2001 From: Javad Date: Sun, 29 Dec 2024 08:42:53 +0330 Subject: [PATCH] fix: used readfile to get password from file --- cmd/daemon/start.go | 4 +++- util/utils.go | 23 ------------------ util/utils_test.go | 58 --------------------------------------------- 3 files changed, 3 insertions(+), 82 deletions(-) diff --git a/cmd/daemon/start.go b/cmd/daemon/start.go index cb117c7f9..fe2efc737 100644 --- a/cmd/daemon/start.go +++ b/cmd/daemon/start.go @@ -3,6 +3,7 @@ package main import ( "os" "path/filepath" + "strings" "github.com/gofrs/flock" "github.com/pactus-project/pactus/cmd" @@ -57,10 +58,11 @@ func buildStartCmd(parentCmd *cobra.Command) { if *passwordOpt != "" { password = *passwordOpt } else if *passwordFromFileOpt != "" { - password, err = util.ReadFileContent(*passwordFromFileOpt) + b, err := util.ReadFile(*passwordFromFileOpt) if err != nil { return "", false } + password = strings.TrimSpace(string(b)) } else { password = cmd.PromptPassword("Wallet password", false) } diff --git a/util/utils.go b/util/utils.go index 55b3bad51..ba58dc8b4 100644 --- a/util/utils.go +++ b/util/utils.go @@ -3,11 +3,8 @@ package util import ( crand "crypto/rand" "fmt" - "io" "math/big" "math/bits" - "os" - "strings" "golang.org/x/exp/constraints" ) @@ -163,23 +160,3 @@ func FormatBytesToHumanReadable(bytes uint64) string { return fmt.Sprintf("%.2f %s", value, unit) } - -// ReadFileContent reads the content of the file at the given path -// up to the specified maxSize. It returns the content as a string -// and any error encountered. -func ReadFileContent(filePath string) (string, error) { - file, err := os.Open(filePath) - if err != nil { - return "", fmt.Errorf("failed to open file: %w", err) - } - defer func() { - _ = file.Close() - }() - - buf, err := io.ReadAll(file) - if err != nil { - return "", fmt.Errorf("failed to read file content: %w", err) - } - - return strings.TrimSpace(string(buf)), nil -} diff --git a/util/utils_test.go b/util/utils_test.go index 73a8c8f9b..7def6e3e0 100644 --- a/util/utils_test.go +++ b/util/utils_test.go @@ -2,7 +2,6 @@ package util import ( "math/big" - "os" "testing" "github.com/stretchr/testify/assert" @@ -140,60 +139,3 @@ func TestFormatBytesToHumanReadable(t *testing.T) { } } } - -func TestReadFileContent(t *testing.T) { - testCases := []struct { - name string - fileContent string - expected string - expectErr bool - }{ - { - name: "Read full content", - fileContent: "Hello, World!", - expected: "Hello, World!", - expectErr: false, - }, - { - name: "Empty file", - fileContent: "", - expected: "", - expectErr: false, - }, - { - name: "Non-existent file", - fileContent: "", - expected: "", - expectErr: true, - }, - } - - for _, test := range testCases { - t.Run(test.name, func(t *testing.T) { - var filePath string - if test.fileContent != "" || test.name == "Empty file" { - tmpFile, err := os.CreateTemp(TempDirPath(), "testfile") - assert.NoError(t, err, "Failed to create temp file") - defer func() { - _ = os.Remove(tmpFile.Name()) - }() - - _, err = tmpFile.WriteString(test.fileContent) - assert.NoError(t, err, "Failed to write to temp file") - _ = tmpFile.Close() - filePath = tmpFile.Name() - } else { - filePath = "nonexistent_file" - } - - content, err := ReadFileContent(filePath) - - if test.expectErr { - assert.Error(t, err, "Expected an error but got none") - } else { - assert.NoError(t, err, "Unexpected error occurred") - assert.Equal(t, test.expected, content, "Content does not match expected value") - } - }) - } -}