From e48ba6ba3d2e8ed1faae345037e56cec9d9b0260 Mon Sep 17 00:00:00 2001 From: Sahilb315 Date: Fri, 2 May 2025 22:11:40 +0530 Subject: [PATCH] Enhance pmg outputs by adding colors and removing markdown notions --- go.mod | 1 + pkg/common/utils/security_confirmation.go | 14 ++++++++++---- pkg/common/utils/terminal_colors.go | 20 ++++++++++++++++++++ pkg/common/utils/utils.go | 10 ++++++++++ pkg/wrapper/npm_base.go | 4 +++- 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 pkg/common/utils/terminal_colors.go diff --git a/go.mod b/go.mod index 917ffda..208635c 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.24.1 require ( buf.build/gen/go/safedep/api/grpc/go v1.5.1-20250418165058-162f6b0cc319.2 buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.6-20250418165058-162f6b0cc319.1 + github.com/fatih/color v1.18.0 github.com/jedib0t/go-pretty/v6 v6.6.7 github.com/safedep/dry v0.0.0-20250410092643-c7079e2f9442 github.com/safedep/vet v1.10.1 diff --git a/pkg/common/utils/security_confirmation.go b/pkg/common/utils/security_confirmation.go index 15316b6..c95de40 100644 --- a/pkg/common/utils/security_confirmation.go +++ b/pkg/common/utils/security_confirmation.go @@ -10,14 +10,20 @@ import ( ) func ConfirmInstallation(maliciousPkgs map[string]string) bool { - fmt.Printf("\nWARNING: %d potentially malicious packages detected!\n", len(maliciousPkgs)) - fmt.Println("The following packages have been flagged:") + colors := NewTerminalColors() + + fmt.Printf("\n%s\n", colors.Red("⚠️ WARNING: %d potentially malicious packages detected!", len(maliciousPkgs))) + fmt.Println(colors.Yellow("The following packages have been flagged:")) for name, reason := range maliciousPkgs { - fmt.Printf("- %s: %s\n", name, reason) + fmt.Printf("%s %s: %s\n", + colors.Cyan("•"), // bullet point + colors.Yellow(name), + removeMarkdown(reason), + ) } - fmt.Print("\nDo you want to continue with installation? (y/N): ") + fmt.Print("\n", colors.Green("Do you want to continue with installation? (y/N): ")) reader := bufio.NewReader(os.Stdin) response, err := reader.ReadString('\n') if err != nil { diff --git a/pkg/common/utils/terminal_colors.go b/pkg/common/utils/terminal_colors.go new file mode 100644 index 0000000..367903d --- /dev/null +++ b/pkg/common/utils/terminal_colors.go @@ -0,0 +1,20 @@ +package utils + +import "github.com/fatih/color" + +type TerminalColors struct { + Red func(format string, a ...interface{}) string + Yellow func(format string, a ...interface{}) string + Cyan func(format string, a ...interface{}) string + Green func(format string, a ...interface{}) string +} + +// NewTerminalColors initializes and returns TerminalColors +func NewTerminalColors() *TerminalColors { + return &TerminalColors{ + Red: color.New(color.FgRed, color.Bold).SprintfFunc(), + Yellow: color.New(color.FgYellow).SprintfFunc(), + Cyan: color.New(color.FgCyan).SprintfFunc(), + Green: color.New(color.FgGreen).SprintfFunc(), + } +} diff --git a/pkg/common/utils/utils.go b/pkg/common/utils/utils.go index 19efad2..f4aacdf 100644 --- a/pkg/common/utils/utils.go +++ b/pkg/common/utils/utils.go @@ -76,3 +76,13 @@ func IsInstallCommand(pkgManager, cmd string) bool { } return false } + +func removeMarkdown(text string) string { + // Remove bold asterisks + text = strings.ReplaceAll(text, "**", "") + // Remove italic asterisks + text = strings.ReplaceAll(text, "*", "") + // Remove backticks + text = strings.ReplaceAll(text, "`", "") + return text +} diff --git a/pkg/wrapper/npm_base.go b/pkg/wrapper/npm_base.go index 04e4efa..1d6a1fb 100644 --- a/pkg/wrapper/npm_base.go +++ b/pkg/wrapper/npm_base.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + "github.com/fatih/color" "github.com/safedep/dry/log" "github.com/safedep/pmg/internal/ui" "github.com/safedep/pmg/pkg/analyser" @@ -133,7 +134,8 @@ func (pmw *PackageManagerWrapper) analyzeDependencies(ctx context.Context, deps log.Infof("Installation canceled due to security concerns") return fmt.Errorf("installation canceled") } - log.Warnf("Continuing installation despite security warnings...") + yellow := color.New(color.FgYellow, color.Bold).SprintfFunc() + log.Warnf(yellow("Continuing installation despite security warnings...")) } return nil