-
Notifications
You must be signed in to change notification settings - Fork 4
/
init.go
204 lines (160 loc) · 4.26 KB
/
init.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
192
193
194
195
196
197
198
199
200
201
202
203
204
package program
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/teamkeel/keel/codegen"
"github.com/teamkeel/keel/colors"
)
const (
StatusInitializing = iota
StatusInitialized
)
type InitModel struct {
// The directory of the Keel project
ProjectDir string
Err error
Status int
GeneratedFiles codegen.GeneratedFiles
SkippedFiles codegen.GeneratedFiles
// Maintain the current dimensions of the user's terminal
width int
height int
}
func (m *InitModel) Init() tea.Cmd {
m.Status = StatusInitializing
return Init(m.ProjectDir)
}
func (m *InitModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
m.Status = StatusQuitting
return m, tea.Quit
}
case tea.WindowSizeMsg:
// This msg is sent once on program start
// and then subsequently every time the user
// resizes their terminal window.
m.width = msg.Width
m.height = msg.Height
return m, nil
case InitialisedMsg:
m.Status = StatusInitialized
m.GeneratedFiles = msg.GeneratedFiles
m.SkippedFiles = msg.SkippedFiles
if msg.Err != nil {
m.Err = msg.Err
}
return m, tea.Quit
}
return m, nil
}
func (m *InitModel) View() string {
b := strings.Builder{}
// lipgloss will automatically wrap any text based on the current dimensions of the user's term.
s := lipgloss.
NewStyle().
MaxWidth(m.width).
MaxHeight(m.height)
b.WriteString(m.renderInit())
// The final "\n" is important as when Bubbletea exists it resets the last
// line of output, meaning without a new line we'd lose the final line
return s.Render(b.String() + "\n")
}
func (m *InitModel) renderInit() string {
b := strings.Builder{}
switch true {
case m.Err != nil:
b.WriteString(colors.Red(fmt.Sprintf("Error: %s", m.Err.Error())).String())
case m.Status == StatusInitialized:
b.WriteString(fmt.Sprintf("%s\n\n", colors.Green("Ready to build with Keel!").String()))
if len(m.GeneratedFiles) > 0 {
b.WriteString("Generated:\n")
b.WriteString("===================================\n")
for _, f := range m.GeneratedFiles {
b.WriteString(fmt.Sprintf("%s\n", colors.Gray(fmt.Sprintf("- %s", f.Path)).String()))
}
b.WriteString("\n")
}
if len(m.SkippedFiles) > 0 {
b.WriteString("Skipped as there are existing files:\n")
b.WriteString("===================================\n")
for _, f := range m.SkippedFiles {
b.WriteString(fmt.Sprintf("%s\n", colors.Gray(fmt.Sprintf("- %s", f.Path)).String()))
}
b.WriteString("\n")
}
b.WriteString(colors.Cyan("Visit https://docs.keel.so/ to get started.").String())
b.WriteString("\n")
}
return b.String()
}
type InitialisedMsg struct {
GeneratedFiles codegen.GeneratedFiles
SkippedFiles codegen.GeneratedFiles
Err error
}
func Init(dir string) tea.Cmd {
return func() tea.Msg {
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return InitialisedMsg{
Err: fmt.Errorf("Could not create the specified directory."),
}
}
}
files := codegen.GeneratedFiles{}
files = append(files, &codegen.GeneratedFile{
Path: ".gitignore",
Contents: `node_modules/
.DS_Store
*.local
# Keel
.build/
`,
})
files = append(files, &codegen.GeneratedFile{
Path: "schema.keel",
Contents: "// Visit https://docs.keel.so/ for documentation on how to get started",
})
files = append(files, &codegen.GeneratedFile{
Path: "keelconfig.yaml",
Contents: `# Visit https://docs.keel.so/envvars for more
# information about environment variables and secrets
environment:
default:
development:
test:
staging:
production:
secrets:
`,
})
generatedFiles := codegen.GeneratedFiles{}
skippedFiles := codegen.GeneratedFiles{}
for _, file := range files {
if _, err := os.Stat(filepath.Join(dir, file.Path)); err == nil {
skippedFiles = append(skippedFiles, file)
} else {
generatedFiles = append(generatedFiles, file)
}
}
err := generatedFiles.Write(dir)
if err != nil {
return InitialisedMsg{
Err: err,
}
}
return InitialisedMsg{
GeneratedFiles: generatedFiles,
SkippedFiles: skippedFiles,
}
}
}