mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: Add support for setup-info command (#108)
* feat: Add support for setup-info command * fix: Code review fixes * fix: Add event log info
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/internal/alias"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/pmg/internal/version"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewInfoCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "info",
|
||||
Short: "Show information about PMG setup and configuration.",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
err := executeSetupInfo()
|
||||
if err != nil {
|
||||
ui.ErrorExit(fmt.Errorf("failed to execute setup info: %w", err))
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func executeSetupInfo() error {
|
||||
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
||||
|
||||
// Configuration section
|
||||
cfg := config.Get()
|
||||
configEntries := make(map[string]string)
|
||||
configEntries["Config File"] = cfg.ConfigFilePath()
|
||||
configEntries["Proxy Mode"] = strconv.FormatBool(cfg.Config.ExperimentalProxyMode)
|
||||
ui.PrintInfoSection("Configuration", configEntries)
|
||||
|
||||
// Shell Integration section
|
||||
aliasCfg := alias.DefaultConfig()
|
||||
rcFileManager, err := alias.NewDefaultRcFileManager(aliasCfg.RcFileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create alias manager: %w", err)
|
||||
}
|
||||
|
||||
aliasManager := alias.New(aliasCfg, rcFileManager)
|
||||
isInstalled, err := aliasManager.IsInstalled()
|
||||
if err != nil {
|
||||
isInstalled = false
|
||||
}
|
||||
|
||||
shellEntries := make(map[string]string)
|
||||
shell, err := alias.DetectShell()
|
||||
if err != nil {
|
||||
shell = "unknown"
|
||||
}
|
||||
|
||||
shellEntries["Detected Shell"] = shell
|
||||
shellEntries["Alias Installed"] = strconv.FormatBool(isInstalled)
|
||||
ui.PrintInfoSection("Shell Integration", shellEntries)
|
||||
|
||||
// Security section
|
||||
securityEntries := make(map[string]string)
|
||||
trustedPackages := cfg.Config.TrustedPackages
|
||||
trustedPackagesCount := len(trustedPackages)
|
||||
|
||||
if trustedPackagesCount > 3 {
|
||||
purls := []string{}
|
||||
for _, p := range trustedPackages[0:3] {
|
||||
purls = append(purls, p.Purl)
|
||||
}
|
||||
|
||||
trustedPackagesValue := fmt.Sprintf("%v ...and %d more", purls, trustedPackagesCount-3)
|
||||
securityEntries["Trusted Packages"] = trustedPackagesValue
|
||||
} else if trustedPackagesCount > 0 {
|
||||
purls := []string{}
|
||||
for _, p := range trustedPackages {
|
||||
purls = append(purls, p.Purl)
|
||||
}
|
||||
|
||||
securityEntries["Trusted Packages"] = fmt.Sprintf("%v", purls)
|
||||
} else {
|
||||
securityEntries["Trusted Packages"] = "None"
|
||||
}
|
||||
|
||||
securityEntries["Event Logging"] = strconv.FormatBool(!cfg.Config.SkipEventLogging)
|
||||
securityEntries["Event Log Directory"] = cfg.EventLogDir()
|
||||
|
||||
ui.PrintInfoSection("Security", securityEntries)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -27,6 +27,7 @@ func NewSetupCommand() *cobra.Command {
|
||||
|
||||
setupCmd.AddCommand(NewInstallCommand())
|
||||
setupCmd.AddCommand(NewRemoveCommand())
|
||||
setupCmd.AddCommand(NewInfoCommand())
|
||||
|
||||
return setupCmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user