Skip to content

Commit

Permalink
fix: used readfile to get password from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Dec 29, 2024
1 parent 0fe8e1a commit 86f8e8a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 82 deletions.
4 changes: 3 additions & 1 deletion cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"
"path/filepath"
"strings"

"github.com/gofrs/flock"
"github.com/pactus-project/pactus/cmd"
Expand Down Expand Up @@ -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))

Check warning on line 65 in cmd/daemon/start.go

View check run for this annotation

Codecov / codecov/patch

cmd/daemon/start.go#L60-L65

Added lines #L60 - L65 were not covered by tests
} else {
password = cmd.PromptPassword("Wallet password", false)
}
Expand Down
23 changes: 0 additions & 23 deletions util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package util
import (
crand "crypto/rand"
"fmt"
"io"
"math/big"
"math/bits"
"os"
"strings"

"golang.org/x/exp/constraints"
)
Expand Down Expand Up @@ -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
}
58 changes: 0 additions & 58 deletions util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package util

import (
"math/big"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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")
}
})
}
}

0 comments on commit 86f8e8a

Please sign in to comment.