mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: Add pmg setup doctor command (#290)
* feat: add doctor check runner core types and logic * feat: add doctor checks for config, binary, directory, and aliases * feat: add doctor checks for sandbox and security features * feat: add protection verification check using test malicious packages * feat: add summarized package manager availability check * feat: add pmg setup doctor command with compact output * feat: add PATH shim verification to doctor command * fix: improve doctor command UX and alias detection - Capitalize all check messages for consistent output - Dim passing checks, color warn/fail for visual clarity - Silence empty Cobra error output on doctor failure - Remove redundant pmg binary check (self-evident) - Fix alias IsInstalled to skip commented-out source lines - Improve protection failure message * refactor: remove package manager availability check from doctor * fix: handle os.RemoveAll error in doctor protection check * refactor: use table layout for setup doctor, extract shared table renderer Move renderTable, truncate, and visibleWidth helpers from cmd/sandbox to internal/ui so both sandbox and setup doctor share them. Rewrite setup doctor output to use the same table structure as sandbox doctor. Fix VisibleWidth to count runes instead of bytes for correct alignment with multi-byte UTF-8 characters. * docs: add pmg setup doctor to README, remove manual verification step * refactor: use constants for check names, rename and inline doctor helpers Address PR review comments: extract check name constants, rename CheckConfigFile to CheckFileExists and CheckDirectoryWritable to CheckDirectoryExists for reusability, inline trivial wrappers (CheckSandbox, CheckSecurityFeature, CheckProxyMode), and add fix hints for all checks with correct config keys. * refactor: inline simple doctor checks into command layer * fix: skip protection check when aliases and shims are inactive Protection checks now fail immediately when shell aliases and shims are both inactive, instead of falsely passing by running through the pmg binary directly. Also clean up summary messages to remove redundant fix hints and truncated paths.
This commit is contained in:
+4
-76
@@ -6,8 +6,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
@@ -128,88 +126,18 @@ func writeJSONIndent(out io.Writer, v any) error {
|
||||
return enc.Encode(v)
|
||||
}
|
||||
|
||||
// renderTable prints rows with ANSI-aware column alignment. The first row is
|
||||
// treated as a header. After each data row, optional continuation lines may be
|
||||
// emitted via the after callback (passed the data row index; -1 for header).
|
||||
func renderTable(out io.Writer, rows [][]string, after func(rowIdx int) error) error {
|
||||
if len(rows) == 0 {
|
||||
return nil
|
||||
}
|
||||
cols := len(rows[0])
|
||||
widths := make([]int, cols)
|
||||
for _, row := range rows {
|
||||
for i, cell := range row {
|
||||
if w := visibleWidth(cell); w > widths[i] {
|
||||
widths[i] = w
|
||||
}
|
||||
}
|
||||
}
|
||||
for rIdx, row := range rows {
|
||||
for i, cell := range row {
|
||||
if i == cols-1 {
|
||||
if _, err := fmt.Fprint(out, cell); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
pad := widths[i] - visibleWidth(cell)
|
||||
if _, err := fmt.Fprint(out, cell, strings.Repeat(" ", pad+2)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := fmt.Fprintln(out); err != nil {
|
||||
return err
|
||||
}
|
||||
if after != nil {
|
||||
dataIdx := rIdx - 1
|
||||
if err := after(dataIdx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return ui.RenderTable(out, rows, after)
|
||||
}
|
||||
|
||||
// firstColumnIndent returns blanks the width of the first column plus the
|
||||
// two-space padding renderTable uses, for continuation-line alignment.
|
||||
func firstColumnIndent(rows [][]string) string {
|
||||
if len(rows) == 0 {
|
||||
return ""
|
||||
}
|
||||
w := 0
|
||||
for _, row := range rows {
|
||||
if v := visibleWidth(row[0]); v > w {
|
||||
w = v
|
||||
}
|
||||
}
|
||||
return strings.Repeat(" ", w+2)
|
||||
}
|
||||
|
||||
var ansiEscapeRe = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`)
|
||||
|
||||
// visibleWidth returns the printable width of s with ANSI escape sequences
|
||||
// stripped — text/tabwriter counts escape bytes as visible chars, misaligning
|
||||
// colored cells.
|
||||
func visibleWidth(s string) int {
|
||||
return len(ansiEscapeRe.ReplaceAllString(s, ""))
|
||||
return ui.FirstColumnIndent(rows)
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
if n <= 3 {
|
||||
return s[:n]
|
||||
}
|
||||
return s[:n-3] + "..."
|
||||
return ui.Truncate(s, n)
|
||||
}
|
||||
|
||||
func truncateLeft(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
if n <= 3 {
|
||||
return s[len(s)-n:]
|
||||
}
|
||||
return "..." + s[len(s)-(n-3):]
|
||||
return ui.TruncateLeft(s, n)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,420 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/internal/alias"
|
||||
"github.com/safedep/pmg/internal/doctor"
|
||||
"github.com/safedep/pmg/internal/shim"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/pmg/internal/version"
|
||||
"github.com/safedep/pmg/sandbox/platform"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
checkConfigFile = "config-file"
|
||||
checkEventLogDir = "event-log-dir"
|
||||
checkShellAliases = "shell-aliases"
|
||||
checkShimDirectory = "shim-directory"
|
||||
checkShimInPath = "shim-in-path"
|
||||
checkProxyMode = "proxy-mode"
|
||||
checkDependencyCooldown = "dependency-cooldown"
|
||||
checkEventLogging = "event-logging"
|
||||
checkSandbox = "sandbox"
|
||||
checkProtectionNpm = "protection-npm"
|
||||
checkProtectionPip = "protection-pip"
|
||||
)
|
||||
|
||||
func NewDoctorCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "doctor",
|
||||
Short: "Validate PMG installation and protection",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
||||
err := executeDoctorChecks()
|
||||
if _, ok := err.(*doctorFailError); ok {
|
||||
cmd.SilenceErrors = true
|
||||
}
|
||||
return err
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type doctorFailError struct{}
|
||||
|
||||
func (e *doctorFailError) Error() string { return "" }
|
||||
func (e *doctorFailError) ExitCode() int { return 1 }
|
||||
|
||||
func executeDoctorChecks() error {
|
||||
cfg := config.Get()
|
||||
|
||||
coreResults := runCoreChecks(cfg)
|
||||
protectionResults := runProtectionChecks(coreResults)
|
||||
allResults := append(coreResults, protectionResults...)
|
||||
|
||||
printResults(allResults)
|
||||
|
||||
if doctor.HasFailures(allResults) {
|
||||
return &doctorFailError{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runCoreChecks(cfg *config.RuntimeConfig) []doctor.CheckResult {
|
||||
checks := []doctor.Check{
|
||||
{
|
||||
Name: checkConfigFile,
|
||||
Category: "Configuration",
|
||||
Run: func() doctor.CheckResult {
|
||||
if _, err := os.Stat(cfg.ConfigFilePath()); err != nil {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Config file not found",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Config file found",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkEventLogDir,
|
||||
Category: "Configuration",
|
||||
Run: func() doctor.CheckResult {
|
||||
info, err := os.Stat(cfg.EventLogDir())
|
||||
if err != nil {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Event log directory not found",
|
||||
}
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Event log path is not a directory",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Event log directory found",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkShellAliases,
|
||||
Category: "Shell Integration",
|
||||
Run: func() doctor.CheckResult {
|
||||
aliasCfg := alias.DefaultConfig()
|
||||
rcFileManager, err := alias.NewDefaultRcFileManager(aliasCfg.RcFileName)
|
||||
if err != nil {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusWarn,
|
||||
Message: fmt.Sprintf("Could not check aliases: %v", err),
|
||||
}
|
||||
}
|
||||
aliasManager := alias.New(aliasCfg, rcFileManager)
|
||||
installed, err := aliasManager.IsInstalled()
|
||||
if err != nil {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusWarn,
|
||||
Message: fmt.Sprintf("Could not determine alias status: %v", err),
|
||||
}
|
||||
}
|
||||
if !installed {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Aliases not installed",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Shell aliases installed",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkShimDirectory,
|
||||
Category: "Shell Integration",
|
||||
Run: func() doctor.CheckResult {
|
||||
sm, err := shim.NewDefaultShimManager()
|
||||
if err != nil {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusWarn,
|
||||
Message: fmt.Sprintf("Could not check shims: %v", err),
|
||||
}
|
||||
}
|
||||
shimDir := sm.GetBinDir()
|
||||
info, err := os.Stat(shimDir)
|
||||
if err != nil || !info.IsDir() {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Shim directory not found",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Shim directory found",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkShimInPath,
|
||||
Category: "Shell Integration",
|
||||
Run: func() doctor.CheckResult {
|
||||
sm, err := shim.NewDefaultShimManager()
|
||||
if err != nil {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusWarn,
|
||||
Message: fmt.Sprintf("Could not check shims: %v", err),
|
||||
}
|
||||
}
|
||||
shimDir := sm.GetBinDir()
|
||||
if slices.Contains(filepath.SplitList(os.Getenv("PATH")), shimDir) {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Shim directory is in PATH",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Shim directory not in PATH",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkProxyMode,
|
||||
Category: "Security",
|
||||
Run: func() doctor.CheckResult {
|
||||
if cfg.IsProxyModeEnabled() {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Proxy mode is enabled",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Proxy mode is disabled",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkDependencyCooldown,
|
||||
Category: "Security",
|
||||
Run: func() doctor.CheckResult {
|
||||
if cfg.Config.DependencyCooldown.Enabled {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Dependency cooldown is enabled",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusWarn,
|
||||
Message: "Dependency cooldown is disabled",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkEventLogging,
|
||||
Category: "Security",
|
||||
Run: func() doctor.CheckResult {
|
||||
if !cfg.Config.SkipEventLogging {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: "Event logging is enabled",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusWarn,
|
||||
Message: "Event logging is disabled",
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: checkSandbox,
|
||||
Category: "Security",
|
||||
Run: func() doctor.CheckResult {
|
||||
sb, err := platform.NewSandbox()
|
||||
available := err == nil && sb != nil && sb.IsAvailable()
|
||||
if !cfg.Config.Sandbox.Enabled {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusWarn,
|
||||
Message: "Sandbox is disabled",
|
||||
}
|
||||
}
|
||||
if !available {
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Sandbox enabled but no driver available on this platform",
|
||||
}
|
||||
}
|
||||
return doctor.CheckResult{
|
||||
Status: doctor.StatusPass,
|
||||
Message: fmt.Sprintf("Sandbox enabled (%s)", sb.Name()),
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
return doctor.RunChecks(checks)
|
||||
}
|
||||
|
||||
func runProtectionChecks(coreResults []doctor.CheckResult) []doctor.CheckResult {
|
||||
if !isInterceptionActive(coreResults) {
|
||||
var results []doctor.CheckResult
|
||||
for _, tc := range doctor.ProtectionTestCases() {
|
||||
results = append(results, doctor.CheckResult{
|
||||
Name: fmt.Sprintf("protection-%s", tc.PackageManager),
|
||||
Category: "Protection",
|
||||
Status: doctor.StatusFail,
|
||||
Message: "Aliases and shims not active",
|
||||
})
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
pmgBinary, err := os.Executable()
|
||||
if err != nil {
|
||||
pmgBinary = "pmg"
|
||||
}
|
||||
|
||||
var results []doctor.CheckResult
|
||||
for _, tc := range doctor.ProtectionTestCases() {
|
||||
result := doctor.RunProtectionCheck(tc, pmgBinary)
|
||||
result.Category = "Protection"
|
||||
result.Name = fmt.Sprintf("protection-%s", tc.PackageManager)
|
||||
results = append(results, result)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func isInterceptionActive(coreResults []doctor.CheckResult) bool {
|
||||
for _, r := range coreResults {
|
||||
if r.Name == checkShellAliases && r.Status == doctor.StatusPass {
|
||||
return true
|
||||
}
|
||||
if r.Name == checkShimInPath && r.Status == doctor.StatusPass {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var checkDisplayNames = map[string]string{
|
||||
checkConfigFile: "Config file",
|
||||
checkEventLogDir: "Event log directory",
|
||||
checkShellAliases: "Shell aliases",
|
||||
checkShimDirectory: "Shim directory",
|
||||
checkShimInPath: "Shim in PATH",
|
||||
checkProxyMode: "Proxy mode",
|
||||
checkDependencyCooldown: "Dependency cooldown",
|
||||
checkEventLogging: "Event logging",
|
||||
checkSandbox: "Sandbox",
|
||||
checkProtectionNpm: "npm protection",
|
||||
checkProtectionPip: "pip protection",
|
||||
}
|
||||
|
||||
var checkFixes = map[string]string{
|
||||
checkConfigFile: "pmg setup install",
|
||||
checkEventLogDir: "pmg setup install",
|
||||
checkShellAliases: "pmg setup install",
|
||||
checkShimDirectory: "pmg setup install",
|
||||
checkShimInPath: "Restart shell or source config",
|
||||
checkProxyMode: "Set proxy.enabled: true in config",
|
||||
checkSandbox: "Set sandbox.enabled: true in config",
|
||||
checkDependencyCooldown: "Set dependency_cooldown.enabled: true in config",
|
||||
checkEventLogging: "Set skip_event_logging: false in config",
|
||||
checkProtectionNpm: "pmg setup install",
|
||||
checkProtectionPip: "pmg setup install",
|
||||
}
|
||||
|
||||
func printResults(results []doctor.CheckResult) {
|
||||
fmt.Println()
|
||||
fmt.Println(ui.Colors.Cyan("Setup Diagnostics"))
|
||||
fmt.Println(ui.Colors.Normal("--------------------"))
|
||||
|
||||
rows := [][]string{{
|
||||
ui.Colors.Bold("STATUS"),
|
||||
ui.Colors.Bold("CHECK"),
|
||||
ui.Colors.Bold("SUMMARY"),
|
||||
ui.Colors.Bold("FIX"),
|
||||
}}
|
||||
for _, r := range results {
|
||||
fix := ui.Colors.Dim("—")
|
||||
if r.Status != doctor.StatusPass {
|
||||
fix = fixHint(r.Name)
|
||||
}
|
||||
rows = append(rows, []string{
|
||||
statusBadge(r.Status),
|
||||
displayName(r.Name),
|
||||
ui.Truncate(r.Message, 60),
|
||||
fix,
|
||||
})
|
||||
}
|
||||
|
||||
if err := ui.RenderTable(os.Stdout, rows, nil); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "render error: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
printSummaryLine(results)
|
||||
}
|
||||
|
||||
func statusBadge(s doctor.CheckStatus) string {
|
||||
switch s {
|
||||
case doctor.StatusPass:
|
||||
return ui.Colors.Green("OK")
|
||||
case doctor.StatusWarn:
|
||||
return ui.Colors.Yellow("WARN")
|
||||
case doctor.StatusFail:
|
||||
return ui.Colors.Red("FAIL")
|
||||
default:
|
||||
return "?"
|
||||
}
|
||||
}
|
||||
|
||||
func displayName(name string) string {
|
||||
if dn, ok := checkDisplayNames[name]; ok {
|
||||
return dn
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func fixHint(name string) string {
|
||||
if fix, ok := checkFixes[name]; ok {
|
||||
return fix
|
||||
}
|
||||
return ui.Colors.Dim("—")
|
||||
}
|
||||
|
||||
func printSummaryLine(results []doctor.CheckResult) {
|
||||
passCount, warnCount, failCount := 0, 0, 0
|
||||
for _, r := range results {
|
||||
switch r.Status {
|
||||
case doctor.StatusPass:
|
||||
passCount++
|
||||
case doctor.StatusWarn:
|
||||
warnCount++
|
||||
case doctor.StatusFail:
|
||||
failCount++
|
||||
}
|
||||
}
|
||||
|
||||
summary := fmt.Sprintf("%d passed", passCount)
|
||||
if warnCount > 0 {
|
||||
summary += fmt.Sprintf(", %d warnings", warnCount)
|
||||
}
|
||||
if failCount > 0 {
|
||||
summary += fmt.Sprintf(", %d failed", failCount)
|
||||
fmt.Printf("%s %s\n", ui.Colors.Red("FAIL"), ui.Colors.Red(summary))
|
||||
} else if warnCount > 0 {
|
||||
fmt.Printf("%s %s\n", ui.Colors.Yellow("WARN"), ui.Colors.Yellow(summary))
|
||||
} else {
|
||||
fmt.Printf("%s %s\n", ui.Colors.Green("OK"), ui.Colors.Green(summary))
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ func NewSetupCommand() *cobra.Command {
|
||||
setupCmd.AddCommand(NewInstallCommand())
|
||||
setupCmd.AddCommand(NewRemoveCommand())
|
||||
setupCmd.AddCommand(NewInfoCommand())
|
||||
setupCmd.AddCommand(NewDoctorCommand())
|
||||
|
||||
return setupCmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user