From 8cfc830f964a599ada9f5faca7f669c58eb3f689 Mon Sep 17 00:00:00 2001 From: MayukhSobo Date: Wed, 15 May 2024 16:56:28 +0530 Subject: [PATCH] Added the logic to get the input from the textinput when connect key is pressed --- db/database.go | 8 ++++++++ internal/app/home/home.go | 7 +++++++ internal/app/home/update.go | 13 ++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 db/database.go diff --git a/db/database.go b/db/database.go new file mode 100644 index 0000000..a017697 --- /dev/null +++ b/db/database.go @@ -0,0 +1,8 @@ +package db + +type Database interface { + Connect() + Ping() + Execute() + Close() +} diff --git a/internal/app/home/home.go b/internal/app/home/home.go index 235ef82..3b691b3 100644 --- a/internal/app/home/home.go +++ b/internal/app/home/home.go @@ -1,7 +1,9 @@ package home import ( + "math/rand" "os" + "spectacle/db" "spectacle/logger" tea "github.com/charmbracelet/bubbletea" @@ -11,6 +13,11 @@ func (m HomeScreenModel) Init() tea.Cmd { return nil } +func ping(endpoint string, db *db.Database) (AlertType, error) { + values := []AlertType{GOOD_CONNECTION, NO_CONNECTION} + return values[rand.Intn(2)], nil +} + func Start() { m := NewHomeScreenModel("What is the ETCD endpoint?") p := tea.NewProgram(m, tea.WithAltScreen()) diff --git a/internal/app/home/update.go b/internal/app/home/update.go index 3ae2b90..d6df9ee 100644 --- a/internal/app/home/update.go +++ b/internal/app/home/update.go @@ -18,7 +18,7 @@ func (m HomeScreenModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch { case key.Matches(msg, m.Keys.Command): - logger.Log.Debugf("Command key is pressed!") + logger.Log.Debugf("Command Mode!") m.Help.IsActive = !m.Help.IsActive } if m.Help.IsActive { @@ -31,6 +31,17 @@ func (m HomeScreenModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmd = tea.ClearScreen case key.Matches(msg, m.Keys.Connect): m.Tooltip.Active = !m.Tooltip.Active + // Try to get the address from the input + address := m.Input.model.Value() + alert, err := ping(address, nil) + if err != nil { + logger.Log.Errorf("Encountered error: %+v", err) + break + } + m.Tooltip.Alert = alert + // Connect to the endpoint + // If success, m.Tooltip.Alert = GOOD_CONNECTION + // else m.Tooltip.Alert = NO_CONNECTION cmd = tea.ClearScreen case key.Matches(msg, m.Keys.Clear): m.Input.model.SetValue("")