[feature] setup bubbletea tui
This commit is contained in:
parent
f763117422
commit
540eba0609
107
cmd/main.go
107
cmd/main.go
@ -10,9 +10,10 @@ import (
|
||||
"os"
|
||||
"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 (
|
||||
@ -20,6 +21,78 @@ var (
|
||||
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() {
|
||||
var db *sql.DB
|
||||
|
||||
@ -64,30 +137,14 @@ func RawConnect(host string, port string) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
PORT = ":8080"
|
||||
p := tea.NewProgram(initalModel())
|
||||
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")
|
||||
if len(args) > 0 {
|
||||
switch args[0] {
|
||||
case "migrate":
|
||||
migrate()
|
||||
case "migrations":
|
||||
migrate()
|
||||
case "reset-migrations":
|
||||
resetMigrations()
|
||||
case "reset-migrate":
|
||||
resetMigrations()
|
||||
case "start":
|
||||
PORT = ":8500"
|
||||
|
||||
fmt.Println("\033[H\033[2J\nSERVER STARTED")
|
||||
fmt.Printf("Connected on port %s", PORT)
|
||||
|
||||
r := h.InitRestRoutes()
|
||||
r := router.InitRestRoutes()
|
||||
log.Fatal(http.ListenAndServe(PORT, r))
|
||||
|
||||
// w := h.InitWsRoutes()
|
||||
// log.Fatal(http.ListenAndServe(":8090", w))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user