diff --git a/.gitignore b/.gitignore index 5045c84..abd9541 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist config.toml +main diff --git a/internal/tui/config.go b/internal/tui/config.go index 9b833c1..a5fc3b4 100644 --- a/internal/tui/config.go +++ b/internal/tui/config.go @@ -13,7 +13,7 @@ func GenerateConfigTOML(cv ConfigValues) (string, error) { // [server] sb.WriteString("# Server Configuration\n") sb.WriteString("[server]\n") - sb.WriteString("port = 8080\n") + sb.WriteString(fmt.Sprintf("port = %s\n", cv.Server["port"])) sb.WriteString(fmt.Sprintf("timeout_seconds = %s\n", cv.Server["timeout"])) sb.WriteString(fmt.Sprintf("environment = %q\n", cv.Server["environment"])) sb.WriteString("\n") @@ -31,18 +31,16 @@ func GenerateConfigTOML(cv ConfigValues) (string, error) { sb.WriteString("# Certificate Options\n") sb.WriteString("[certificate]\n") sb.WriteString(fmt.Sprintf("mapped_dir = %q\n", cv.Cert["cert_dir_path"])) - sb.WriteString(fmt.Sprintf("cert_path = %q\n", "/app/certs/"+cv.Cert["cert_name"])) - sb.WriteString(fmt.Sprintf("key_path = %q\n", "/app/certs/"+cv.Cert["key_name"])) - sb.WriteString(fmt.Sprintf("ca_path = %q\n", "/app/certs/"+cv.Cert["ca_name"])) - sb.WriteString(fmt.Sprintf("server_name = %q\n", cv.Cert["server_name"])) + sb.WriteString("cert_path = \"/app/certs/client.crt\"\n") + sb.WriteString("key_path = \"/app/certs/client.key\"\n") + sb.WriteString("ca_path = \"/app/certs/ca.crt\"\n") sb.WriteString("\n") - // [application] — hardcoded / pre-defined - sb.WriteString("# Pre-defined options\n") + // [application] sb.WriteString("[application]\n") sb.WriteString("erp = \"TOTVS\"\n") - sb.WriteString("central_server_url = \"https://warden:8080\"\n") - sb.WriteString("api_key = \"super secreto\"\n") + sb.WriteString(fmt.Sprintf("central_server_url = %q\n", cv.Application["central_server_url"])) + sb.WriteString(fmt.Sprintf("enrollment_token = %q\n", cv.Application["enrollment_token"])) sb.WriteString("\n") // [log] — hardcoded defaults diff --git a/internal/tui/model.go b/internal/tui/model.go index bd5da45..e3db2f4 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -3,7 +3,6 @@ package tui import ( "fmt" "os" - "path/filepath" "strconv" "charm.land/bubbles/v2/spinner" @@ -31,6 +30,7 @@ type Model struct { loginForm FormStep wireguardForm FormStep + appForm FormStep serverForm FormStep dbForm FormStep certForm FormStep @@ -50,11 +50,12 @@ type DockerLoginData struct { } type ConfigValues struct { - Login map[string]string - Wireguard map[string]string - Server map[string]string - Database map[string]string - Cert map[string]string + Login map[string]string + Wireguard map[string]string + Server map[string]string + Database map[string]string + Cert map[string]string + Application map[string]string } type AppConfig struct { @@ -70,12 +71,12 @@ type AppConfig struct { MinConns int64 `toml:"min_conns"` } `toml:"database"` Certificates struct { - DirPath string `toml:"mapped_dir"` - CertName string `toml:"cert_path"` - KeyName string `toml:"key_path"` - CAName string `toml:"ca_path"` - ServerName string `toml:"server_name"` + DirPath string `toml:"mapped_dir"` } `toml:"certificate"` + Application struct { + CentralServerURL string `toml:"central_server_url"` + EnrollmentToken string `toml:"enrollment_token"` + } `toml:"application"` } func loadConfig() AppConfig { @@ -91,10 +92,8 @@ func loadConfig() AppConfig { config.Database.MinConns = 2 config.Certificates.DirPath = "/caminho/para/diretorio" - config.Certificates.CertName = "certificado.crt" - config.Certificates.KeyName = "chave.key" - config.Certificates.CAName = "chaveCA.crt" - config.Certificates.ServerName = "client" + + config.Application.CentralServerURL = "https://servidor:8443" _, err := os.Stat("config.toml") if err == nil { @@ -103,12 +102,6 @@ func loadConfig() AppConfig { } } - if err == nil { - config.Certificates.CertName = filepath.Base(config.Certificates.CertName) - config.Certificates.KeyName = filepath.Base(config.Certificates.KeyName) - config.Certificates.CAName = filepath.Base(config.Certificates.CAName) - } - return config } @@ -231,37 +224,29 @@ func InitialModel() Model { Type: FieldTypeNumber, }, }), - certForm: NewFormStep("Certificado", []FormField{ + appForm: NewFormStep("Aplicação", []FormField{ { - Id: "cert_dir_path", - Label: "Caminho para o diretório dos certificados", - Placeholder: "/caminho/para/diretorio", - Default: cfg.Certificates.DirPath, + Id: "central_server_url", + Label: "URL do Servidor Central", + Placeholder: "https://servidor:8443", + Default: cfg.Application.CentralServerURL, Type: FieldTypeText, }, { - Id: "cert_name", - Label: "Nome do arquivo do certificado", - Default: cfg.Certificates.CertName, - Type: FieldTypeText, + Id: "enrollment_token", + Label: "Token de Inscrição", + Placeholder: "token gerado no painel web", + Default: cfg.Application.EnrollmentToken, + Type: FieldTypeText, }, + }), + certForm: NewFormStep("Certificado", []FormField{ { - Id: "key_name", - Label: "Nome do arquivo da chave", - Default: cfg.Certificates.KeyName, - Type: FieldTypeText, - }, - { - Id: "ca_name", - Label: "Nome do arquivo da autoridade certificadora", - Default: cfg.Certificates.CAName, - Type: FieldTypeText, - }, - { - Id: "server_name", - Label: "Nome do servidor", - Default: cfg.Certificates.ServerName, - Type: FieldTypeText, + Id: "cert_dir_path", + Label: "Diretório para armazenar certificados", + Placeholder: "/caminho/para/diretorio", + Default: cfg.Certificates.DirPath, + Type: FieldTypeText, }, }), spinner: s, diff --git a/internal/tui/steps.go b/internal/tui/steps.go index 32427e0..5680ad4 100644 --- a/internal/tui/steps.go +++ b/internal/tui/steps.go @@ -19,6 +19,7 @@ const ( StepRunWireguard // Docker Config + StepAppConfig StepServerConfig StepDatabaseConfig StepCertConfig diff --git a/internal/tui/update.go b/internal/tui/update.go index 23e780b..3878eb3 100644 --- a/internal/tui/update.go +++ b/internal/tui/update.go @@ -77,6 +77,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case StepRunWireguard: return m.updateRunWireguardDocker(msg) + case StepAppConfig: + done, cmd := m.appForm.Update(msg) + + if done { + m.configValues.Application = m.appForm.Values() + m.currentStep = StepServerConfig + } + + return m, cmd case StepServerConfig: done, cmd := m.serverForm.Update(msg) @@ -192,7 +201,7 @@ func (m Model) updateIPQuestion(msg tea.Msg) (tea.Model, tea.Cmd) { case "enter": // Yes if m.cursor == 0 { - m.currentStep = StepServerConfig + m.currentStep = StepAppConfig return m, nil } @@ -262,7 +271,7 @@ func (m Model) updateRunWireguardDocker(msg tea.Msg) (tea.Model, tea.Cmd) { if m.finishedDockerRun && m.dockerRunError != nil { return m, tea.Quit } else if m.finishedDockerRun && m.dockerRunError == nil { - m.currentStep = StepServerConfig + m.currentStep = StepAppConfig } } diff --git a/internal/tui/view.go b/internal/tui/view.go index 4673838..831f4b6 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -53,6 +53,9 @@ func (m Model) View() tea.View { } // App Config Stuff + case StepAppConfig: + body = m.appForm.View() + helpMsg = formMsg case StepServerConfig: body = m.serverForm.View() helpMsg = formMsg