274 lines
6.4 KiB
Go
274 lines
6.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"charm.land/bubbles/v2/spinner"
|
|
tea "charm.land/bubbletea/v2"
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
type Model struct {
|
|
currentStep step
|
|
cursor int
|
|
width int
|
|
height int
|
|
spinner spinner.Model
|
|
loading bool
|
|
|
|
dockerInstalled bool
|
|
checkDockerDone bool
|
|
checkProgress float64
|
|
downloadDone bool
|
|
downloadMessage string
|
|
downloadError error
|
|
loginData DockerLoginData
|
|
|
|
isPublicIP bool
|
|
|
|
loginForm FormStep
|
|
wireguardForm FormStep
|
|
serverForm FormStep
|
|
dbForm FormStep
|
|
certForm FormStep
|
|
configValues ConfigValues
|
|
|
|
finishedFile bool
|
|
configFileError error
|
|
finishedDockerRun bool
|
|
dockerRunError error
|
|
|
|
err error
|
|
}
|
|
|
|
type DockerLoginData struct {
|
|
Login string
|
|
Password string
|
|
}
|
|
|
|
type ConfigValues struct {
|
|
Login map[string]string
|
|
Wireguard map[string]string
|
|
Server map[string]string
|
|
Database map[string]string
|
|
Cert map[string]string
|
|
}
|
|
|
|
type AppConfig struct {
|
|
Server struct {
|
|
Port int64 `toml:"port"`
|
|
Timeout int64 `toml:"timeout_seconds"`
|
|
Environment string `toml:"environment"`
|
|
} `toml:"server"`
|
|
Database struct {
|
|
Type string `toml:"type"`
|
|
URL string `toml:"url"`
|
|
MaxConns int64 `toml:"max_conns"`
|
|
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"`
|
|
} `toml:"certificate"`
|
|
}
|
|
|
|
func loadConfig() AppConfig {
|
|
var config AppConfig
|
|
|
|
config.Server.Port = 8081
|
|
config.Server.Timeout = 30
|
|
config.Server.Environment = "production"
|
|
|
|
config.Database.Type = "postgres"
|
|
config.Database.URL = "postgres://usuario:senha@banco:5432/app_dono_db"
|
|
config.Database.MaxConns = 10
|
|
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"
|
|
|
|
_, err := os.Stat("config.toml")
|
|
if err == nil {
|
|
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
|
|
fmt.Printf("Error loading config: %v\n", err)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func InitialModel() Model {
|
|
cfg := loadConfig()
|
|
|
|
s := spinner.New()
|
|
s.Spinner = spinner.Dot
|
|
s.Style = SpinnerStyle
|
|
|
|
return Model{
|
|
currentStep: StepCheckDocker,
|
|
loginForm: NewFormStep("Login no Repositório Docker", []FormField{
|
|
{
|
|
Id: "user",
|
|
Label: "Usuário",
|
|
Placeholder: "usuario",
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
Id: "password",
|
|
Label: "Senha",
|
|
Placeholder: "senhaForte123",
|
|
Type: FieldTypePassword,
|
|
},
|
|
}),
|
|
wireguardForm: NewFormStep("Configurações vproxy", []FormField{
|
|
{
|
|
Id: "privkey",
|
|
Label: "Chave Privada",
|
|
Placeholder: "",
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
Id: "vip",
|
|
Label: "IP Virtual",
|
|
Default: "127.0.0.1",
|
|
Placeholder: "127.0.0.1",
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
Id: "psk",
|
|
Label: "Pre-Shared Key",
|
|
Placeholder: "",
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
Id: "proxy_edps",
|
|
Label: "Proxy EDPS",
|
|
Placeholder: "22:127.0.0.1:22",
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
Id: "mtu",
|
|
Label: "MTU",
|
|
Default: "1380",
|
|
Placeholder: "1380",
|
|
Type: FieldTypeNumber,
|
|
},
|
|
{
|
|
Id: "proto",
|
|
Label: "Protocolo",
|
|
Default: "UDP",
|
|
Type: FieldTypeSelect,
|
|
Options: []string{"UDP", "TCP"},
|
|
},
|
|
}),
|
|
serverForm: NewFormStep("Servidor", []FormField{
|
|
{
|
|
Id: "port",
|
|
Label: "Porta",
|
|
Placeholder: "8081",
|
|
Default: strconv.FormatInt(cfg.Server.Port, 10),
|
|
Type: FieldTypeNumber,
|
|
CharLimit: 4,
|
|
},
|
|
{
|
|
Id: "timeout",
|
|
Label: "Timeout (Segundos)",
|
|
Placeholder: "30",
|
|
Default: strconv.FormatInt(cfg.Server.Timeout, 10),
|
|
Type: FieldTypeNumber,
|
|
CharLimit: 3,
|
|
},
|
|
{
|
|
Id: "environment",
|
|
Label: "Ambiente",
|
|
Default: cfg.Server.Environment,
|
|
Type: FieldTypeSelect,
|
|
Options: []string{"development", "production"},
|
|
},
|
|
}),
|
|
dbForm: NewFormStep("Banco de Dados", []FormField{
|
|
{
|
|
Id: "database_type",
|
|
Label: "Tipo do Banco",
|
|
Default: cfg.Database.Type,
|
|
Type: FieldTypeSelect,
|
|
Options: []string{"postgres", "oracle"},
|
|
},
|
|
{
|
|
Id: "database_url",
|
|
Label: "URL de acesso",
|
|
Placeholder: "postgres://usuario:senha@banco:5432/app_dono_db",
|
|
Default: cfg.Database.URL,
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
Id: "max_conns",
|
|
Label: "Conexões ativas (máximo)",
|
|
Placeholder: "10",
|
|
Default: strconv.FormatInt(cfg.Database.MaxConns, 10),
|
|
Type: FieldTypeNumber,
|
|
},
|
|
{
|
|
Id: "min_conns",
|
|
Label: "Conexões ativas (mínimo)",
|
|
Placeholder: "2",
|
|
Default: strconv.FormatInt(cfg.Database.MinConns, 10),
|
|
Type: FieldTypeNumber,
|
|
},
|
|
}),
|
|
certForm: NewFormStep("Certificado", []FormField{
|
|
{
|
|
Id: "cert_dir_path",
|
|
Label: "Caminho para o diretório dos certificados",
|
|
Placeholder: "/caminho/para/diretorio",
|
|
Default: cfg.Certificates.DirPath,
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
Id: "cert_name",
|
|
Label: "Nome do arquivo do certificado",
|
|
Default: cfg.Certificates.CertName,
|
|
Type: FieldTypeText,
|
|
},
|
|
{
|
|
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,
|
|
},
|
|
}),
|
|
spinner: s,
|
|
}
|
|
}
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
return tea.Batch(CheckDockerCmd(), TickCmd(), m.spinner.Tick)
|
|
}
|