fix: Fail when command exec workflow fails

This commit is contained in:
abhisek
2025-05-15 14:00:18 +05:30
parent c138880593
commit a399e2f59c
4 changed files with 35 additions and 6 deletions
+8 -3
View File
@@ -2,9 +2,9 @@ package npm
import ( import (
_ "embed" _ "embed"
"fmt"
"github.com/safedep/pmg/config" "github.com/safedep/pmg/config"
"github.com/safedep/pmg/internal/ui"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -16,10 +16,15 @@ func NewNpmCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
config, err := config.FromContext(cmd.Context()) config, err := config.FromContext(cmd.Context())
if err != nil { if err != nil {
return fmt.Errorf("failed to get config: %w", err) ui.Fatalf("Failed to get config: %s", err)
} }
return executeNpmFlow(cmd.Context(), config, args) err = executeNpmFlow(cmd.Context(), config, args)
if err != nil {
ui.Fatalf("Failed to execute npm flow: %s", err)
}
return nil
}, },
} }
} }
+8 -3
View File
@@ -2,9 +2,9 @@ package npm
import ( import (
_ "embed" _ "embed"
"fmt"
"github.com/safedep/pmg/config" "github.com/safedep/pmg/config"
"github.com/safedep/pmg/internal/ui"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -16,10 +16,15 @@ func NewPnpmCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
config, err := config.FromContext(cmd.Context()) config, err := config.FromContext(cmd.Context())
if err != nil { if err != nil {
return fmt.Errorf("failed to get config: %w", err) ui.Fatalf("Failed to get config: %s", err)
} }
return executePnpmFlow(cmd.Context(), config, args) err = executePnpmFlow(cmd.Context(), config, args)
if err != nil {
ui.Fatalf("Failed to execute pnpm flow: %s", err)
}
return nil
}, },
} }
} }
+7
View File
@@ -101,6 +101,13 @@ func GetConfirmationOnMalware(malwarePackages []*analyzer.PackageVersionAnalysis
return false, nil return false, nil
} }
func Fatalf(msg string, args ...interface{}) {
ClearStatus()
fmt.Println(Colors.Red(fmt.Sprintf(msg, args...)))
os.Exit(1)
}
// Format the string to be maximum maxWidth. Use newlines to wrap the text. // Format the string to be maximum maxWidth. Use newlines to wrap the text.
func termWidthFormatText(text string, maxWidth int) string { func termWidthFormatText(text string, maxWidth int) string {
words := strings.Split(text, " ") words := strings.Split(text, " ")
+12
View File
@@ -97,6 +97,18 @@ func TestNpmParseCommand(t *testing.T) {
assert.Equal(t, "@types/react", parsedCommand.InstallTargets[1].PackageVersion.Package.Name) assert.Equal(t, "@types/react", parsedCommand.InstallTargets[1].PackageVersion.Package.Name)
}, },
}, },
{
name: "second package has a version",
command: "npm i express @types/node@1.2.3",
assert: func(t *testing.T, parsedCommand *ParsedCommand, err error) {
assert.NoError(t, err)
assert.Equal(t, 2, len(parsedCommand.InstallTargets))
assert.Equal(t, "express", parsedCommand.InstallTargets[0].PackageVersion.Package.Name)
assert.Empty(t, parsedCommand.InstallTargets[0].PackageVersion.Version)
assert.Equal(t, "@types/node", parsedCommand.InstallTargets[1].PackageVersion.Package.Name)
assert.Equal(t, "1.2.3", parsedCommand.InstallTargets[1].PackageVersion.Version)
},
},
} }
for _, tc := range cases { for _, tc := range cases {