Files
pmg/main.go
T

85 lines
2.3 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
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-05-14 16:09:29 +05:30
"github.com/safedep/pmg/config"
2025-05-14 19:08:25 +05:30
"github.com/safedep/pmg/internal/ui"
"github.com/spf13/cobra"
)
var (
2025-05-14 16:09:29 +05:30
debug bool
2025-05-14 19:08:25 +05:30
silent bool
verbose bool
2025-05-15 14:51:34 +05:30
logFile string
2025-05-14 16:09:29 +05:30
globalConfig config.Config
)
func main() {
cmd := &cobra.Command{
Use: "pmg",
TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
2025-05-15 14:51:34 +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")
}
// Set the log level when debug is enabled
if debug {
os.Setenv("APP_LOG_LEVEL", "debug")
}
2025-05-15 15:26:48 +05:30
// Skip stdout logging when debugging is not enabled
if !debug {
2025-05-15 14:51:34 +05:30
os.Setenv("APP_LOG_SKIP_STDOUT_LOGGER", "true")
}
2025-05-14 16:09:29 +05:30
2025-05-14 19:08:25 +05:30
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-05-15 14:51:34 +05:30
log.InitZapLogger("pmg", "cli")
2025-05-14 16:09:29 +05:30
cmd.SetContext(globalConfig.Inject(cmd.Context()))
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
return nil
}
return fmt.Errorf("pmg: %s is not a valid command", args[0])
},
}
2025-05-15 14:51:34 +05:30
cmd.PersistentFlags().StringVar(&logFile, "log", "", "Log file to write to")
2025-05-14 19:08:25 +05:30
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)")
2025-05-14 16:09:29 +05:30
cmd.PersistentFlags().BoolVar(&globalConfig.Transitive, "transitive", true, "Resolve transitive dependencies")
cmd.PersistentFlags().IntVar(&globalConfig.TransitiveDepth, "transitive-depth", 5,
2025-05-14 16:09:29 +05:30
"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-04-30 14:26:38 +05:30
cmd.AddCommand(npm.NewNpmCommand())
cmd.AddCommand(npm.NewPnpmCommand())
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}