1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-01-08 05:09:15 +00:00
ctrl/stew.go

145 lines
3.4 KiB
Go
Raw Normal View History

2021-06-14 04:49:47 +00:00
package steward
import (
2021-06-14 15:07:31 +00:00
"bufio"
2021-06-14 04:49:47 +00:00
"flag"
"fmt"
"os"
2021-06-14 15:07:31 +00:00
"github.com/gdamore/tcell/v2"
2021-06-14 04:49:47 +00:00
"github.com/rivo/tview"
)
type Stew struct {
stewardSocket string
}
func NewStew() (*Stew, error) {
stewardSocket := flag.String("stewardSocket", "/usr/local/steward/tmp/steward.sock", "specify the full path of the steward socket file")
flag.Parse()
_, err := os.Stat(*stewardSocket)
if err != nil {
return nil, fmt.Errorf("error: specify the full path to the steward.sock file: %v", err)
}
s := Stew{
stewardSocket: *stewardSocket,
}
return &s, nil
}
func (s *Stew) Start() error {
err := console()
if err != nil {
return fmt.Errorf("error: console failed: %v", err)
}
return nil
}
2021-06-14 15:07:31 +00:00
// ---------------------------------------------------
2021-06-14 04:49:47 +00:00
func console() error {
app := tview.NewApplication()
2021-06-14 15:07:31 +00:00
nodeListForm := tview.NewList().ShowSecondaryText(false)
nodeListForm.SetBorder(true).SetTitle("nodes").SetTitleAlign(tview.AlignLeft)
inputCapture := func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEscape {
app.Stop()
}
return event
}
nodeListForm.SetInputCapture(inputCapture)
2021-06-14 04:49:47 +00:00
2021-06-14 15:07:31 +00:00
reqListForm := tview.NewList().ShowSecondaryText(false)
reqListForm.SetBorder(true).SetTitle("methods").SetTitleAlign(tview.AlignLeft)
reqFillForm := tview.NewForm()
reqFillForm.SetBorder(true).SetTitle("Request values").SetTitleAlign(tview.AlignLeft)
2021-06-14 04:49:47 +00:00
2021-06-14 15:07:31 +00:00
nodeListForm.SetSelectedFunc(func(i int, pri string, sec string, ru rune) {
app.SetFocus(reqListForm)
2021-06-14 04:49:47 +00:00
})
2021-06-14 15:07:31 +00:00
// Create a flex layout.
flexContainer := tview.NewFlex().
AddItem(nodeListForm, 0, 1, true).
AddItem(reqListForm, 0, 1, false).
AddItem(reqFillForm, 0, 2, false)
// Get nodes from file.
nodes, err := getNodeNames("nodeslist.cfg")
if err != nil {
return err
}
2021-06-14 04:49:47 +00:00
2021-06-14 15:07:31 +00:00
// Selected func for node list.
selectedFuncNodes := func() {
var m Method
ma := m.GetMethodsAvailable()
// Select func to create the reqFillForm when req is selected in the reqListForm.
selectedFuncReqList := func() {
currentItem := reqListForm.GetCurrentItem()
currentItemText, _ := reqListForm.GetItemText(currentItem)
reqFillForm.AddButton(fmt.Sprintf("%v", currentItemText), nil)
reqFillForm.AddButton("back", func() {
reqFillForm.Clear(true)
app.SetFocus(reqListForm)
})
inputCapture := func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEscape {
app.SetFocus(nodeListForm)
reqFillForm.Clear(true)
reqListForm.Clear()
}
return event
}
reqFillForm.SetInputCapture(inputCapture)
app.SetFocus(reqFillForm)
}
// Add req items to the req list
for k := range ma.methodhandlers {
reqListForm.AddItem(string(k), "", rune(0), selectedFuncReqList)
}
}
2021-06-14 04:49:47 +00:00
2021-06-14 15:07:31 +00:00
// Add nodes to the node list form.
for _, v := range nodes {
nodeListForm.AddItem(v, "", rune(0), selectedFuncNodes)
2021-06-14 04:49:47 +00:00
}
2021-06-14 15:07:31 +00:00
if err := app.SetRoot(flexContainer, true).Run(); err != nil {
2021-06-14 04:49:47 +00:00
panic(err)
}
return nil
}
2021-06-14 15:07:31 +00:00
// getNodes will load all the node names from a file, and return a slice of
// string values, each representing a unique node.
func getNodeNames(filePath string) ([]string, error) {
fh, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("error: unable to open node file: %v", err)
2021-06-14 04:49:47 +00:00
}
2021-06-14 15:07:31 +00:00
defer fh.Close()
nodes := []string{}
2021-06-14 04:49:47 +00:00
2021-06-14 15:07:31 +00:00
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
node := scanner.Text()
nodes = append(nodes, node)
2021-06-14 04:49:47 +00:00
}
2021-06-14 15:07:31 +00:00
return nodes, nil
2021-06-14 04:49:47 +00:00
}