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:
@@ -142,6 +142,34 @@ func (a *AliasManager) Remove() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsInstalled checks if the PMG aliases are sourced in any of the shell config files.
|
||||
func (a *AliasManager) IsInstalled() (bool, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, shell := range a.config.Shells {
|
||||
configPath := filepath.Join(homeDir, shell.Path())
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Warnf("Warning: could not read %s (%s)", shell.Name(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(string(data), a.config.RcFileName) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// buildAliases creates the alias strings for all configured package managers.
|
||||
func (a *AliasManager) buildAliases() []string {
|
||||
aliases := make([]string, 0, len(a.config.PackageManagers))
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package alias
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Shell interface {
|
||||
Source(rcPath string) string
|
||||
@@ -13,3 +17,16 @@ var commentForRemovingShellSource = "# remove aliases by running `pmg setup remo
|
||||
func defaultShellSource(rcPath string) string {
|
||||
return fmt.Sprintf("%s \n[ -f '%s' ] && source '%s' # PMG source aliases\n", commentForRemovingShellSource, rcPath, rcPath)
|
||||
}
|
||||
|
||||
// DetectShell attempts to detect the current shell from the SHELL environment variable.
|
||||
func DetectShell() (string, error) {
|
||||
shellEnv := os.Getenv("SHELL")
|
||||
if shellEnv == "" {
|
||||
return "", fmt.Errorf("SHELL environment variable not set")
|
||||
}
|
||||
|
||||
parts := strings.Split(shellEnv, "/")
|
||||
shellName := parts[len(parts)-1]
|
||||
|
||||
return shellName, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package alias
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDetectShell(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
shellEnvValue string
|
||||
want string
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "bash full path",
|
||||
shellEnvValue: "/bin/bash",
|
||||
want: "bash",
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "zsh full path",
|
||||
shellEnvValue: "/bin/zsh",
|
||||
want: "zsh",
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "bash only name",
|
||||
shellEnvValue: "bash",
|
||||
want: "bash",
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "when shell env is not set",
|
||||
shellEnvValue: "",
|
||||
want: "",
|
||||
wantErr: fmt.Errorf("SHELL environment variable not set"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Setenv("SHELL", tc.shellEnvValue)
|
||||
got, err := DetectShell()
|
||||
|
||||
if tc.wantErr != nil {
|
||||
assert.ErrorContains(t, err, tc.wantErr.Error())
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
var (
|
||||
brandPinkRed = color.RGB(219, 39, 119).Add(color.Bold).SprintFunc() // #DB2777 Brand Pink
|
||||
whiteDim = color.New(color.Faint).SprintFunc()
|
||||
whiteBold = color.New(color.Bold).SprintFunc()
|
||||
)
|
||||
|
||||
func GeneratePMGBanner(version, commit string) string {
|
||||
@@ -21,8 +20,8 @@ func GeneratePMGBanner(version, commit string) string {
|
||||
commit = commit[:6]
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s \t%s: %s %s: %s\n\n", brandPinkRed(pmgASCIIText),
|
||||
whiteDim("version"), whiteBold(version),
|
||||
whiteDim("commit"), whiteBold(commit),
|
||||
return fmt.Sprintf("%s %s: %s %s: %s\n\n", brandPinkRed(pmgASCIIText),
|
||||
whiteDim("version"), Colors.Bold(version),
|
||||
whiteDim("commit"), Colors.Bold(commit),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ type TerminalColors struct {
|
||||
Yellow ColorFn
|
||||
Cyan ColorFn
|
||||
Green ColorFn
|
||||
Bold ColorFn
|
||||
}
|
||||
|
||||
var Colors = TerminalColors{
|
||||
@@ -18,4 +19,5 @@ var Colors = TerminalColors{
|
||||
Yellow: color.New(color.FgYellow).SprintfFunc(),
|
||||
Cyan: color.New(color.FgCyan).SprintfFunc(),
|
||||
Green: color.New(color.FgGreen).SprintfFunc(),
|
||||
Bold: color.New(color.Bold).SprintfFunc(),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// PrintInfoSection prints a formatted block of key-value information.
|
||||
func PrintInfoSection(title string, entries map[string]string) {
|
||||
fmt.Println()
|
||||
fmt.Println(Colors.Cyan(title))
|
||||
fmt.Println(Colors.Normal("--------------------"))
|
||||
|
||||
// Sort keys for consistent output
|
||||
keys := make([]string, 0, len(entries))
|
||||
for k := range entries {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
fmt.Printf("%-25s: %s\n", Colors.Bold(k), entries[k])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user