Skip to content

Commit

Permalink
refactor: prefer filepath package over path
Browse files Browse the repository at this point in the history
  • Loading branch information
ALX99 committed Jun 1, 2024
1 parent 4878314 commit ce6f8d6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"cmp"
"os"
"path"
"path/filepath"

"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -71,5 +71,5 @@ func GetConfig() (Config, error) {

// configPath returns the configuration file location
func configPath(homeDir string) string {
return path.Join(cmp.Or(os.Getenv("XDG_CONFIG_HOME"), path.Join(homeDir, ".config")), "sail", "config.yaml")
return filepath.Join(cmp.Or(os.Getenv("XDG_CONFIG_HOME"), filepath.Join(homeDir, ".config")), "sail", "config.yaml")
}
14 changes: 7 additions & 7 deletions internal/models/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import (
"io/fs"
"os"
"path"
"path/filepath"
"slices"
)

Expand All @@ -19,39 +19,39 @@ func (m mockOS) ReadDir(path string) ([]fs.DirEntry, error) {
}

func (m mockOS) RemoveAll(fPath string) error {
dir := path.Dir(fPath)
dir := filepath.Dir(fPath)
files, ok := m.files[dir]
if !ok {
return os.ErrNotExist
}

if !slices.ContainsFunc(files, func(f fs.DirEntry) bool { return f.Name() == path.Base(fPath) }) {
if !slices.ContainsFunc(files, func(f fs.DirEntry) bool { return f.Name() == filepath.Base(fPath) }) {
return os.ErrNotExist
}

m.files[dir] = slices.DeleteFunc(files, func(f fs.DirEntry) bool {
return f.Name() == path.Base(fPath)
return f.Name() == filepath.Base(fPath)
})

return nil
}

func (m mockOS) rename(oldPath, newPath string) error {
dir := path.Dir(oldPath)
dir := filepath.Dir(oldPath)
files, ok := m.files[dir]
if !ok {
return os.ErrNotExist
}

i := slices.IndexFunc(files, func(f fs.DirEntry) bool { return f.Name() == path.Base(oldPath) })
i := slices.IndexFunc(files, func(f fs.DirEntry) bool { return f.Name() == filepath.Base(oldPath) })
if i == -1 {
return os.ErrNotExist
}

oldFile := files[i]
m.files[dir] = slices.Delete(files, i, i+1)

dir = path.Dir(newPath)
dir = filepath.Dir(newPath)
if _, ok := m.files[dir]; !ok {
m.files[dir] = []fs.DirEntry{}
}
Expand Down
19 changes: 9 additions & 10 deletions internal/models/main_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package models
import (
"io/fs"
"os"
"path"
"path/filepath"
"slices"
"strings"
Expand Down Expand Up @@ -116,7 +115,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

return m, nil
case m.cfg.Settings.Keymap.NavOut:
return m, m.loadDir(path.Dir(m.cwd))
return m, m.loadDir(filepath.Dir(m.cwd))

case m.cfg.Settings.Keymap.NavIn:
if len(m.files) <= 0 {
Expand All @@ -125,14 +124,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

currFile := m.currFile()
if currFile.IsDir() {
return m, m.loadDir(path.Join(m.cwd, currFile.Name()))
return m, m.loadDir(filepath.Join(m.cwd, currFile.Name()))
}

if currFile.Type() != fs.ModeSymlink {
return m, nil
}

path, err := os.Readlink(path.Join(m.cwd, currFile.Name()))
path, err := os.Readlink(filepath.Join(m.cwd, currFile.Name()))
if err != nil {
m.lastError = err
return m, nil
Expand Down Expand Up @@ -180,7 +179,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

return m, tea.Sequence(
func() tea.Msg {
return m.do(func(f string) error { return osi.rename(f, path.Join(m.cwd, path.Base(f))) })
return m.do(func(f string) error { return osi.rename(f, filepath.Join(m.cwd, filepath.Base(f))) })
},
m.loadDir(m.cwd),
)
Expand All @@ -192,7 +191,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.Lock()
defer m.Unlock()

fName := path.Join(m.cwd, m.currFile().Name())
fName := filepath.Join(m.cwd, m.currFile().Name())
if _, ok := m.selectedFiles[fName]; ok {
delete(m.selectedFiles, fName)
log.Debug().Msgf("Deselected %s", fName)
Expand Down Expand Up @@ -231,10 +230,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.files = msg.files

fName, ok := m.cachedDirSelections[newDir]
if !ok && path.Join(newDir, path.Base(oldDir)) == oldDir {
if !ok && filepath.Join(newDir, filepath.Base(oldDir)) == oldDir {
// in case of a navigation to the parent directory
// select the parent directory in the parent directory
fName = path.Base(oldDir)
fName = filepath.Base(oldDir)
}

if fName != "" {
Expand Down Expand Up @@ -431,7 +430,7 @@ func (m Model) goUp(wrap bool) Model {

func (m Model) isSelected(name string) bool {
m.RLock()
_, ok := m.selectedFiles[path.Join(m.cwd, name)]
_, ok := m.selectedFiles[filepath.Join(m.cwd, name)]
m.RUnlock()
return ok
}
Expand All @@ -443,7 +442,7 @@ func (m Model) do(do func(string) error) error {
m.Lock()
defer m.Unlock()
if len(m.selectedFiles) == 0 {
return do(path.Join(m.cwd, m.currFile().Name()))
return do(filepath.Join(m.cwd, m.currFile().Name()))
}

for f := range m.selectedFiles {
Expand Down
5 changes: 2 additions & 3 deletions internal/util/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -16,7 +15,7 @@ import (

// SetupLogger sets up the global logger
func SetupLogger(buffered bool) (flush func() (string, error)) {
fPath := path.Join(os.TempDir(), "sail.log")
fPath := filepath.Join(os.TempDir(), "sail.log")
f, err := os.Create(fPath)
if err != nil {
panic(err)
Expand All @@ -42,7 +41,7 @@ func SetupLogger(buffered bool) (flush func() (string, error)) {
Logger()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

switch strings.ToLower("debug") {
switch strings.ToLower("trace") {
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "debug":
Expand Down

0 comments on commit ce6f8d6

Please sign in to comment.