-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile_delete.go
163 lines (140 loc) · 3.19 KB
/
profile_delete.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
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
type DeleteProfileModel struct {
profileName string
confirmed bool
err error
done bool
profilePath string
}
func (m DeleteProfileModel) Init() tea.Cmd {
return nil
}
func (m DeleteProfileModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
if !m.confirmed {
m.done = true
m.err = fmt.Errorf("operation cancelled by user")
return m, tea.Quit
}
case "y", "Y":
if !m.confirmed {
m.confirmed = true
if err := os.Remove(m.profilePath); err != nil {
m.err = fmt.Errorf("failed to delete profile: %v", err)
return m, tea.Quit
}
m.done = true
return m, tea.Quit
}
case "n", "N":
if !m.confirmed {
m.done = true
m.err = fmt.Errorf("operation cancelled by user")
return m, tea.Quit
}
}
}
return m, nil
}
func (m DeleteProfileModel) View() string {
if m.err != nil {
return fmt.Sprintf("\n%s %s%sError:%s %v\n\n",
iconX,
colorRed,
colorBold,
colorReset,
m.err,
)
}
if m.done && m.confirmed {
return fmt.Sprintf("\n%s %s%sProfile deleted:%s %s\n\n",
iconCheck,
colorGreen,
colorBold,
colorReset,
m.profileName,
)
}
if m.done && !m.confirmed {
return fmt.Sprintf("\n%s %s%sOperation cancelled by user%s\n\n",
iconInfo,
colorYellow,
colorBold,
colorReset,
)
}
if !m.confirmed {
return fmt.Sprintf("\n%s %s%sDelete profile:%s %s\n"+
"%s %s%sAre you sure?%s [y/N]: ",
iconInfo,
colorYellow,
colorBold,
colorReset,
m.profileName,
iconInfo,
colorRed,
colorBold,
colorReset,
)
}
return "\n"
}
func DeleteProfile(name string) error {
if name == "" {
return fmt.Errorf("profile name cannot be empty")
}
name = strings.TrimSpace(name)
if strings.Contains(name, "/") {
return fmt.Errorf("profile name cannot contain '/'")
}
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")
}
profilePath := filepath.Join(profileDir, name+".env")
if _, err := os.Stat(profilePath); os.IsNotExist(err) {
return fmt.Errorf("profile '%s' does not exist", name)
}
model := DeleteProfileModel{
profileName: name,
profilePath: profilePath,
}
p := tea.NewProgram(model)
finalModel, err := p.Run()
if err != nil {
return fmt.Errorf("failed to run UI: %v", err)
}
finalState := finalModel.(DeleteProfileModel)
if finalState.err != nil {
return finalState.err
}
return nil
}