Skip to content

Commit

Permalink
chore: Tidy up mackup code
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 11, 2023
1 parent 31780bf commit 1f07dee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ linters-settings:
- ^compress/gzip\.
- ^fmt\.Print.*$
- ^ioutil\..*$
- ^os\.(DirEntry|FileInfo|FileMode|Is.*|Mode.*)$
- ^os\.(DirEntry|ErrExist|ErrNotExist|FileInfo|FileMode|Is.*|Mode.*)$
gci:
sections:
- standard
Expand Down
30 changes: 10 additions & 20 deletions internal/cmd/mackupcmd_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"io/fs"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -77,7 +76,7 @@ func (c *Config) runMackupAddCmd(
for _, arg := range args {
configRelPath := chezmoi.NewRelPath(arg + ".cfg")
data, err := c.baseSystem.ReadFile(mackupDirAbsPath.Join(configRelPath))
if errors.Is(err, os.ErrNotExist) {
if errors.Is(err, fs.ErrNotExist) {
data, err = c.baseSystem.ReadFile(mackupApplicationsDir.Join(configRelPath))
}
if err != nil {
Expand Down Expand Up @@ -122,17 +121,17 @@ func (c *Config) runMackupAddCmd(
}

func (c *Config) mackupApplicationsDir() (chezmoi.AbsPath, error) {
mackupBinaryPath, err := exec.LookPath("mackup")
mackupBinaryPath, err := chezmoi.LookPath("mackup")
if err != nil {
return chezmoi.EmptyAbsPath, fmt.Errorf("mackup binary not found in PATH (%w)", err)
return chezmoi.EmptyAbsPath, err
}
mackupBinaryPathResolved, err := filepath.EvalSymlinks(mackupBinaryPath)
if err != nil {
return chezmoi.EmptyAbsPath, err
}
mackupBinaryPathAbs := chezmoi.NewAbsPath(mackupBinaryPathResolved)
mackupBinaryAbsPath := chezmoi.NewAbsPath(mackupBinaryPathResolved)

libDirAbsPath := mackupBinaryPathAbs.Dir().Dir().JoinString("lib")
libDirAbsPath := mackupBinaryAbsPath.Dir().Dir().JoinString("lib")
dirEntries, err := c.baseSystem.ReadDir(libDirAbsPath)
if err != nil {
return chezmoi.EmptyAbsPath, err
Expand All @@ -142,22 +141,13 @@ func (c *Config) mackupApplicationsDir() (chezmoi.AbsPath, error) {
if !dirEntry.IsDir() || !strings.HasPrefix(dirEntry.Name(), "python") {
continue
}

pythonDirRelPath := chezmoi.NewRelPath(dirEntry.Name())
mackupAppsDir := libDirAbsPath.Join(pythonDirRelPath).
JoinString("site-packages", "mackup", "applications")

if _, err := os.Stat(mackupAppsDir.String()); errors.Is(err, os.ErrNotExist) {
continue
mackupApplicationsDirAbsPath := libDirAbsPath.JoinString(dirEntry.Name(), "site-packages", "mackup", "applications")
if fileInfo, err := c.baseSystem.Stat(mackupApplicationsDirAbsPath); err == nil && fileInfo.IsDir() {
return mackupApplicationsDirAbsPath, nil
}

return mackupAppsDir, nil
}

return chezmoi.EmptyAbsPath, fmt.Errorf(
"mackup python directory cannot be found: %s",
libDirAbsPath,
)
return chezmoi.EmptyAbsPath, fmt.Errorf("%s: mackup application directory not found", libDirAbsPath)
}

func parseMackupApplication(data []byte) (mackupApplicationConfig, error) {
Expand Down

0 comments on commit 1f07dee

Please sign in to comment.