feat: main flow created
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
)
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if key, ok := msg.(tea.KeyPressMsg); ok {
|
||||
switch key.String() {
|
||||
case "ctrl+c":
|
||||
return m, tea.Quit
|
||||
}
|
||||
}
|
||||
|
||||
switch m.currentStep {
|
||||
case StepCheckDocker:
|
||||
return m.updateCheckDocker(msg)
|
||||
case StepDockerInstall:
|
||||
return m.updateDockerInstall(msg)
|
||||
case StepDownloadImage:
|
||||
return m.updateDownloadImage(msg)
|
||||
case StepIPQuestion:
|
||||
return m.updateIPQuestion(msg)
|
||||
//case StepInstallWireguard
|
||||
// return m.updateInstallWireguard(msg)
|
||||
case StepServerConfig:
|
||||
done, cmd := m.serverForm.Update(msg)
|
||||
|
||||
if done {
|
||||
m.currentStep = StepDatabaseConfig
|
||||
}
|
||||
|
||||
return m, cmd
|
||||
case StepDatabaseConfig:
|
||||
done, cmd := m.dbForm.Update(msg)
|
||||
|
||||
if done {
|
||||
m.currentStep = StepCertConfig
|
||||
}
|
||||
|
||||
return m, cmd
|
||||
case StepCertConfig:
|
||||
done, cmd := m.certForm.Update(msg)
|
||||
|
||||
if done {
|
||||
m.currentStep = StepDone
|
||||
}
|
||||
|
||||
return m, cmd
|
||||
case StepDone:
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) updateCheckDocker(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case DockerCheckedMsg:
|
||||
m.dockerInstalled = msg.Installed
|
||||
m.checkDockerDone = true
|
||||
|
||||
case TickMsg:
|
||||
if m.checkProgress < 1.0 {
|
||||
m.checkProgress += 0.15 + (rand.Float64()*0.15 - 0.15)
|
||||
if m.checkProgress > 1.0 {
|
||||
m.checkProgress = 1.0
|
||||
}
|
||||
return m, tickCmd()
|
||||
}
|
||||
case tea.KeyPressMsg:
|
||||
if m.checkDockerDone {
|
||||
if m.dockerInstalled {
|
||||
m.currentStep = StepIPQuestion
|
||||
} else {
|
||||
m.currentStep = StepDockerInstall
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) updateDockerInstall(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyPressMsg:
|
||||
return m, tea.Quit
|
||||
case DockerInstalledMsg:
|
||||
if msg.Err != nil {
|
||||
m.err = msg.Err
|
||||
} else {
|
||||
m.currentStep = StepIPQuestion
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) updateIPQuestion(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
numOptions := 2
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyPressMsg:
|
||||
switch msg.String() {
|
||||
case "up", "k":
|
||||
if m.cursor > 0 {
|
||||
m.cursor--
|
||||
}
|
||||
case "down", "j":
|
||||
if m.cursor < numOptions-1 {
|
||||
m.cursor++
|
||||
}
|
||||
case "enter":
|
||||
// Yes
|
||||
if m.cursor == 0 {
|
||||
m.currentStep = StepServerConfig
|
||||
return m, nil
|
||||
}
|
||||
|
||||
m.currentStep = StepInstallWireguard
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) updateWaitForProgramQuit(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg.(type) {
|
||||
case tea.KeyPressMsg:
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
Reference in New Issue
Block a user