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(inventory): use to slash for windows #76

Merged
merged 1 commit into from
Nov 18, 2024
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
5 changes: 5 additions & 0 deletions pkg/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math"
"os"
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
Expand Down Expand Up @@ -307,6 +308,8 @@ func (a *Asset) determineInstallable() {
}
}

var versionReplace = regexp.MustCompile(`\d+\.\d+`)

// Install installs the asset
// TODO(ek): simplify this function
func (a *Asset) Install(id, binDir, optDir string) error {
Expand Down Expand Up @@ -343,6 +346,8 @@ func (a *Asset) Install(id, binDir, optDir string) error {
dstFilename = strings.ReplaceAll(dstFilename, fmt.Sprintf("v%s", a.Version), "")
dstFilename = strings.ReplaceAll(dstFilename, a.Version, "")

dstFilename = versionReplace.ReplaceAllString(dstFilename, "")

if a.OS == osconfig.Windows || strings.HasSuffix(dstFilename, ".exe") {
dstFilename = strings.TrimSuffix(dstFilename, ".exe")
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/asset/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,17 @@ func TestAssetInstall(t *testing.T) {
"dist",
},
},
{
name: "test-1.12.3-darwin-10.12-amd64",
os: "darwin",
arch: "amd64",
version: "1.12.3",
fileType: Binary,
downloadFile: createFile(t, []byte{0xFE, 0xED, 0xFA, 0xCE}),
expectedFiles: []string{
"test",
},
},
}

for _, c := range cases {
Expand Down
14 changes: 9 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,28 @@ type Config struct {
Providers map[string]*Provider `yaml:"providers" toml:"providers"`
}

func (c *Config) GetPath() string {
return filepath.ToSlash(c.Path)
}

// GetCachePath - get the cache path
func (c *Config) GetCachePath() string {
return filepath.Join(c.CachePath, common.NAME)
return filepath.ToSlash(filepath.Join(c.CachePath, common.NAME))
}

// GetMetadataPath - get the metadata path
func (c *Config) GetMetadataPath() string {
return filepath.Join(c.CachePath, common.NAME, "metadata")
return filepath.ToSlash(filepath.Join(c.CachePath, common.NAME, "metadata"))
}

// GetDownloadsPath - get the downloads path
func (c *Config) GetDownloadsPath() string {
return filepath.Join(c.CachePath, common.NAME, "downloads")
return filepath.ToSlash(filepath.Join(c.CachePath, common.NAME, "downloads"))
}

// GetOptPath - get the opt path
func (c *Config) GetOptPath() string {
return filepath.Join(c.Path, "opt")
return filepath.ToSlash(filepath.Join(c.GetPath(), "opt"))
}

// GetAlias - get an alias by name
Expand All @@ -82,7 +86,7 @@ func (c *Config) GetAlias(name string) *Alias {

// MkdirAll - create all the directories
func (c *Config) MkdirAll() error {
paths := []string{c.BinPath, c.GetOptPath(), c.CachePath, c.GetMetadataPath(), c.GetDownloadsPath()}
paths := []string{c.BinPath, c.GetOptPath(), c.GetCachePath(), c.GetMetadataPath(), c.GetDownloadsPath()}

for _, path := range paths {
err := os.MkdirAll(path, 0755)
Expand Down
18 changes: 14 additions & 4 deletions pkg/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (i *Inventory) SetConfig(cfg *config.Config) {
}

func (i *Inventory) AddVersion(path, target string) error {
path = filepath.ToSlash(path)
target = filepath.ToSlash(target)

binName := filepath.Base(path)
version := "latest"
latest := true
Expand All @@ -54,9 +57,13 @@ func (i *Inventory) AddVersion(path, target string) error {
i.latestPaths = make(map[string]string)
}

source := strings.TrimPrefix(strings.TrimPrefix(target, i.config.GetOptPath()), "/")
baseSourceParts := strings.SplitAfterN(source, "/", 4)
baseSource := strings.TrimSuffix(strings.Join(baseSourceParts[:3], ""), "/")
// TODO: this doesn't work on windows :facepalm:

relativeBin, _ := filepath.Rel(i.config.GetOptPath(), target)
relativeBin = filepath.ToSlash(relativeBin)

relativeParts := strings.Split(relativeBin, "/") // note: using / because we are standardizing via ToSlash
baseSource := filepath.ToSlash(filepath.Join(relativeParts[:3]...))

if i.Bins[baseSource] == nil {
src := strings.TrimPrefix(strings.TrimPrefix(target, i.config.GetOptPath()), "/")
Expand Down Expand Up @@ -181,11 +188,14 @@ func New(fileSystem fs.FS, basePath, binPath string, cfg *config.Config) *Invent
return nil
}

target, err := os.Readlink(filepath.Join(basePath, path))
target, err := os.Readlink(filepath.ToSlash(filepath.Join(basePath, path)))
if err != nil {
logrus.WithError(err).Warn("failed to read symlink")
}

path = filepath.ToSlash(path)
target = filepath.ToSlash(target)

logrus.WithFields(logrus.Fields{
"path": path,
"target": target,
Expand Down
7 changes: 6 additions & 1 deletion pkg/inventory/inventory_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package inventory_test

import (
Expand Down Expand Up @@ -221,7 +224,9 @@ func TestInventory_AddVersion(t *testing.T) {
_ = inv.AddVersion(bin, target)
}

assert.EqualValues(t, tc.expected, inv.Bins)
for bin, expected := range tc.expected {
assert.ElementsMatch(t, expected.Versions, inv.Bins[bin].Versions)
}
})
}
}
Expand Down
Loading