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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user