Skip to content

Commit

Permalink
utils: clean up and remove stdlib duplication (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow authored and brennanjl committed Feb 26, 2024
1 parent cfad037 commit 59765fd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 48 deletions.
4 changes: 2 additions & 2 deletions cmd/kwil-cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func PersistConfig(conf *KwilCliConfig) error {
return fmt.Errorf("failed to marshal config: %w", err)
}

file, err := utils.CreateOrOpenFile(DefaultConfigFile, os.O_CREATE|os.O_RDWR)
file, err := utils.CreateOrOpenFile(DefaultConfigFile)
if err != nil {
return fmt.Errorf("failed to create or open config file: %w", err)
}
Expand All @@ -78,7 +78,7 @@ func PersistConfig(conf *KwilCliConfig) error {
}

func LoadPersistedConfig() (*KwilCliConfig, error) {
bts, err := utils.ReadOrCreateFile(DefaultConfigFile, os.O_CREATE|os.O_RDWR)
bts, err := utils.ReadOrCreateFile(DefaultConfigFile)
if err != nil {
return nil, fmt.Errorf("failed to create or open config file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/snapshots/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (s *Snapshotter) ListSnapshots() ([]Snapshot, error) {
// Returns the chunk of index chunkID from snapshot at given height
func (s *Snapshotter) LoadSnapshotChunk(height uint64, format uint32, chunkID uint32) ([]byte, error) {
chunkFile := s.chunkFilePath(chunkID)
chunk, err := utils.ReadFile(chunkFile)
chunk, err := os.ReadFile(chunkFile)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/snapshots/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s *Snapshotter) writeSnapshotFile() error {
}

func (s *Snapshotter) ReadSnapshotFile(filePath string) (*Snapshot, error) {
bts, err := utils.ReadFile(filePath)
bts, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand Down
50 changes: 6 additions & 44 deletions pkg/utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ func CreateDirIfNeeded(path string) error {
return os.MkdirAll(path, 0755)
}

func DeleteDir(path string) error {
return os.RemoveAll(path)
}

func ReadOrCreateFile(path string, permissions int) ([]byte, error) {
func ReadOrCreateFile(path string) ([]byte, error) {
dir := filepath.Dir(path)
if err := CreateDirIfNeeded(dir); err != nil {
return nil, err
}

file, err := os.OpenFile(path, permissions, 0644)
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, err
}
Expand All @@ -35,58 +31,24 @@ func ReadOrCreateFile(path string, permissions int) ([]byte, error) {
return data, nil
}

func CreateOrOpenFile(path string, permissions int) (*os.File, error) {
func CreateOrOpenFile(path string) (*os.File, error) {
dir := filepath.Dir(path)
if err := CreateDirIfNeeded(dir); err != nil {
return nil, err
}

file, err := os.OpenFile(path, permissions, 0644)
if err != nil {
return nil, err
}

return file, nil
}

func OpenFile(path string, permissions int) (*os.File, error) {
file, err := os.OpenFile(path, permissions, 0644)
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, err
}

return file, nil
}

func ReadFile(path string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
return nil, err
}

return data, nil
}
// NOTE: os.ReadFile requires no wrapper.

func WriteFile(path string, data []byte) error {
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return err
}
return nil
}

func FileStat(path string) (os.FileInfo, error) {
return os.Stat(path)
return os.WriteFile(path, data, 0644)
}

func HashFile(path string) ([]byte, error) {
Expand Down

0 comments on commit 59765fd

Please sign in to comment.