Added starting/stopping services from servicesCmd

This commit is contained in:
Christian Muehlhaeuser 2018-03-27 10:08:44 +02:00
parent 90e5792d0d
commit ba3639228e
No known key found for this signature in database
GPG Key ID: BA4CF857DD4117E9
1 changed files with 53 additions and 7 deletions

View File

@ -34,6 +34,8 @@ func servicesForm() (tview.Primitive, error) {
activeOnly := false activeOnly := false
filter := logLevelFilter(6) filter := logLevelFilter(6)
pages := tview.NewPages()
confirmDialog := tview.NewModal()
list := NewServicesView() list := NewServicesView()
err := list.loadModel(false, activeOnly) err := list.loadModel(false, activeOnly)
if err != nil { if err != nil {
@ -58,7 +60,7 @@ func servicesForm() (tview.Primitive, error) {
app.SetFocus(logView) app.SetFocus(logView)
}) })
list.SetChangedFunc(func(index int, primText, secText string, shortcut rune) { list.SetChangedFunc(func(index int, primText, secText string, shortcut rune) {
pipe = selectService(pipe, list.Model[index], filter, logView, serviceView, infoTable) pipe = selectService(pipe, list.Model[index], filter, logView, serviceView, infoTable, confirmDialog)
}) })
logView. logView.
@ -95,9 +97,24 @@ func servicesForm() (tview.Primitive, error) {
AddItem(tview.NewBox(), 0, 1, false), 30, 1, true). AddItem(tview.NewBox(), 0, 1, false), 30, 1, true).
AddItem(tview.NewBox(), 0, 1, false) AddItem(tview.NewBox(), 0, 1, false)
pages := tview.NewPages() confirmDialog.
SetText("Do you want to start the service?").
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
pages.HidePage("confirm_dialog")
if buttonIndex == 0 {
err := toggleServiceState(list)
if err != nil {
panic(err)
}
updateServiceUI(list.Model[list.GetCurrentItem()].Name, serviceView, infoTable, confirmDialog)
}
app.SetFocus(list)
})
pages.AddPage("flex", flex, true, true) pages.AddPage("flex", flex, true, true)
pages.AddPage("dropdown_loglevel", logLevelDialog, true, false) pages.AddPage("dropdown_loglevel", logLevelDialog, true, false)
pages.AddPage("confirm_dialog", confirmDialog, false, false)
menuPages := tview.NewPages() menuPages := tview.NewPages()
searchInput := tview.NewInputField() searchInput := tview.NewInputField()
@ -108,7 +125,7 @@ func servicesForm() (tview.Primitive, error) {
SetDoneFunc(func(key tcell.Key) { SetDoneFunc(func(key tcell.Key) {
search = searchInput.GetText() search = searchInput.GetText()
menuPages.HidePage("search") menuPages.HidePage("search")
pipe = selectService(pipe, list.Model[list.GetCurrentItem()], filter, logView, serviceView, infoTable) pipe = selectService(pipe, list.Model[list.GetCurrentItem()], filter, logView, serviceView, infoTable, confirmDialog)
app.SetFocus(list) app.SetFocus(list)
}) })
@ -126,7 +143,7 @@ func servicesForm() (tview.Primitive, error) {
logLevelDropDown.SetSelectedFunc(func(index int, primText, secText string, shortcut rune) { logLevelDropDown.SetSelectedFunc(func(index int, primText, secText string, shortcut rune) {
filter = logLevelFilter(index) filter = logLevelFilter(index)
pages.HidePage("dropdown_loglevel") pages.HidePage("dropdown_loglevel")
pipe = selectService(pipe, list.Model[list.GetCurrentItem()], filter, logView, serviceView, infoTable) pipe = selectService(pipe, list.Model[list.GetCurrentItem()], filter, logView, serviceView, infoTable, confirmDialog)
}) })
menu.AddItem("Active Services", tcell.KeyF1, func() { menu.AddItem("Active Services", tcell.KeyF1, func() {
@ -147,6 +164,7 @@ func servicesForm() (tview.Primitive, error) {
app.SetFocus(searchInput) app.SetFocus(searchInput)
}) })
menu.AddItem("Start Service", tcell.KeyF8, func() { menu.AddItem("Start Service", tcell.KeyF8, func() {
pages.ShowPage("confirm_dialog")
}) })
// Create the main layout. // Create the main layout.
@ -155,22 +173,34 @@ func servicesForm() (tview.Primitive, error) {
AddItem(pages, 0, 1, true). AddItem(pages, 0, 1, true).
AddItem(menuPages, 1, 1, false) AddItem(menuPages, 1, 1, false)
pipe = selectService(pipe, list.Model[0], filter, logView, serviceView, infoTable) pipe = selectService(pipe, list.Model[0], filter, logView, serviceView, infoTable, confirmDialog)
return layout, nil return layout, nil
} }
func selectService(pipe *LogPipe, l ServiceItem, filter []sdjournal.Match, logView *tview.TextView, serviceView *tview.Flex, infoTable *tview.Table) *LogPipe { func updateServiceUI(name string, serviceView *tview.Flex, infoTable *tview.Table, confirmDialog *tview.Modal) {
u, err := service(l.Name) u, err := service(name)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if u.ActiveState == "active" {
menu.Items[3].Text = "Stop Service"
confirmDialog.SetText(fmt.Sprintf("Do you want to stop service %s?", u.Name))
} else {
menu.Items[3].Text = "Start Service"
confirmDialog.SetText(fmt.Sprintf("Do you want to start service %s?", u.Name))
}
serviceView.SetTitle(u.Name) serviceView.SetTitle(u.Name)
infoTable.SetCell(0, 1, tview.NewTableCell(u.ActiveState)) infoTable.SetCell(0, 1, tview.NewTableCell(u.ActiveState))
infoTable.SetCell(1, 1, tview.NewTableCell(u.Description)) infoTable.SetCell(1, 1, tview.NewTableCell(u.Description))
infoTable.SetCell(3, 1, tview.NewTableCell(u.LoadState)) infoTable.SetCell(3, 1, tview.NewTableCell(u.LoadState))
infoTable.SetCell(4, 1, tview.NewTableCell(u.SubState)) infoTable.SetCell(4, 1, tview.NewTableCell(u.SubState))
}
func selectService(pipe *LogPipe, l ServiceItem, filter []sdjournal.Match, logView *tview.TextView, serviceView *tview.Flex, infoTable *tview.Table, confirmDialog *tview.Modal) *LogPipe {
updateServiceUI(l.Name, serviceView, infoTable, confirmDialog)
// cancel previous reader // cancel previous reader
if pipe != nil { if pipe != nil {
@ -193,6 +223,22 @@ func selectService(pipe *LogPipe, l ServiceItem, filter []sdjournal.Match, logVi
return pipe return pipe
} }
func toggleServiceState(list *ServicesView) error {
s := list.Model[list.GetCurrentItem()]
u, err := service(s.Name)
if err != nil {
return err
}
if u.ActiveState == "active" {
err = stopService(s.Name)
} else {
err = startService(s.Name)
}
return err
}
func init() { func init() {
RootCmd.AddCommand(servicesCmd) RootCmd.AddCommand(servicesCmd)
} }