mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: migrate PMG to use PATH shims for package manager wrapper (#246)
* feat: add FilterPMGFromPath utility for PATH shim recursion prevention * feat: add FilterPMGFromEnv to filter PATH from env slices * feat: filter ~/.pmg/bin from PATH in proxy subprocess env * feat: add PathExport method to Shell interface for shim PATH integration * feat: add ShimManager for PATH shim install/remove lifecycle * feat: wire ShimManager into setup commands with --use-aliases fallback * refactor: add DefaultShimConfig helper to reduce setup boilerplate * fix: resolve real binary path to prevent shim double-invocation exec.CommandContext resolves the binary using the current process PATH, which still contains ~/.pmg/bin. This caused pmg to launch the shim instead of the real package manager, resulting in a second pmg instance with its own proxy — producing duplicate error messages and wasted work. ResolveRealBinary searches a filtered PATH (without ~/.pmg/bin) to find the real package manager binary before execution. * fix: resolve real binary in runner.Execute and expand path resolution tests Ensure guard mode and proxy skip paths also resolve through ResolveRealBinary to prevent infinite shim recursion. Add table-driven tests covering error cases, multi-binary PATH, and PATH restoration. * fix: handle error return values from os.Setenv and file Close calls Address errcheck lint failures: check os.Setenv returns in ResolveRealBinary, and check f.Close/tempFile.Close in ShimManager. * feat: auto-migrate shell aliases to PATH shims on setup install When running `pmg setup install`, detect existing shell aliases and automatically remove them before installing shims. Existing users get a seamless migration with no extra flags or commands needed. * fix: update E2E test to verify shim installation instead of alias RC file Replace the .pmg.rc file check with assertions that ~/.pmg/bin/ exists and contains executable shim scripts for npm and pip. * feat: add FilterPMGFromPath utility for PATH shim recursion prevention * feat: add FilterPMGFromEnv to filter PATH from env slices * feat: filter ~/.pmg/bin from PATH in proxy subprocess env * feat: add PathExport method to Shell interface for shim PATH integration * feat: add ShimManager for PATH shim install/remove lifecycle * feat: wire ShimManager into setup commands with --use-aliases fallback * refactor: add DefaultShimConfig helper to reduce setup boilerplate * fix: resolve real binary path to prevent shim double-invocation exec.CommandContext resolves the binary using the current process PATH, which still contains ~/.pmg/bin. This caused pmg to launch the shim instead of the real package manager, resulting in a second pmg instance with its own proxy — producing duplicate error messages and wasted work. ResolveRealBinary searches a filtered PATH (without ~/.pmg/bin) to find the real package manager binary before execution. * fix: resolve real binary in runner.Execute and expand path resolution tests Ensure guard mode and proxy skip paths also resolve through ResolveRealBinary to prevent infinite shim recursion. Add table-driven tests covering error cases, multi-binary PATH, and PATH restoration. * fix: handle error return values from os.Setenv and file Close calls Address errcheck lint failures: check os.Setenv returns in ResolveRealBinary, and check f.Close/tempFile.Close in ShimManager. * feat: auto-migrate shell aliases to PATH shims on setup install When running `pmg setup install`, detect existing shell aliases and automatically remove them before installing shims. Existing users get a seamless migration with no extra flags or commands needed. * fix: update E2E test to verify shim installation instead of alias RC file Replace the .pmg.rc file check with assertions that ~/.pmg/bin/ exists and contains executable shim scripts for npm and pip. * feat: install both aliases and shims for full coverage Aliases win in interactive shells (including venvs), shims catch non-interactive contexts (IDEs, CI, subprocesses). Remove --use-aliases flag and migration logic since both are always installed together. Update E2E to verify all shim scripts and alias RC file. * feat: address review feedback for shim implementation - Install both aliases and shims together for full coverage - Move homeDir resolution into NewDefaultShimManager (internal concern) - Add mutex to ResolveRealBinary to guard against concurrent PATH mutation - Use filepath.SplitList for platform-correct PATH splitting - Add ResolveRealBinary to runner.Execute and proxy flow to prevent shim recursion in all execution paths - Remove print side-effects from ShimManager.Remove - Update E2E to verify all shim scripts and alias RC file - Expand ResolveRealBinary tests with table-driven cases * fix: restore errcheck handling and add concurrency test for ResolveRealBinary - Restore proper defer with log.Warnf for PATH restoration in ResolveRealBinary - Restore errcheck handling for f.Close() and tempFile.Close() in ShimManager - Add explanatory comment for ResolveRealBinary call in proxy_flow - Add TestResolveRealBinaryConcurrent to verify mutex guards concurrent access * feat: skip shell integration on Windows with informative warning On Windows, pmg setup install now writes only the config file and prints a warning that shell aliases and PATH shims require WSL. * fix: PMG use pre-resolved binary path (#253) --------- Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
This commit is contained in:
co-authored by
Abhisek Datta
parent
d993d57e3d
commit
6546116e28
+64
-25
@@ -3,9 +3,11 @@ package setup
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/internal/alias"
|
||||
"github.com/safedep/pmg/internal/shim"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/pmg/internal/version"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -18,8 +20,8 @@ var (
|
||||
func NewSetupCommand() *cobra.Command {
|
||||
setupCmd := &cobra.Command{
|
||||
Use: "setup",
|
||||
Short: "Manage PMG shell aliases and integration",
|
||||
Long: "Setup and manage PMG config, shell aliases that allow you to use package manager commands with security guardrails.",
|
||||
Short: "Manage PMG shell integration (aliases and shims)",
|
||||
Long: "Setup and manage PMG config, shell aliases and PATH shims that allow you to use package manager commands with security guardrails.",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return cmd.Help()
|
||||
},
|
||||
@@ -36,42 +38,60 @@ func NewSetupCommand() *cobra.Command {
|
||||
func NewInstallCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "Setup PMG config and aliases for package managers (npm, pnpm, pip, and more)",
|
||||
Short: "Setup PMG config, aliases, and shims for package managers (npm, pnpm, pip, and more)",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
||||
|
||||
cfg := alias.DefaultConfig()
|
||||
rcFileManager, err := alias.NewDefaultRcFileManager(cfg.RcFileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create alias manager: %w", err)
|
||||
}
|
||||
|
||||
aliasManager := alias.New(cfg, rcFileManager)
|
||||
err = aliasManager.Install()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to install aliases: %w", err)
|
||||
}
|
||||
|
||||
if err := config.WriteTemplateConfig(); err != nil {
|
||||
return fmt.Errorf("failed to write template config: %w", err)
|
||||
}
|
||||
|
||||
ui.PrintSetupInstallCmdInfo(aliasManager.GetRcPath(), config.Get().ConfigDir())
|
||||
return nil
|
||||
return install()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func install() error {
|
||||
if err := config.WriteTemplateConfig(); err != nil {
|
||||
return fmt.Errorf("failed to write template config: %w", err)
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
fmt.Printf("%s %s\n", ui.Colors.Green("✓"), "PMG config written successfully")
|
||||
fmt.Printf(" %s\n", ui.Colors.Dim(fmt.Sprintf("Config: %s", config.Get().ConfigDir())))
|
||||
fmt.Printf("\n%s Shell aliases and PATH shims are not supported on Windows. Use WSL for full shell integration.\n",
|
||||
ui.Colors.Yellow("⚠"))
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := alias.DefaultConfig()
|
||||
rcFileManager, err := alias.NewDefaultRcFileManager(cfg.RcFileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create alias manager: %w", err)
|
||||
}
|
||||
|
||||
aliasManager := alias.New(cfg, rcFileManager)
|
||||
if err := aliasManager.Install(); err != nil {
|
||||
return fmt.Errorf("failed to install aliases: %w", err)
|
||||
}
|
||||
|
||||
shimMgr, err := shim.NewDefaultShimManager()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create shim manager: %w", err)
|
||||
}
|
||||
|
||||
if err := shimMgr.Install(); err != nil {
|
||||
return fmt.Errorf("failed to install shims: %w", err)
|
||||
}
|
||||
|
||||
ui.PrintSetupInstallCmdInfo(aliasManager.GetRcPath(), shimMgr.GetBinDir(), config.Get().ConfigDir())
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewRemoveCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Removes pmg aliases from the user's shell config file.",
|
||||
Short: "Removes pmg aliases and shims from the user's shell config.",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
||||
|
||||
// We remove the config file only if explicitly asked to do so.
|
||||
if setupRemoveConfigFile {
|
||||
config := config.Get()
|
||||
if err := os.Remove(config.ConfigFilePath()); err != nil && !os.IsNotExist(err) {
|
||||
@@ -79,6 +99,11 @@ func NewRemoveCommand() *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
fmt.Printf("%s %s\n", ui.Colors.Green("✓"), "PMG config removed. No aliases or shims to clean up on Windows.")
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := alias.DefaultConfig()
|
||||
rcFileManager, err := alias.NewDefaultRcFileManager(cfg.RcFileName)
|
||||
if err != nil {
|
||||
@@ -86,7 +111,21 @@ func NewRemoveCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
aliasManager := alias.New(cfg, rcFileManager)
|
||||
return aliasManager.Remove()
|
||||
if err := aliasManager.Remove(); err != nil {
|
||||
return fmt.Errorf("failed to remove aliases: %w", err)
|
||||
}
|
||||
|
||||
shimMgr, err := shim.NewDefaultShimManager()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create shim manager: %w", err)
|
||||
}
|
||||
|
||||
if err := shimMgr.Remove(); err != nil {
|
||||
return fmt.Errorf("failed to remove shims: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("%s %s\n", ui.Colors.Green("✓"), "PMG aliases and shims removed. Restart your terminal for changes to take effect")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user