2025-06-24 12:46:21 +05:30
|
|
|
package setup
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-21 23:01:30 +05:30
|
|
|
"fmt"
|
2026-01-01 12:33:52 +05:30
|
|
|
"os"
|
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"
|
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-01-01 12:33:52 +05:30
|
|
|
var (
|
|
|
|
|
setupRemoveConfigFile = false
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-24 12:46:21 +05:30
|
|
|
func NewSetupCommand() *cobra.Command {
|
|
|
|
|
setupCmd := &cobra.Command{
|
|
|
|
|
Use: "setup",
|
|
|
|
|
Short: "Manage PMG shell aliases and integration",
|
2026-01-01 12:33:52 +05:30
|
|
|
Long: "Setup and manage PMG config, shell aliases 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())
|
|
|
|
|
|
|
|
|
|
return setupCmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewInstallCommand() *cobra.Command {
|
|
|
|
|
return &cobra.Command{
|
|
|
|
|
Use: "install",
|
2026-01-01 12:33:52 +05:30
|
|
|
Short: "Setup PMG config and aliases for package managers (npm, pnpm, pip, and more)",
|
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
|
|
|
cfg := alias.DefaultConfig()
|
|
|
|
|
rcFileManager, err := alias.NewDefaultRcFileManager(cfg.RcFileName)
|
2025-06-24 12:46:21 +05:30
|
|
|
if err != nil {
|
2026-01-01 12:33:52 +05:30
|
|
|
return fmt.Errorf("failed to create alias manager: %w", err)
|
2025-06-24 12:46:21 +05:30
|
|
|
}
|
2025-08-21 18:26:05 +05:30
|
|
|
|
2026-01-01 12:33:52 +05:30
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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{
|
2025-06-24 12:46:21 +05:30
|
|
|
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))
|
|
|
|
|
|
2026-01-01 12:33:52 +05:30
|
|
|
// 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) {
|
|
|
|
|
return fmt.Errorf("failed to remove config file %q: %w", config.ConfigFilePath(), err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2025-06-24 12:46:21 +05:30
|
|
|
return aliasManager.Remove()
|
|
|
|
|
},
|
|
|
|
|
}
|
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
|
|
|
}
|