You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing an interactive automation tool that runs jobs against hosts in software-defined storage environments (like ceph, scaleio, minio, etc..) I was originally trying this with goterm and after giving up found this.
I'm not sure if I'm doing this right. I'm not seeing all my output and tea.Quit doesn't seem to work. I end up stuck in a loop.
I dumbed down an example of what I'm trying to do; tried to make it as brief as I could. Basically I'm looping over an inventory of hosts serially and inside each loop Im doing a bunch of api calls and remote commands to each hosts.
The output I want the user to see is basically a list of hostnames with status field showing the current state.
Example:
running upgrade...
[host1] SUCCESS
[host2] FAILED
[host3] processing blah.....
I only ever see "running upgrade..." on my screen in what appears to be an infinite loop.
My dumbed down example code:
type message struct {
hostname string
status string
quit bool
}
type model struct {
hosts map[string]string
err error
}
// Init is a noop; needed to satisfy model interface
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.WindowSizeMsg: // TODO figure out why we're getting this, for now workaround by ignoring
return m, nil
case tea.KeyMsg:
if msg.Type == tea.KeyCtrlC {
return m, tea.Quit
}
return m, nil
case message: // TODO am I doing the right thing here
if msg.quit {
return m, tea.Quit
}
m.hosts[msg.hostname] = msg.status
return m, nil
default:
return m, nil
}
}
func (m model) View() string {
if m.err != nil {
return m.err.Error()
}
buff := strings.Builder{}
buff.WriteString("upgrading...\n")
for host, status := range m.hosts {
buff.WriteString(fmt.Sprintf("%-10s %s\n", host, status))
}
return buff.String()
}
func main() {
tty := make(chan message)
defer close(tty)
// cluster map
nodes := []string{"host1", "host2", "host3"}
// start our output thread
go TTYOutputter(tty)
// simulate our workload
for _, node := range nodes {
tty <- message{hostname: node, status: "some status one"}
time.Sleep(time.Millisecond * 300)
tty <- message{hostname: node, status: "some status two"}
time.Sleep(time.Millisecond * 300)
}
tty <- message{quit: true}
}
// TTYOutputter is a go routine wrapped around a channel for tty output
func TTYOutputter(c <-chan message) {
// initialize our output model
m := model{hosts: make(map[string]string)}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
for {
select {
case out, ok := <-c:
if ok {
if out.quit {
return
}
m.Update(out) // TODO not really sure what I do here
continue
} else { // channel is closed
return
}
default: //channel still open, nothing there
time.Sleep(time.Second)
continue
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm writing an interactive automation tool that runs jobs against hosts in software-defined storage environments (like ceph, scaleio, minio, etc..) I was originally trying this with goterm and after giving up found this.
I'm not sure if I'm doing this right. I'm not seeing all my output and tea.Quit doesn't seem to work. I end up stuck in a loop.
I dumbed down an example of what I'm trying to do; tried to make it as brief as I could. Basically I'm looping over an inventory of hosts serially and inside each loop Im doing a bunch of api calls and remote commands to each hosts.
The output I want the user to see is basically a list of hostnames with status field showing the current state.
Example:
running upgrade...
[host1] SUCCESS
[host2] FAILED
[host3] processing blah.....
I only ever see "running upgrade..." on my screen in what appears to be an infinite loop.
My dumbed down example code:
Beta Was this translation helpful? Give feedback.
All reactions