2020-07-23 22:04:53 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
|
|
|
const url = "https://charm.sh/"
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
status int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkServer() tea.Msg {
|
|
|
|
c := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
res, err := c.Get(url)
|
|
|
|
if err != nil {
|
2020-08-26 15:28:18 -04:00
|
|
|
return errMsg{err}
|
2020-07-23 22:04:53 -04:00
|
|
|
}
|
2022-10-07 18:34:41 -04:00
|
|
|
defer res.Body.Close()
|
|
|
|
|
2020-07-23 22:04:53 -04:00
|
|
|
return statusMsg(res.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
type statusMsg int
|
2020-08-26 15:28:18 -04:00
|
|
|
|
|
|
|
type errMsg struct{ err error }
|
|
|
|
|
|
|
|
// For messages that contain errors it's often handy to also implement the
|
|
|
|
// error interface on the message.
|
|
|
|
func (e errMsg) Error() string { return e.err.Error() }
|
2020-07-23 22:04:53 -04:00
|
|
|
|
2020-10-15 20:26:02 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return checkServer
|
2020-07-23 22:04:53 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 20:26:02 -04:00
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2020-07-23 22:04:53 -04:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case statusMsg:
|
|
|
|
m.status = int(msg)
|
|
|
|
return m, tea.Quit
|
|
|
|
|
|
|
|
case errMsg:
|
|
|
|
m.err = msg
|
|
|
|
return m, tea.Quit
|
|
|
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
if msg.Type == tea.KeyCtrlC {
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:26:02 -04:00
|
|
|
func (m model) View() string {
|
2020-07-23 22:04:53 -04:00
|
|
|
if m.err != nil {
|
|
|
|
return fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf("Checking %s ... ", url)
|
|
|
|
if m.status > 0 {
|
|
|
|
s += fmt.Sprintf("%d %s!", m.status, http.StatusText(m.status))
|
|
|
|
}
|
|
|
|
return "\n" + s + "\n\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-10-15 20:26:02 -04:00
|
|
|
if err := tea.NewProgram(model{}).Start(); err != nil {
|
2020-07-23 22:04:53 -04:00
|
|
|
fmt.Printf("Uh oh, there was an error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|