Skip to content
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
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ plugin list when no URL has been recorded.`,
return
}

manifest.Add(pluginConfig.Name, version, pluginInstaller.PluginType(), pluginInstaller.Source(), "")
manifest.Add(pluginConfig.Name, version, pluginInstaller.PluginType(), pluginInstaller.Source(), pluginConfig.BinaryPath)
if pluginConfig.Description != "" {
manifest.SetDescription(pluginConfig.Name, pluginConfig.Description)
}
Expand Down
80 changes: 51 additions & 29 deletions internal/installer/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,14 @@ func (installer *ScriptInstaller) Install(name string) (string, error) {
return "", fmt.Errorf("install script failed: %w", err)
}

binaryPath := installer.BinaryPath
if binaryPath != "" {
if strings.HasPrefix(binaryPath, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
binaryPath = filepath.Join(home, binaryPath[2:])
}
if _, err := os.Stat(binaryPath); err != nil {
return "", fmt.Errorf("binary not found at %s after install: %w", binaryPath, err)
}
} else {
// Auto-detect binary on PATH by name
found, err := installer.lookPath(name)
if err != nil {
return "", fmt.Errorf("binary %q not found on PATH after install; use --binary-path to specify its location", name)
}
binaryPath = found
}

installDir, err := installer.pluginBinDir()
binaryPath, err := installer.resolveBinary(name)
if err != nil {
return "", err
}
if err := os.MkdirAll(installDir, 0755); err != nil {
if err := installer.link(name, binaryPath); err != nil {
return "", err
}

linkPath := filepath.Join(installDir, plugin.BinPrefix+name)
os.Remove(linkPath)
if err := os.Symlink(binaryPath, linkPath); err != nil {
return "", fmt.Errorf("failed to create symlink: %w", err)
}

version := installer.DetectVersion(name)
return version, nil
}
Expand All @@ -83,6 +56,15 @@ func (installer *ScriptInstaller) Update(name string, current plugin.ManifestEnt
return nil, fmt.Errorf("failed to update plugin from script source %q: %w", installer.ScriptURL, err)
}

// Re-point the managed link at the binary the script just installed. Without this, a link that
// drifted (e.g. replaced by a plugin's self-upgrade) keeps reporting the old version forever.
// If the binary can't be located, keep the existing link as before.
if binaryPath, err := installer.resolveBinary(name); err == nil {
if err := installer.link(name, binaryPath); err != nil {
return nil, err
}
}

version := installer.DetectVersion(name)

installDir, err := installer.pluginBinDir()
Expand Down Expand Up @@ -110,6 +92,46 @@ func (installer *ScriptInstaller) Uninstall(name string, entry plugin.ManifestEn
return removePluginBinaryAndTarget(name)
}

// resolveBinary returns the binary the install script produced: BinaryPath if set, else name on PATH.
func (installer *ScriptInstaller) resolveBinary(name string) (string, error) {
binaryPath := installer.BinaryPath
if binaryPath == "" {
found, err := installer.lookPath(name)
if err != nil {
return "", fmt.Errorf("binary %q not found on PATH after install; use --binary-path to specify its location", name)
}
return found, nil
}
if strings.HasPrefix(binaryPath, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
binaryPath = filepath.Join(home, binaryPath[2:])
}
if _, err := os.Stat(binaryPath); err != nil {
return "", fmt.Errorf("binary not found at %s after install: %w", binaryPath, err)
}
return binaryPath, nil
}

// link (re)creates the managed clime-<name> symlink pointing at binaryPath.
func (installer *ScriptInstaller) link(name, binaryPath string) error {
installDir, err := installer.pluginBinDir()
if err != nil {
return err
}
if err := os.MkdirAll(installDir, 0755); err != nil {
return err
}
linkPath := filepath.Join(installDir, plugin.BinPrefix+name)
os.Remove(linkPath)
if err := os.Symlink(binaryPath, linkPath); err != nil {
return fmt.Errorf("failed to create symlink: %w", err)
}
return nil
}

func (installer *ScriptInstaller) DetectVersion(name string) string {
binPath, ok := installer.findPlugin(name)
if !ok {
Expand Down
38 changes: 38 additions & 0 deletions internal/installer/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func TestScriptInstallerUpdateFallsBackToLatest(t *testing.T) {
runVersion: func(binPath string) (string, error) {
return "", nil
},
lookPath: func(name string) (string, error) {
return "", fmt.Errorf("not found")
},
}

entry := plugin.ManifestEntry{
Expand All @@ -107,6 +110,41 @@ func TestScriptInstallerUpdateFallsBackToLatest(t *testing.T) {
require.Equal(t, plugin.VersionLatest, result.LatestVersion)
}

// A managed link that no longer points at binary_path (e.g. a plugin's self-upgrade renamed a new
// binary over it) must be re-pointed on update, or the stale copy's version is reported as current.
func TestScriptInstallerUpdateRelinksDriftedSymlink(t *testing.T) {
t.Setenv("HOME", t.TempDir())
pluginsDir, err := plugin.PluginBinDir()
require.NoError(t, err)
require.NoError(t, os.MkdirAll(pluginsDir, 0o755))

writeCLI := func(path, version string) {
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
require.NoError(t, os.WriteFile(path, []byte("#!/bin/sh\necho "+version+"\n"), 0o755))
}
stale := filepath.Join(os.Getenv("HOME"), "old", "tool")
writeCLI(stale, "v1.0.0")
linkPath := filepath.Join(pluginsDir, plugin.BinPrefix+"tool")
require.NoError(t, os.Symlink(stale, linkPath))

installed := filepath.Join(os.Getenv("HOME"), ".local", "bin", "tool")
s := NewScriptInstaller("https://example.com/install.sh", "~/.local/bin/tool")
s.runScript = func(string) error { writeCLI(installed, "v2.0.0"); return nil }

result, err := s.Update("tool", plugin.ManifestEntry{
Name: "tool",
Version: "1.0.0",
Type: plugin.SourceTypeScript,
Source: "https://example.com/install.sh",
})
require.NoError(t, err)
require.True(t, result.Updated)
require.Equal(t, "2.0.0", result.LatestVersion)
target, err := os.Readlink(linkPath)
require.NoError(t, err)
require.Equal(t, installed, target)
}

func TestScriptInstallerInstallAutoDetectBinary(t *testing.T) {
t.Parallel()

Expand Down
Loading