2025-06-24 12:46:21 +05:30
|
|
|
|
package setup
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-21 23:01:30 +05:30
|
|
|
|
"fmt"
|
2026-05-12 22:27:05 +05:30
|
|
|
|
"runtime"
|
2025-08-21 23:01:30 +05:30
|
|
|
|
|
2026-01-01 12:33:52 +05:30
|
|
|
|
"github.com/safedep/pmg/config"
|
2025-06-24 12:46:21 +05:30
|
|
|
|
"github.com/safedep/pmg/internal/alias"
|
2026-05-12 22:27:05 +05:30
|
|
|
|
"github.com/safedep/pmg/internal/shim"
|
2025-08-21 23:01:30 +05:30
|
|
|
|
"github.com/safedep/pmg/internal/ui"
|
|
|
|
|
|
"github.com/safedep/pmg/internal/version"
|
2025-06-24 12:46:21 +05:30
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-05-21 14:32:30 +05:30
|
|
|
|
var setupRemoveConfigFile = false
|
2026-01-01 12:33:52 +05:30
|
|
|
|
|
2025-06-24 12:46:21 +05:30
|
|
|
|
func NewSetupCommand() *cobra.Command {
|
|
|
|
|
|
setupCmd := &cobra.Command{
|
|
|
|
|
|
Use: "setup",
|
2026-05-12 22:27:05 +05:30
|
|
|
|
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.",
|
2025-06-24 12:46:21 +05:30
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
return cmd.Help()
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setupCmd.AddCommand(NewInstallCommand())
|
|
|
|
|
|
setupCmd.AddCommand(NewRemoveCommand())
|
2026-01-11 19:25:13 +05:30
|
|
|
|
setupCmd.AddCommand(NewInfoCommand())
|
2026-05-26 12:21:39 +05:30
|
|
|
|
setupCmd.AddCommand(NewDoctorCommand())
|
2025-06-24 12:46:21 +05:30
|
|
|
|
|
|
|
|
|
|
return setupCmd
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewInstallCommand() *cobra.Command {
|
|
|
|
|
|
return &cobra.Command{
|
2026-03-31 19:21:01 +05:30
|
|
|
|
Use: "install",
|
2026-05-12 22:27:05 +05:30
|
|
|
|
Short: "Setup PMG config, aliases, and shims for package managers (npm, pnpm, pip, and more)",
|
2026-03-31 19:21:01 +05:30
|
|
|
|
SilenceUsage: true,
|
2025-06-24 12:46:21 +05:30
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2025-08-21 23:01:30 +05:30
|
|
|
|
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
2026-05-12 22:27:05 +05:30
|
|
|
|
return install()
|
2025-06-24 12:46:21 +05:30
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-12 22:27:05 +05:30
|
|
|
|
func install() error {
|
|
|
|
|
|
if err := config.WriteTemplateConfig(); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to write template config: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 14:32:30 +05:30
|
|
|
|
if config.Get().IsManaged() {
|
|
|
|
|
|
fmt.Printf("%s %s\n", ui.Colors.Dim("ℹ"),
|
|
|
|
|
|
fmt.Sprintf("Using globally managed config: %s", config.Get().ConfigFilePath()))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-12 22:27:05 +05:30
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 12:46:21 +05:30
|
|
|
|
func NewRemoveCommand() *cobra.Command {
|
2026-01-01 12:33:52 +05:30
|
|
|
|
cmd := &cobra.Command{
|
2026-03-31 19:21:01 +05:30
|
|
|
|
Use: "remove",
|
2026-05-12 22:27:05 +05:30
|
|
|
|
Short: "Removes pmg aliases and shims from the user's shell config.",
|
2026-03-31 19:21:01 +05:30
|
|
|
|
SilenceUsage: true,
|
2025-06-24 12:46:21 +05:30
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2025-08-21 23:01:30 +05:30
|
|
|
|
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
|
|
|
|
|
|
2026-01-01 12:33:52 +05:30
|
|
|
|
if setupRemoveConfigFile {
|
2026-05-21 14:32:30 +05:30
|
|
|
|
// Only ever remove the per-user file; the globally managed
|
|
|
|
|
|
// config is not ours to delete from a per-user uninstall.
|
|
|
|
|
|
if err := config.RemoveUserConfigFile(); err != nil {
|
|
|
|
|
|
return err
|
2026-01-01 12:33:52 +05:30
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-12 22:27:05 +05:30
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-01 12:33:52 +05:30
|
|
|
|
cfg := alias.DefaultConfig()
|
|
|
|
|
|
rcFileManager, err := alias.NewDefaultRcFileManager(cfg.RcFileName)
|
2025-06-24 12:46:21 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-08-21 18:26:05 +05:30
|
|
|
|
|
2026-01-01 12:33:52 +05:30
|
|
|
|
aliasManager := alias.New(cfg, rcFileManager)
|
2026-05-12 22:27:05 +05:30
|
|
|
|
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
|
2025-06-24 12:46:21 +05:30
|
|
|
|
},
|
|
|
|
|
|
}
|
2026-01-01 12:33:52 +05:30
|
|
|
|
|
|
|
|
|
|
cmd.Flags().BoolVar(&setupRemoveConfigFile, "config-file", false, "Remove the config file")
|
|
|
|
|
|
return cmd
|
2025-06-24 12:46:21 +05:30
|
|
|
|
}
|