-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
79 lines (69 loc) · 1.93 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"os"
"os/user"
"path/filepath"
"strings"
)
func EnsureConfig() error {
currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %v", err)
}
configDir := filepath.Join("/home", currentUser.Username, ".config", ProjectName)
configFile := filepath.Join(configDir, configFileName)
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("failed to create config directory: %v", err)
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
content := fmt.Sprintf(configTemplate, currentUser.Username)
if err := os.WriteFile(configFile, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to create config file: %v", err)
}
}
return nil
}
func (m AppModel) View() string {
if m.err != nil {
return fmt.Sprintf("Error: %v\n", m.err)
}
return helpText
}
func (m AppModel) Init() tea.Cmd {
return nil
}
func (m AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.Type == tea.KeyCtrlC {
return m, tea.Quit
}
}
return m, nil
}
func getEnvmanRoot() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", fmt.Errorf("failed to get current user: %v", err)
}
configFilePath := fmt.Sprintf("/home/%s/.config/%s/%s", currentUser.Username, ProjectName, configFileName)
configContent, err := os.ReadFile(configFilePath)
if err != nil {
return "", fmt.Errorf("failed to read config file: %v", err)
}
var profileDir string
for _, line := range strings.Split(string(configContent), "\n") {
if strings.HasPrefix(line, "PROFILE_DIR=") {
profileDir = strings.TrimPrefix(line, "PROFILE_DIR=")
profileDir = strings.Split(profileDir, "#")[0]
profileDir = strings.TrimSpace(profileDir)
break
}
}
if profileDir == "" {
return "", fmt.Errorf("PROFILE_DIR not found in config")
}
return profileDir, nil
}