asdf/internal/installs/installs.go
Trevor Brown be52d8f39c feat(golang-rewrite): create installs and installtest packages to avoid circular dependency
* Correct `go test` command in GitHub test workflow
* Update execute tests to work on Github Actions
* Check in `shims/testdata` directory
* Create `installtest` helper package
* Create `installs` package
2024-12-18 11:32:02 -05:00

65 lines
1.8 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"
"asdf/internal/config"
"asdf/internal/plugins"
)
const (
dataDirInstalls = "installs"
dataDirDownloads = "downloads"
)
// 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 := pluginInstallPath(conf, plugin)
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 string) string {
return filepath.Join(pluginInstallPath(conf, plugin), version)
}
// DownloadPath returns the download path for a particular plugin and version
func DownloadPath(conf config.Config, plugin plugins.Plugin, version string) string {
return filepath.Join(conf.DataDir, dataDirDownloads, plugin.Name, version)
}
// IsInstalled checks if a specific version of a tool is installed
func IsInstalled(conf config.Config, plugin plugins.Plugin, version string) bool {
installDir := InstallPath(conf, plugin, version)
// Check if version already installed
_, err := os.Stat(installDir)
return !os.IsNotExist(err)
}
func pluginInstallPath(conf config.Config, plugin plugins.Plugin) string {
return filepath.Join(conf.DataDir, dataDirInstalls, plugin.Name)
}