mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* introduce a persistent config * add tests and refactor config creation * update config handling and add support for removing config * add support to skip suspicious pkgs marked as trusted * add support for config dir Env & unexport functions * small fixes * add assert for dir * fix tests * fix shell source line & trusted pkgs parsing * fix flag inconsistency * update config to read on each invocation and create if does not exist * fix flags value being overridden * remove redundant func call * modify trusted pkg check to be config bound * modify RemoveConfig to rm files & not dir. add tests for paths.go * add versions for package for e2e * modify tests to reset config * fix: Simplify config persistence * fix: Misc comments * fix: Misc fix * fix: Do not overwrite config file if exists * fix: Do not overwrite config file if exists * fix: Config cobra command should override and not replace * fix: Create dir before writing config template * fix: Create dir before writing config template * fix: Misc refactoring * test: Add test for is trusted package version * Update cmd/setup/setup.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> * Update config/config.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> * fix: Remove unused constant in config * fix: Resolve conflict with event logger * docs: Add doc for eventlogger.Logger interface * test: Add E2E for config file creation * fix: Code review fixes --------- Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> Co-authored-by: Sahilb315 <bansalsahil315@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package setup
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/safedep/pmg/config"
|
|
"github.com/safedep/pmg/internal/alias"
|
|
"github.com/safedep/pmg/internal/ui"
|
|
"github.com/safedep/pmg/internal/version"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
setupRemoveConfigFile = false
|
|
)
|
|
|
|
func NewSetupCommand() *cobra.Command {
|
|
setupCmd := &cobra.Command{
|
|
Use: "setup",
|
|
Short: "Manage PMG shell aliases and integration",
|
|
Long: "Setup and manage PMG config, shell aliases that allow you to use package manager commands with security guardrails.",
|
|
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: "Setup PMG config and aliases for package managers (npm, pnpm, pip, and more)",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
|
|
|
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)
|
|
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
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewRemoveCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "remove",
|
|
Short: "Removes pmg aliases from the user's shell config file.",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
aliasManager := alias.New(cfg, rcFileManager)
|
|
return aliasManager.Remove()
|
|
},
|
|
}
|
|
|
|
cmd.Flags().BoolVar(&setupRemoveConfigFile, "config-file", false, "Remove the config file")
|
|
return cmd
|
|
}
|