[feature] setup bubbletea tui

This commit is contained in:
Abdellah El Morabit 2025-11-02 17:45:20 +01:00
parent f763117422
commit 540eba0609

View File

@ -10,9 +10,10 @@ import (
"os" "os"
"time" "time"
"github.com/pressly/goose/v3" router "synf/internal/api/routes"
h "synf/internal/api/routes" tea "github.com/charmbracelet/bubbletea"
"github.com/pressly/goose/v3"
) )
var ( var (
@ -20,6 +21,78 @@ var (
PORT string PORT string
) )
type model struct {
choices []string
cursor int
selected map[int]struct{}
}
func initalModel() model {
return model{
choices: []string{"start", "migrate", "reset-migrations"},
selected: make(map[int]struct{}),
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
}
}
return m, nil
}
func (m model) View() string {
s := "Start the application\n\n"
for i, choice := range m.choices {
cursor := " "
if m.cursor == i {
cursor = ">"
}
checked := " "
if _, ok := m.selected[i]; ok {
checked = "x"
}
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
}
s += "\nPress q to quit.\n"
return s
}
func migrate() { func migrate() {
var db *sql.DB var db *sql.DB
@ -64,30 +137,14 @@ func RawConnect(host string, port string) {
} }
func main() { func main() {
args := os.Args[1:] p := tea.NewProgram(initalModel())
PORT = ":8080" if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
fmt.Println("\033[H\033[2J") PORT = ":8500"
if len(args) > 0 {
switch args[0] {
case "migrate":
migrate()
case "migrations":
migrate()
case "reset-migrations":
resetMigrations()
case "reset-migrate":
resetMigrations()
case "start":
fmt.Println("\033[H\033[2J\nSERVER STARTED") r := router.InitRestRoutes()
fmt.Printf("Connected on port %s", PORT)
r := h.InitRestRoutes()
log.Fatal(http.ListenAndServe(PORT, r)) log.Fatal(http.ListenAndServe(PORT, r))
// w := h.InitWsRoutes()
// log.Fatal(http.ListenAndServe(":8090", w))
}
}
} }