2025-04-08 02:37:15 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2025-07-02 19:09:27 +05:30
|
|
|
"strconv"
|
2025-04-08 02:37:15 +05:30
|
|
|
|
2025-04-09 23:27:46 +05:30
|
|
|
"github.com/safedep/dry/log"
|
2025-04-30 14:26:38 +05:30
|
|
|
"github.com/safedep/pmg/cmd/npm"
|
2025-06-09 17:37:08 +05:30
|
|
|
"github.com/safedep/pmg/cmd/pypi"
|
2025-06-24 12:46:21 +05:30
|
|
|
"github.com/safedep/pmg/cmd/setup"
|
2025-05-16 09:41:48 +05:30
|
|
|
"github.com/safedep/pmg/cmd/version"
|
2025-05-15 16:50:59 +05:30
|
|
|
"github.com/safedep/pmg/config"
|
2025-06-30 09:47:14 +05:30
|
|
|
"github.com/safedep/pmg/internal/analytics"
|
2025-05-15 16:50:59 +05:30
|
|
|
"github.com/safedep/pmg/internal/ui"
|
2025-08-21 23:01:30 +05:30
|
|
|
appVersion "github.com/safedep/pmg/internal/version"
|
2025-04-08 02:37:15 +05:30
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-15 16:50:59 +05:30
|
|
|
var (
|
|
|
|
|
debug bool
|
|
|
|
|
silent bool
|
|
|
|
|
verbose bool
|
|
|
|
|
logFile string
|
|
|
|
|
globalConfig config.Config
|
|
|
|
|
)
|
2025-04-29 01:33:27 +05:30
|
|
|
|
2025-05-15 16:50:59 +05:30
|
|
|
func main() {
|
2025-04-08 02:37:15 +05:30
|
|
|
cmd := &cobra.Command{
|
|
|
|
|
Use: "pmg",
|
|
|
|
|
TraverseChildren: true,
|
2025-04-29 01:33:27 +05:30
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2025-05-15 16:50:59 +05:30
|
|
|
// Always set this first because we will override the log
|
|
|
|
|
// level if debug or verbose is set
|
|
|
|
|
if logFile != "" {
|
|
|
|
|
os.Setenv("APP_LOG_FILE", logFile)
|
|
|
|
|
os.Setenv("APP_LOG_LEVEL", "info")
|
2025-04-29 01:33:27 +05:30
|
|
|
}
|
2025-05-15 16:50:59 +05:30
|
|
|
|
|
|
|
|
// Set the log level when debug is enabled
|
|
|
|
|
if debug {
|
|
|
|
|
os.Setenv("APP_LOG_LEVEL", "debug")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip stdout logging when debugging is not enabled
|
|
|
|
|
if !debug {
|
|
|
|
|
os.Setenv("APP_LOG_SKIP_STDOUT_LOGGER", "true")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if silent && verbose {
|
|
|
|
|
fmt.Println("pmg: --silent and --verbose cannot be used together")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if silent {
|
|
|
|
|
ui.SetVerbosityLevel(ui.VerbosityLevelSilent)
|
|
|
|
|
} else if verbose {
|
|
|
|
|
ui.SetVerbosityLevel(ui.VerbosityLevelVerbose)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 19:09:27 +05:30
|
|
|
// Check for PMG_INSECURE_INSTALLATION environment variable
|
|
|
|
|
if val := os.Getenv("PMG_INSECURE_INSTALLATION"); val != "" {
|
|
|
|
|
if boolVal, err := strconv.ParseBool(val); err == nil {
|
|
|
|
|
globalConfig.InsecureInstallation = boolVal
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 16:50:59 +05:30
|
|
|
log.InitZapLogger("pmg", "cli")
|
|
|
|
|
cmd.SetContext(globalConfig.Inject(cmd.Context()))
|
2025-04-29 01:33:27 +05:30
|
|
|
},
|
2025-04-08 02:37:15 +05:30
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
if len(args) == 0 {
|
2025-05-15 16:59:13 +05:30
|
|
|
return cmd.Help()
|
2025-04-08 02:37:15 +05:30
|
|
|
}
|
2025-05-15 16:50:59 +05:30
|
|
|
|
2025-04-08 02:37:15 +05:30
|
|
|
return fmt.Errorf("pmg: %s is not a valid command", args[0])
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 16:50:59 +05:30
|
|
|
cmd.PersistentFlags().StringVar(&logFile, "log", "", "Log file to write to")
|
|
|
|
|
cmd.PersistentFlags().BoolVar(&silent, "silent", false, "Silent mode for invisible experience")
|
|
|
|
|
cmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Verbose mode for more information")
|
|
|
|
|
cmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug logging (defaults to stdout)")
|
|
|
|
|
cmd.PersistentFlags().BoolVar(&globalConfig.Transitive, "transitive", true, "Resolve transitive dependencies")
|
|
|
|
|
cmd.PersistentFlags().IntVar(&globalConfig.TransitiveDepth, "transitive-depth", 5,
|
|
|
|
|
"Maximum depth of transitive dependencies to resolve")
|
|
|
|
|
cmd.PersistentFlags().BoolVar(&globalConfig.IncludeDevDependencies, "include-dev-dependencies", false,
|
|
|
|
|
"Include dev dependencies in the dependency graph (slows down resolution)")
|
2025-05-16 19:38:06 +05:30
|
|
|
cmd.PersistentFlags().BoolVar(&globalConfig.DryRun, "dry-run", false, "Dry run skips execution of package manager")
|
|
|
|
|
cmd.PersistentFlags().BoolVar(&globalConfig.Paranoid, "paranoid", false, "Perform active scanning of unknown packages (slow)")
|
2025-04-29 01:33:27 +05:30
|
|
|
|
2025-04-30 14:26:38 +05:30
|
|
|
cmd.AddCommand(npm.NewNpmCommand())
|
|
|
|
|
cmd.AddCommand(npm.NewPnpmCommand())
|
2025-07-28 17:23:32 +05:30
|
|
|
cmd.AddCommand(npm.NewBunCommand())
|
2025-06-09 17:37:08 +05:30
|
|
|
cmd.AddCommand(pypi.NewPipCommand())
|
2025-08-06 21:10:51 +05:30
|
|
|
cmd.AddCommand(pypi.NewUvCommand())
|
2025-05-16 09:41:48 +05:30
|
|
|
cmd.AddCommand(version.NewVersionCommand())
|
2025-06-24 12:46:21 +05:30
|
|
|
cmd.AddCommand(setup.NewSetupCommand())
|
|
|
|
|
cmd.AddCommand(setup.NewRemoveCommand())
|
2025-04-08 02:37:15 +05:30
|
|
|
|
2025-08-21 23:01:30 +05:30
|
|
|
// Print Banner on --help / -h
|
|
|
|
|
cmd.SetHelpFunc(func(command *cobra.Command, args []string) {
|
|
|
|
|
fmt.Print(ui.GeneratePMGBanner(appVersion.Version, appVersion.Commit))
|
|
|
|
|
fmt.Println(command.UsageString())
|
|
|
|
|
})
|
|
|
|
|
|
2025-06-30 09:47:14 +05:30
|
|
|
defer analytics.Close()
|
|
|
|
|
|
|
|
|
|
analytics.TrackCommandRun()
|
|
|
|
|
analytics.TrackCI()
|
|
|
|
|
|
2025-04-08 02:37:15 +05:30
|
|
|
if err := cmd.Execute(); err != nil {
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|