2025-06-24 12:46:21 +05:30
|
|
|
package setup
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-17 15:41:04 +05:30
|
|
|
"errors"
|
2025-08-21 23:01:30 +05:30
|
|
|
"fmt"
|
|
|
|
|
|
2025-12-16 22:00:47 +05:30
|
|
|
"github.com/safedep/pmg/config"
|
2025-06-24 12:46:21 +05:30
|
|
|
"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"
|
2025-06-24 12:46:21 +05:30
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func NewSetupCommand() *cobra.Command {
|
|
|
|
|
setupCmd := &cobra.Command{
|
|
|
|
|
Use: "setup",
|
|
|
|
|
Short: "Manage PMG shell aliases and integration",
|
2025-12-17 23:19:11 +05:30
|
|
|
Long: "Setup and manage PMG config, shell aliases that allow you to use package manager commands through PMG's security wrapper.",
|
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())
|
|
|
|
|
|
|
|
|
|
return setupCmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewInstallCommand() *cobra.Command {
|
|
|
|
|
return &cobra.Command{
|
|
|
|
|
Use: "install",
|
2025-12-17 23:19:11 +05:30
|
|
|
Short: "Setup PMG config and aliases for package managers (npm, pnpm, pip, and more)",
|
2025-12-16 23:52:51 +05:30
|
|
|
Long: "",
|
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))
|
|
|
|
|
|
2025-12-17 15:41:04 +05:30
|
|
|
cfgPath, err := config.CreateConfig()
|
2025-12-16 22:00:47 +05:30
|
|
|
if err != nil {
|
2025-12-17 15:41:04 +05:30
|
|
|
if errors.Is(err, config.ErrConfigAlreadyExists) {
|
|
|
|
|
fmt.Printf("PMG config already exists at %s\n", cfgPath)
|
|
|
|
|
} else {
|
|
|
|
|
return fmt.Errorf("failed to create config file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Printf("📄 PMG config created at %s\n", cfgPath)
|
2025-12-16 22:00:47 +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
|
|
|
|
2025-12-16 22:00:47 +05:30
|
|
|
aliasManager := alias.New(cfg, rcFileManager)
|
2025-06-24 12:46:21 +05:30
|
|
|
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))
|
|
|
|
|
|
2025-12-17 23:19:11 +05:30
|
|
|
err := config.RemoveConfig()
|
2025-12-17 15:41:04 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 12:46:21 +05:30
|
|
|
config := alias.DefaultConfig()
|
|
|
|
|
rcFileManager, err := alias.NewDefaultRcFileManager(config.RcFileName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-08-21 18:26:05 +05:30
|
|
|
|
|
|
|
|
aliasManager := alias.New(config, rcFileManager)
|
2025-06-24 12:46:21 +05:30
|
|
|
return aliasManager.Remove()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|