refactor(packagemanager,cli,tui): move packages in specific subfolders
This commit is contained in:
parent
11b8541630
commit
e49138fdd2
34 changed files with 18 additions and 18 deletions
80
internal/utils/utils.go
Normal file
80
internal/utils/utils.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
func GetSudoPassword() (string, error) {
|
||||
var password string
|
||||
form := huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewInput().
|
||||
Title("Enter your password").
|
||||
EchoMode(huh.EchoModePassword).
|
||||
Value(&password),
|
||||
),
|
||||
).WithTheme(huh.ThemeCatppuccin())
|
||||
|
||||
err := form.Run()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error at password-dialog: %v", err)
|
||||
}
|
||||
return password, nil
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
const (
|
||||
maxHistoryLines = 2000
|
||||
historyFileName = "sst_history"
|
||||
)
|
||||
|
||||
func LogToHistory(action, pkg, manager string) error {
|
||||
dirname, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cant obtain config dir: %v", err)
|
||||
}
|
||||
historyFile := filepath.Join(dirname, "sst", historyFileName)
|
||||
|
||||
lines, err := readHistoryLines(historyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||
logEntry := fmt.Sprintf("%s: action: [%s] - package: [%s] - packagemanager: [%s]", timestamp, action, pkg, manager)
|
||||
lines = append(lines, logEntry)
|
||||
|
||||
if len(lines) > maxHistoryLines {
|
||||
lines = lines[len(lines)-maxHistoryLines:]
|
||||
}
|
||||
|
||||
return writeHistoryLines(historyFile, lines)
|
||||
}
|
||||
|
||||
func readHistoryLines(filePath string) ([]string, error) {
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return []string{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return strings.Split(string(content), "\n"), nil
|
||||
}
|
||||
|
||||
func writeHistoryLines(filePath string, lines []string) error {
|
||||
content := strings.Join(lines, "\n")
|
||||
return os.WriteFile(filePath, []byte(content), 0644)
|
||||
}
|
||||
40
internal/utils/utils_test.go
Normal file
40
internal/utils/utils_test.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"system_setup_tool/internal/shell"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
tests := []struct {
|
||||
a, b, want int
|
||||
}{
|
||||
{1, 2, 2},
|
||||
{5, 3, 5},
|
||||
{0, 0, 0},
|
||||
{-1, -5, -1},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got := max(tt.a, tt.b)
|
||||
if got != tt.want {
|
||||
t.Errorf("max(%d, %d) = %d; want %d", tt.a, tt.b, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteShellCommand(t *testing.T) {
|
||||
// Mock execCommand
|
||||
oldExecCommand := shell.ExecCommand
|
||||
defer func() { shell.ExecCommand = oldExecCommand }()
|
||||
|
||||
shell.ExecCommand = func(command string, args ...string) *exec.Cmd {
|
||||
return exec.Command("echo", "mocked command")
|
||||
}
|
||||
|
||||
err := shell.ExecuteShellCommand("test command", "TEST_ENV=value")
|
||||
if err != nil {
|
||||
t.Errorf("executeShellCommand() error = %v; want nil", err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue