feat: implemented all features besides wireguard

This commit is contained in:
jb
2026-03-11 11:01:35 -03:00
parent 6870144f39
commit 085cda7251
11 changed files with 539 additions and 73 deletions
+97 -14
View File
@@ -1,11 +1,18 @@
package tui
import (
"fmt"
"math/rand"
"charm.land/bubbles/v2/spinner"
tea "charm.land/bubbletea/v2"
)
const (
imageName = "hub.davinti.com.br:443/app-dono/app-cliente:latest"
configPath = "config.toml"
)
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if key, ok := msg.(tea.KeyPressMsg); ok {
switch key.String() {
@@ -14,11 +21,32 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
switch m.currentStep {
case StepCheckDocker:
return m.updateCheckDocker(msg)
case StepDockerInstall:
return m.updateDockerInstall(msg)
case StepDockerLogin:
done, cmd := m.loginForm.Update(msg)
if done {
m.configValues.Login = m.loginForm.Values()
m.currentStep = StepDownloadImage
return m, DownloadImageCmd(m.configValues.Login["user"], m.configValues.Login["password"])
}
return m, cmd
case StepDownloadImage:
return m.updateDownloadImage(msg)
case StepIPQuestion:
@@ -29,6 +57,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
done, cmd := m.serverForm.Update(msg)
if done {
m.configValues.Server = m.serverForm.Values()
m.currentStep = StepDatabaseConfig
}
@@ -37,6 +66,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
done, cmd := m.dbForm.Update(msg)
if done {
m.configValues.Database = m.dbForm.Values()
m.currentStep = StepCertConfig
}
@@ -45,10 +75,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
done, cmd := m.certForm.Update(msg)
if done {
m.currentStep = StepDone
m.configValues.Cert = m.certForm.Values()
m.currentStep = StepGenerateFile
return m, GenerateConfigFile(m.configValues, configPath)
}
return m, cmd
case StepGenerateFile:
return m.updateGenerateFile(msg)
case StepRunDocker:
return m.updateRunDocker(msg)
case StepDone:
return m, tea.Quit
}
@@ -61,19 +98,19 @@ func (m Model) updateCheckDocker(msg tea.Msg) (tea.Model, tea.Cmd) {
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)
m.checkProgress += 0.2 + (rand.Float64()*0.2 - 0.2)
if m.checkProgress > 1.0 {
m.checkProgress = 1.0
}
return m, tickCmd()
return m, TickCmd()
}
case tea.KeyPressMsg:
if m.checkDockerDone {
if m.checkDockerDone && m.checkProgress == 1 {
if m.dockerInstalled {
m.currentStep = StepIPQuestion
m.loading = true
m.currentStep = StepDockerLogin
} else {
m.currentStep = StepDockerInstall
}
@@ -84,14 +121,27 @@ func (m Model) updateCheckDocker(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m Model) updateDockerInstall(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
switch msg.(type) {
case tea.KeyPressMsg:
return m, tea.Quit
case DockerInstalledMsg:
if msg.Err != nil {
m.err = msg.Err
} else {
}
return m, nil
}
func (m Model) updateDownloadImage(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case ImageDownloadFinishedMsg:
m.downloadDone = true
m.downloadMessage = msg.Message
m.downloadError = msg.Err
return m, nil
case tea.KeyPressMsg:
if m.downloadDone && m.downloadError == nil {
m.currentStep = StepIPQuestion
} else if m.downloadDone {
return m, tea.Quit
}
}
@@ -127,10 +177,43 @@ func (m Model) updateIPQuestion(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
func (m Model) updateWaitForProgramQuit(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
func (m Model) updateGenerateFile(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case ConfigFileMsg:
m.finishedFile = true
m.configFileError = msg.Err
case tea.KeyPressMsg:
return m, tea.Quit
if m.finishedFile && m.configFileError != nil {
return m, tea.Quit
} else if m.finishedFile && m.configFileError == nil {
m.currentStep = StepRunDocker
return m, RunAppContainer(
imageName,
"app-dono-cliente",
configPath,
fmt.Sprintf("/app/%s", configPath),
m.configValues,
)
}
}
return m, nil
}
func (m Model) updateRunDocker(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case DockerRunMsg:
m.finishedDockerRun = true
m.dockerRunError = msg.Err
case tea.KeyPressMsg:
if m.finishedDockerRun && m.dockerRunError != nil {
return m, tea.Quit
} else if m.finishedDockerRun && m.dockerRunError == nil {
m.currentStep = StepDone
}
}
return m, nil