-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from bandprotocol/separate-store-app
add store object
- Loading branch information
Showing
35 changed files
with
1,440 additions
and
721 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package os | ||
|
||
import ( | ||
"os" | ||
"path" | ||
) | ||
|
||
// CheckAndCreateFolder checks if the folder exists and creates it if it doesn't. | ||
func CheckAndCreateFolder(path string) error { | ||
if exist, err := IsPathExist(path); err != nil { | ||
return err | ||
} else if !exist { | ||
return os.Mkdir(path, os.ModePerm) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IsPathExist checks if the path exists. | ||
func IsPathExist(path string) (bool, error) { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return false, nil | ||
} else if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// ReadFileIfExist reads file from the given path. It returns empty data with no error, | ||
// if the file doesn't exist. Otherwise, it returns an error. | ||
func ReadFileIfExist(path string) ([]byte, error) { | ||
if exist, err := IsPathExist(path); err != nil { | ||
return nil, err | ||
} else if !exist { | ||
return nil, nil | ||
} | ||
|
||
return os.ReadFile(path) | ||
} | ||
|
||
// Write writes the given data to the file at the given path. It also creates the folders if they don't exist. | ||
func Write(data []byte, paths []string) error { | ||
// Create folders if they don't exist | ||
folderPath := "" | ||
for _, p := range paths[:len(paths)-1] { | ||
folderPath = path.Join(folderPath, p) | ||
if err := CheckAndCreateFolder(folderPath); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Write the data to the file | ||
filePath := path.Join(folderPath, paths[len(paths)-1]) | ||
f, err := os.Create(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
if _, err = f.Write(data); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package os_test | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
internal_os "github.com/bandprotocol/falcon/internal/os" | ||
) | ||
|
||
func TestCheckAndCreateFolder(t *testing.T) { | ||
tmpDir := path.Join(t.TempDir(), "test") | ||
|
||
// create a folder | ||
err := internal_os.CheckAndCreateFolder(tmpDir) | ||
require.NoError(t, err) | ||
|
||
_, err = os.Stat(tmpDir) | ||
require.NoError(t, err) | ||
|
||
// create a folder again; shouldn't cause any error | ||
err = internal_os.CheckAndCreateFolder(tmpDir) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestIsPathExist(t *testing.T) { | ||
tmpDir := path.Join(t.TempDir(), "test") | ||
|
||
// check if a folder exists, should return false | ||
exist, err := internal_os.IsPathExist(tmpDir) | ||
require.NoError(t, err) | ||
require.False(t, exist) | ||
|
||
err = internal_os.CheckAndCreateFolder(tmpDir) | ||
require.NoError(t, err) | ||
|
||
exist, err = internal_os.IsPathExist(tmpDir) | ||
require.NoError(t, err) | ||
require.True(t, exist) | ||
} | ||
|
||
func TestWrite(t *testing.T) { | ||
tmpDir := path.Join(t.TempDir(), "test") | ||
|
||
// write a file | ||
err := internal_os.Write([]byte("test"), []string{tmpDir, "test.txt"}) | ||
require.NoError(t, err) | ||
|
||
// check if the file exists | ||
exist, err := internal_os.IsPathExist(path.Join(tmpDir, "test.txt")) | ||
require.NoError(t, err) | ||
require.True(t, exist) | ||
|
||
// check if the file contains the correct data | ||
data, err := os.ReadFile(path.Join(tmpDir, "test.txt")) | ||
require.NoError(t, err) | ||
require.Equal(t, "test", string(data)) | ||
|
||
// write a file again; shouldn't cause any error | ||
err = internal_os.Write([]byte("new test"), []string{tmpDir, "test.txt"}) | ||
require.NoError(t, err) | ||
|
||
// check if the file contains the correct data | ||
data, err = os.ReadFile(path.Join(tmpDir, "test.txt")) | ||
require.NoError(t, err) | ||
require.Equal(t, "new test", string(data)) | ||
} | ||
|
||
func TestReadFileIfExist(t *testing.T) { | ||
tmpDir := path.Join(t.TempDir(), "test") | ||
|
||
// check if a file exists, should return nil | ||
data, err := internal_os.ReadFileIfExist(path.Join(tmpDir, "test.txt")) | ||
require.NoError(t, err) | ||
require.Nil(t, data) | ||
|
||
// write a file | ||
err = internal_os.Write([]byte("test"), []string{tmpDir, "test.txt"}) | ||
require.NoError(t, err) | ||
|
||
// check if the file exists | ||
exist, err := internal_os.IsPathExist(path.Join(tmpDir, "test.txt")) | ||
require.NoError(t, err) | ||
require.True(t, exist) | ||
|
||
// check if the file contains the correct data | ||
data, err = internal_os.ReadFileIfExist(path.Join(tmpDir, "test.txt")) | ||
require.NoError(t, err) | ||
require.Equal(t, "test", string(data)) | ||
|
||
// check if a file doesn't exist, should return nil | ||
data, err = internal_os.ReadFileIfExist(path.Join(tmpDir, "non-exist.txt")) | ||
require.NoError(t, err) | ||
require.Nil(t, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.