Files
pmg/cmd/setup/setup.go
T

72 lines
1.8 KiB
Go
Raw Normal View History

package setup
import (
2025-08-21 23:01:30 +05:30
"fmt"
2025-12-16 22:00:47 +05:30
"github.com/safedep/pmg/config"
"github.com/safedep/pmg/internal/alias"
2025-08-21 23:01:30 +05:30
"github.com/safedep/pmg/internal/ui"
"github.com/safedep/pmg/internal/version"
"github.com/spf13/cobra"
)
func NewSetupCommand() *cobra.Command {
setupCmd := &cobra.Command{
Use: "setup",
Short: "Manage PMG shell aliases and integration",
2025-12-16 23:52:51 +05:30
Long: "Setup and manage PMG config and 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",
2025-12-16 23:52:51 +05:30
Short: "Setup PMG config and aliases for package managers (npm, pnpm, pip)",
Long: "",
RunE: func(cmd *cobra.Command, args []string) error {
2025-08-21 23:01:30 +05:30
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
2025-12-16 22:00:47 +05:30
_, err := config.CreatePmgConfigDir()
if err != nil {
return fmt.Errorf("failed to create config dir: %s", err.Error())
}
cfg := alias.DefaultConfig()
rcFileManager, err := alias.NewDefaultRcFileManager(cfg.RcFileName)
if err != nil {
return err
}
2025-12-16 22:00:47 +05:30
aliasManager := alias.New(cfg, 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 {
2025-08-21 23:01:30 +05:30
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
config := alias.DefaultConfig()
rcFileManager, err := alias.NewDefaultRcFileManager(config.RcFileName)
if err != nil {
return err
}
aliasManager := alias.New(config, rcFileManager)
return aliasManager.Remove()
},
}
}