-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile_list.go
191 lines (165 loc) · 3.92 KB
/
profile_list.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type ProfileInfo struct {
name string
size int64
lastModified time.Time
entries int
}
type ListProfileModel struct {
profiles []ProfileInfo
err error
done bool
}
func (m ListProfileModel) Init() tea.Cmd {
return nil
}
func (m ListProfileModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
func (m ListProfileModel) View() string {
if m.err != nil {
return fmt.Sprintf("\n%s %s%sError:%s %v\n\n",
iconX,
colorRed,
colorBold,
colorReset,
m.err,
)
}
if len(m.profiles) == 0 {
return fmt.Sprintf("\n%s %s%sNo profiles found%s\n"+
"%s %s%sCreate one with:%s envman profile create <name>\n\n",
iconInfo,
colorYellow,
colorBold,
colorReset,
iconInfo,
colorGreen,
colorBold,
colorReset,
)
}
var output strings.Builder
output.WriteString(fmt.Sprintf("\n%s %s%sAvailable Profiles:%s (%d total)\n\n",
iconInfo,
colorGreen,
colorBold,
colorReset,
len(m.profiles),
))
output.WriteString(fmt.Sprintf("%s%s%-20s %-8s %-19s %-19s\n",
colorBold,
colorYellow,
"Profile Name",
"Entries",
"Last Modified",
colorReset,
))
output.WriteString(fmt.Sprintf("%s%s%s\n",
colorYellow,
strings.Repeat("-", 70),
colorReset,
))
for _, p := range m.profiles {
profileName := strings.TrimSuffix(p.name, ".env")
output.WriteString(fmt.Sprintf("%s %-20s %s%-8d%s %-19s\n",
colorBold,
profileName,
colorGreen,
p.entries,
colorReset,
p.lastModified.Format("2006-01-02 15:04"),
))
}
output.WriteString(fmt.Sprintf("\n%s %s%sCommands:%s\n",
iconInfo,
colorYellow,
colorBold,
colorReset,
))
output.WriteString(fmt.Sprintf(" • Use '%senvman profile edit <name>%s' to edit a profile\n", colorBold, colorReset))
output.WriteString(fmt.Sprintf(" • Use '%senvman profile delete <name>%s' to delete a profile\n", colorBold, colorReset))
return output.String()
}
func getProfileEntries(filePath string) int {
content, err := os.ReadFile(filePath)
if err != nil {
return 0
}
lines := strings.Split(string(content), "\n")
count := 0
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
count++
}
}
return count
}
func ListProfiles() error {
currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %v", err)
}
configPath := filepath.Join("/home", currentUser.Username, ".config", ProjectName, configFileName)
configContent, err := os.ReadFile(configPath)
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")
}
if _, err := os.Stat(profileDir); os.IsNotExist(err) {
model := ListProfileModel{
profiles: []ProfileInfo{},
}
p := tea.NewProgram(model)
p.Run()
return nil
}
entries, err := os.ReadDir(profileDir)
if err != nil {
return fmt.Errorf("failed to read profiles directory: %v", err)
}
var profiles []ProfileInfo
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".env") {
info, err := entry.Info()
if err != nil {
continue
}
fullPath := filepath.Join(profileDir, entry.Name())
entryCount := getProfileEntries(fullPath)
profiles = append(profiles, ProfileInfo{
name: entry.Name(),
size: info.Size(),
lastModified: info.ModTime(),
entries: entryCount,
})
}
}
model := ListProfileModel{
profiles: profiles,
}
p := tea.NewProgram(model)
p.Run()
return nil
}