feat: add setup & remove cmd for better UX (#43)

* feat: add setup & remove cmd for better UX

* refactor(alias): improve separation of concerns and fix shell sourcing

* chore: remove extra/unused folder

* refactor: introduce separate files for shells

* fix: use temp files for safe shell config modification
This commit is contained in:
Sahil Bansal
2025-06-24 12:46:21 +05:30
committed by GitHub
parent 3f85282935
commit d363167981
7 changed files with 386 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
package setup
import (
"github.com/safedep/pmg/internal/alias"
"github.com/spf13/cobra"
)
func NewSetupCommand() *cobra.Command {
setupCmd := &cobra.Command{
Use: "setup",
Short: "Manage PMG shell aliases and integration",
Long: "Setup and manage PMG shell aliases that allow you to use 'npm', 'pnpm', 'pip' commands through PMG's security wrapper.",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
setupCmd.AddCommand(NewInstallCommand())
setupCmd.AddCommand(NewRemoveCommand())
return setupCmd
}
func NewInstallCommand() *cobra.Command {
return &cobra.Command{
Use: "install",
Short: "Install PMG aliases for package managers (npm, pnpm, pip)",
Long: "Creates ~/.pmg.rc with package manager aliases and sources it in your shell config files (.bashrc, .zshrc, config.fish)",
RunE: func(cmd *cobra.Command, args []string) error {
config := alias.DefaultConfig()
rcFileManager, err := alias.NewDefaultRcFileManager(config.RcFileName)
if err != nil {
return err
}
aliasManager := alias.New(*config, rcFileManager)
return aliasManager.Install()
},
}
}
func NewRemoveCommand() *cobra.Command {
return &cobra.Command{
Use: "remove",
Short: "Removes pmg aliases from the user's shell config file.",
RunE: func(cmd *cobra.Command, args []string) error {
config := alias.DefaultConfig()
rcFileManager, err := alias.NewDefaultRcFileManager(config.RcFileName)
if err != nil {
return err
}
aliasManager := alias.New(*config, rcFileManager)
return aliasManager.Remove()
},
}
}