Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make detection of mackup config dir more robust #3128

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions pkg/cmd/mackupcmd_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import (
)

var (
mackupCommentRx = regexp.MustCompile(`\A#.*\z`)
mackupKeyValueRx = regexp.MustCompile(`\A(\w+)\s*=\s*(.*)\z`)
mackupSectionRx = regexp.MustCompile(`\A\[(.*)\]\z`)
mackupVersionRx = regexp.MustCompile(`\AMackup\s+(\d+\.\d+\.\d+)\s*\z`)
pythonMajorMinorVersionRx = regexp.MustCompile(`\APython\s+(\d+\.\d+)\.\d+\s*\z`)
mackupCommentRx = regexp.MustCompile(`\A#.*\z`)
mackupKeyValueRx = regexp.MustCompile(`\A(\w+)\s*=\s*(.*)\z`)
mackupSectionRx = regexp.MustCompile(`\A\[(.*)\]\z`)
mackupVersionRx = regexp.MustCompile(`\AMackup\s+(\d+\.\d+\.\d+)\s*\z`)
)

type mackupApplicationApplicationConfig struct {
Expand Down Expand Up @@ -139,31 +138,28 @@ func (c *Config) mackupApplicationsDir() (chezmoi.AbsPath, error) {
}
mackupVersion := string(mackupVersionMatch[1])

pythonVersionCmd := exec.Command("python3", "--version")
pythonVersionData, err := pythonVersionCmd.Output()
libDirAbsPath := brewPrefix.JoinString("Cellar", "mackup", mackupVersion, "libexec", "lib")
dirEntries, err := c.baseSystem.ReadDir(libDirAbsPath)
if err != nil {
return chezmoi.EmptyAbsPath, err
}
pythonMajorMinorVersionMatch := pythonMajorMinorVersionRx.FindSubmatch(pythonVersionData)
if pythonMajorMinorVersionMatch == nil {
var pythonDirRelPath chezmoi.RelPath
for _, dirEntry := range dirEntries {
if dirEntry.IsDir() && strings.HasPrefix(dirEntry.Name(), "python") {
pythonDirRelPath = chezmoi.NewRelPath(dirEntry.Name())
break
}
}
if pythonDirRelPath.Empty() {
return chezmoi.EmptyAbsPath, fmt.Errorf(
"%q: cannot determine Python version",
pythonVersionData,
"%s: could not find python directory",
libDirAbsPath,
)
}
pythonMajorMinorVersion := string(pythonMajorMinorVersionMatch[1])

return brewPrefix.JoinString(
"Cellar",
"mackup",
mackupVersion,
"libexec",
"lib",
"python"+pythonMajorMinorVersion,
"site-packages",
"mackup",
"applications",
), nil

return libDirAbsPath.Join(pythonDirRelPath).
JoinString("site-packages", "mackup", "applications"),
nil
}

func parseMackupApplication(data []byte) (mackupApplicationConfig, error) {
Expand Down
14 changes: 1 addition & 13 deletions pkg/cmd/testdata/scripts/mackup_darwin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

chmod 755 bin/brew
chmod 755 bin/mackup
chmod 755 bin/python3

# test that chezmoi mackup add adds normal dotfiles
exec chezmoi mackup add curl
cmp $CHEZMOISOURCEDIR/dot_curlrc golden/dot_curlrc

# test that chezmoi
# test that chezmoi mackup add adds XDG configuration files
exec chezmoi mackup add vscode
cmp $CHEZMOISOURCEDIR/dot_config/Code/User/settings.json golden/settings.json

Expand All @@ -34,17 +33,6 @@ case "$*" in
echo "Usage:"
;;
esac
-- bin/python3 --
#!/bin/sh

case "$*" in
"--version")
echo "Python 3.9.0"
;;
*)
echo "Usage:"
;;
esac
-- golden/dot_curlrc --
# contents of .curlrc
-- golden/settings.json --
Expand Down