mirror of
https://github.com/asdf-vm/asdf.git
synced 2024-12-19 18:05:02 -07:00
5d5d04fbb7
* Replace direct `fmt.Println()` usage in a test with `t.Log()` * Rename `cmd` to `cli` * Move asdf command from module root * Fix some linter warnings, thus enabling some tests that were being skipped * Fix `Makefile` * Rename module to `github.com/asdf-vm/asdf` * Fix `TestGetAllToolsAndVersionsInContent/returns_empty_list_with_found_true_and_no_error_when_empty_content` * Rewrite `Unique()` to be a bit more straightforwards * Get workflow checks passing again toolversions.Unique is ever so slightly faster, technically. ``` goos: linux goarch: amd64 pkg: github.com/asdf-vm/asdf/internal/toolversions cpu: AMD Ryzen 9 3900X 12-Core Processor │ /tmp/old.txt │ /tmp/new.txt │ │ sec/op │ sec/op vs base │ Unique-24 346.5n ± 1% 342.4n ± 1% -1.17% (p=0.027 n=10) │ /tmp/old.txt │ /tmp/new.txt │ │ B/op │ B/op vs base │ Unique-24 160.0 ± 0% 160.0 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal │ /tmp/old.txt │ /tmp/new.txt │ │ allocs/op │ allocs/op vs base │ Unique-24 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal ```
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
// Package installs contains tool installation logic. It is "dumb" when it comes
|
|
// to versions and treats versions as opaque strings. It cannot depend on the
|
|
// versions package because the versions package relies on this page.
|
|
package installs
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/asdf-vm/asdf/internal/config"
|
|
"github.com/asdf-vm/asdf/internal/data"
|
|
"github.com/asdf-vm/asdf/internal/plugins"
|
|
"github.com/asdf-vm/asdf/internal/toolversions"
|
|
)
|
|
|
|
// Installed returns a slice of all installed versions for a given plugin
|
|
func Installed(conf config.Config, plugin plugins.Plugin) (versions []string, err error) {
|
|
installDirectory := data.InstallDirectory(conf.DataDir, plugin.Name)
|
|
files, err := os.ReadDir(installDirectory)
|
|
if err != nil {
|
|
if _, ok := err.(*fs.PathError); ok {
|
|
return versions, nil
|
|
}
|
|
|
|
return versions, err
|
|
}
|
|
|
|
for _, file := range files {
|
|
if !file.IsDir() {
|
|
continue
|
|
}
|
|
|
|
versions = append(versions, file.Name())
|
|
}
|
|
|
|
return versions, err
|
|
}
|
|
|
|
// InstallPath returns the path to a tool installation
|
|
func InstallPath(conf config.Config, plugin plugins.Plugin, version toolversions.Version) string {
|
|
if version.Type == "path" {
|
|
return version.Value
|
|
}
|
|
|
|
return filepath.Join(data.InstallDirectory(conf.DataDir, plugin.Name), toolversions.FormatForFS(version))
|
|
}
|
|
|
|
// DownloadPath returns the download path for a particular plugin and version
|
|
func DownloadPath(conf config.Config, plugin plugins.Plugin, version toolversions.Version) string {
|
|
if version.Type == "path" {
|
|
return ""
|
|
}
|
|
|
|
return filepath.Join(data.DownloadDirectory(conf.DataDir, plugin.Name), toolversions.FormatForFS(version))
|
|
}
|
|
|
|
// IsInstalled checks if a specific version of a tool is installed
|
|
func IsInstalled(conf config.Config, plugin plugins.Plugin, version toolversions.Version) bool {
|
|
installDir := InstallPath(conf, plugin, version)
|
|
|
|
// Check if version already installed
|
|
_, err := os.Stat(installDir)
|
|
return !os.IsNotExist(err)
|
|
}
|