mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: Refactor PMG to Maintain Separation of Concerns and Clean Architecture (#19)
* feat: Add separate package manager and resolver * fix: Npm dependency resolver * feat: Add analyzer for malysis query * feat: Add package manager guard as the orchestrator * feat: Add PMG to orchestrate installation * Add concurrent scan execution * Introduce package manager interaction abstraction * feat: Add UI port for guard * Remove refactored source files * Update README * fix: CI script for multi-arch build * ci: goreleaser CI fix * fix: npm command parser to extract package names * feat: Introduce global config primitive * fix: Close results channel for clean goroutine exit * ci: Add container image releaser * test: Improve test for npm resolver * refactor: Analyzer to generalise * Improve UI with additional info * fix: Goreleaser config * fix: npm resolver bug * fix: Fail when command exec workflow fails * fix: Bug with transitive dependency resolution * fix: Synchronize common data update in dependency resolver * chore: Improve log handling * docs: Update README * fix: UI text wrapping * fix: UI handling bugs * feat: Use concurrent dependency resolver
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package npm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/safedep/pmg/analyzer"
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/guard"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/pmg/packagemanager"
|
||||
)
|
||||
|
||||
func executeCommonFlow(ctx context.Context, config config.Config, pm packagemanager.PackageManager, args []string) error {
|
||||
packageResolverConfig := packagemanager.NewDefaultNpmDependencyResolverConfig()
|
||||
packageResolverConfig.IncludeTransitiveDependencies = config.Transitive
|
||||
packageResolverConfig.TransitiveDepth = config.TransitiveDepth
|
||||
packageResolverConfig.IncludeDevDependencies = config.IncludeDevDependencies
|
||||
|
||||
packageResolver, err := packagemanager.NewNpmDependencyResolver(packageResolverConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create npm dependency resolver: %w", err)
|
||||
}
|
||||
|
||||
malysisQueryAnalyzer, err := analyzer.NewMalysisQueryAnalyzer(analyzer.MalysisQueryAnalyzerConfig{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create malysis query analyzer: %w", err)
|
||||
}
|
||||
|
||||
interaction := guard.PackageManagerGuardInteraction{
|
||||
SetStatus: ui.SetStatus,
|
||||
ClearStatus: ui.ClearStatus,
|
||||
GetConfirmationOnMalware: ui.GetConfirmationOnMalware,
|
||||
Block: ui.Block,
|
||||
}
|
||||
|
||||
proxy, err := guard.NewPackageManagerGuard(guard.DefaultPackageManagerGuardConfig(),
|
||||
pm, packageResolver, []analyzer.PackageVersionAnalyzer{malysisQueryAnalyzer}, interaction)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create package manager guard: %w", err)
|
||||
}
|
||||
|
||||
return proxy.Run(ctx, args)
|
||||
}
|
||||
|
||||
func executeNpmFlow(ctx context.Context, config config.Config, args []string) error {
|
||||
packageManager, err := packagemanager.NewNpmPackageManager(packagemanager.DefaultNpmPackageManagerConfig())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create npm package manager: %w", err)
|
||||
}
|
||||
|
||||
return executeCommonFlow(ctx, config, packageManager, args)
|
||||
}
|
||||
|
||||
func executePnpmFlow(ctx context.Context, config config.Config, args []string) error {
|
||||
packageManager, err := packagemanager.NewNpmPackageManager(packagemanager.DefaultPnpmPackageManagerConfig())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create pnpm package manager: %w", err)
|
||||
}
|
||||
|
||||
return executeCommonFlow(ctx, config, packageManager, args)
|
||||
}
|
||||
+10
-31
@@ -2,51 +2,30 @@ package npm
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/safedep/pmg/pkg/common/utils"
|
||||
"github.com/safedep/pmg/pkg/registry"
|
||||
"github.com/safedep/pmg/pkg/wrapper"
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewNpmCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
return &cobra.Command{
|
||||
Use: "npm [action] [package]",
|
||||
Short: "Scan packages from npm registry",
|
||||
Short: "Guard npm package manager",
|
||||
DisableFlagParsing: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
execPath, err := utils.GetExecutablePath(string(registry.RegistryNPM))
|
||||
config, err := config.FromContext(cmd.Context())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "npm not found: %v\n", err)
|
||||
return err
|
||||
ui.Fatalf("Failed to get config: %s", err)
|
||||
}
|
||||
|
||||
if len(args) >= 2 && utils.IsInstallCommand(string(registry.RegistryNPM), args[0]) {
|
||||
// Parse arguments to separate flags and packages
|
||||
flags, packages := utils.ParseNpmInstallArgs(args[1:])
|
||||
|
||||
// If no packages specified, just pass through to npm
|
||||
if len(packages) == 0 {
|
||||
return utils.ExecCmd(execPath, args, []string{})
|
||||
}
|
||||
|
||||
// Create single wrapper instance for all packages
|
||||
pmw := wrapper.NewPackageManagerWrapper(registry.RegistryNPM, flags, packages, args[0])
|
||||
if err := pmw.Wrap(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
err = executeNpmFlow(cmd.Context(), config, args)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to execute npm flow: %s", err)
|
||||
}
|
||||
|
||||
if err := utils.ExecCmd(execPath, args, []string{}); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
+10
-31
@@ -2,51 +2,30 @@ package npm
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/safedep/pmg/pkg/common/utils"
|
||||
"github.com/safedep/pmg/pkg/registry"
|
||||
"github.com/safedep/pmg/pkg/wrapper"
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewPnpmCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
return &cobra.Command{
|
||||
Use: "pnpm [action] [package]",
|
||||
Short: "Scan packages from npm registry",
|
||||
Short: "Guard pnpm package manager",
|
||||
DisableFlagParsing: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
execPath, err := utils.GetExecutablePath(string(registry.RegistryPNPM))
|
||||
config, err := config.FromContext(cmd.Context())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "pnpm not found: %v\n", err)
|
||||
return err
|
||||
ui.Fatalf("Failed to get config: %s", err)
|
||||
}
|
||||
|
||||
if len(args) >= 2 && utils.IsInstallCommand(string(registry.RegistryPNPM), args[0]) {
|
||||
// Parse arguments to separate flags and packages
|
||||
flags, packages := utils.ParseNpmInstallArgs(args[1:])
|
||||
|
||||
// If no packages specified, just pass through to npm
|
||||
if len(packages) == 0 {
|
||||
return utils.ExecCmd(execPath, args, []string{})
|
||||
}
|
||||
|
||||
// Create single wrapper instance for all packages
|
||||
pmw := wrapper.NewPackageManagerWrapper(registry.RegistryPNPM, flags, packages, args[0])
|
||||
if err := pmw.Wrap(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
err = executePnpmFlow(cmd.Context(), config, args)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to execute pnpm flow: %s", err)
|
||||
}
|
||||
|
||||
if err := utils.ExecCmd(execPath, args, []string{}); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user