1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Closes #43. Add CommandRunner module to the app.

CommandRunner allows you to define a terminal command and arguments, run
it on a schedule, and view the output.

Examples:

   ping -3 cisco.com
This commit is contained in:
Chris Cummer 2018-05-17 17:15:03 -07:00
parent c3f14025ba
commit a62b910893
17 changed files with 74 additions and 50 deletions

View File

@ -2,7 +2,6 @@ package bamboohr
import (
"fmt"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
@ -37,12 +36,11 @@ func (widget *Widget) Refresh() {
wtf.Now().Format(wtf.DateFormat),
)
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf(" 👽 Away (%d) ", len(todayItems)))
widget.View.Clear()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(todayItems))
widget.RefreshedAt = time.Now()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(todayItems))
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -33,9 +33,9 @@ func (widget *Widget) Refresh() {
return
}
widget.UpdateRefreshedAt()
widget.View.Clear()
widget.display(widget.clockColl.Sorted())
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -2,6 +2,11 @@ package cmdrunner
import (
"fmt"
"os/exec"
"strings"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
@ -10,9 +15,42 @@ var Config *config.Config
type Widget struct {
wtf.TextWidget
cmd string
args []string
cmd string
result string
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" 🏃 Runner ", "cmdrunner", true),
args: wtf.ToStrs(Config.UList("wtf.mods.cmdrunner.args")),
cmd: Config.UString("wtf.mods.cmdrunner.cmd"),
}
return &widget
}
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.UpdateRefreshedAt()
widget.execute()
widget.View.Clear()
widget.View.SetTitle(fmt.Sprintf(" %s ", widget))
fmt.Fprintf(widget.View, "%s", widget.result)
}
func (widget *Widget) String() string {
args := strings.Join(widget.args, " ")
return fmt.Sprintf("%s %s", widget.cmd, args)
}
func (widget *Widget) execute() {
cmd := exec.Command(widget.cmd, widget.args...)
widget.result = wtf.ExecuteCommand(cmd)
}

View File

@ -35,10 +35,10 @@ func (widget *Widget) Refresh() {
events, _ := Fetch()
widget.UpdateRefreshedAt()
widget.View.Clear()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(events))
widget.RefreshedAt = time.Now()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(events))
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -1,8 +1,6 @@
package git
import (
"time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview"
@ -26,7 +24,7 @@ const helpText = `
type Widget struct {
wtf.TextWidget
app *tview.Application
app *tview.Application
Data []*GitRepo
Idx int
pages *tview.Pages
@ -36,9 +34,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Git ", "git", true),
app: app,
Idx: 0,
pages: pages,
app: app,
Idx: 0,
pages: pages,
}
widget.View.SetInputCapture(widget.keyboardIntercept)
@ -54,10 +52,10 @@ func (widget *Widget) Refresh() {
}
repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories"))
widget.Data = widget.gitRepos(repoPaths)
widget.UpdateRefreshedAt()
widget.Data = widget.gitRepos(repoPaths)
widget.display()
widget.RefreshedAt = time.Now()
}
func (widget *Widget) Next() {

View File

@ -1,8 +1,6 @@
package github
import (
"time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview"
@ -56,8 +54,8 @@ func (widget *Widget) Refresh() {
widget.Data = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories"))
widget.UpdateRefreshedAt()
widget.display()
widget.RefreshedAt = time.Now()
}
func (widget *Widget) Next() {

View File

@ -2,7 +2,6 @@ package jira
import (
"fmt"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
@ -32,6 +31,7 @@ func (widget *Widget) Refresh() {
searchResult, err := IssuesFor(Config.UString("wtf.mods.jira.username"))
widget.UpdateRefreshedAt()
widget.View.Clear()
if err != nil {
@ -49,8 +49,6 @@ func (widget *Widget) Refresh() {
)
fmt.Fprintf(widget.View, "%s", widget.contentFrom(searchResult))
}
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package newrelic
import (
"fmt"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
@ -39,8 +38,8 @@ func (widget *Widget) Refresh() {
appName = app.Name
}
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf(" New Relic: [green]%s[white] ", appName))
widget.View.Clear()
if depErr != nil {
@ -50,8 +49,6 @@ func (widget *Widget) Refresh() {
widget.View.SetWrap(false)
fmt.Fprintf(widget.View, "%s", widget.contentFrom(deploys))
}
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -3,7 +3,6 @@ package opsgenie
import (
"fmt"
"strings"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
@ -33,8 +32,8 @@ func (widget *Widget) Refresh() {
data, err := Fetch()
widget.UpdateRefreshedAt()
widget.View.SetTitle(" ⏰ On Call ")
widget.View.Clear()
if err != nil {
@ -44,8 +43,6 @@ func (widget *Widget) Refresh() {
widget.View.SetWrap(false)
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
}
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package security
import (
"fmt"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
@ -33,10 +32,10 @@ func (widget *Widget) Refresh() {
data := NewSecurityData()
data.Fetch()
widget.UpdateRefreshedAt()
widget.View.Clear()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
widget.RefreshedAt = time.Now()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package status
import (
"fmt"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
@ -33,14 +32,14 @@ func (widget *Widget) Refresh() {
return
}
widget.UpdateRefreshedAt()
widget.View.Clear()
fmt.Fprintf(
widget.View,
"\n%s",
widget.animation(),
)
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -37,6 +37,7 @@ func (widget *Widget) Refresh() {
return
}
widget.UpdateRefreshedAt()
widget.View.Clear()
fmt.Fprintf(
@ -51,8 +52,6 @@ func (widget *Widget) Refresh() {
"Build",
widget.systemInfo.BuildVersion,
)
widget.RefreshedAt = time.Now()
}
func (widget *Widget) prettyDate() string {

View File

@ -3,7 +3,6 @@ package textfile
import (
"fmt"
"io/ioutil"
"time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
@ -53,9 +52,8 @@ func (widget *Widget) Refresh() {
return
}
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf(" 📄 %s ", widget.filePath))
widget.RefreshedAt = time.Now()
widget.View.Clear()
filePath, _ := wtf.ExpandHomeDir(widget.filePath)

View File

@ -3,7 +3,6 @@ package todo
import (
"fmt"
"io/ioutil"
"time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
@ -66,9 +65,9 @@ func (widget *Widget) Refresh() {
return
}
widget.UpdateRefreshedAt()
widget.load()
widget.display()
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package weather
import (
"os"
"time"
owm "github.com/briandowns/openweathermap"
"github.com/gdamore/tcell"
@ -81,8 +80,8 @@ func (widget *Widget) Refresh() {
widget.Data = widget.Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes())))
widget.UpdateRefreshedAt()
widget.display()
widget.RefreshedAt = time.Now()
}
// Next displays data for the next city data in the list. If the current city is the last
@ -219,7 +218,7 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
case "/":
widget.showHelp()
return nil
case "h":
case "h":
widget.Prev()
return nil
case "l":

11
wtf.go
View File

@ -10,6 +10,7 @@ import (
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/bamboohr"
"github.com/senorprogrammer/wtf/clocks"
"github.com/senorprogrammer/wtf/cmdrunner"
"github.com/senorprogrammer/wtf/gcal"
"github.com/senorprogrammer/wtf/git"
"github.com/senorprogrammer/wtf/github"
@ -63,7 +64,7 @@ func buildGrid(modules []wtf.Wtfable) *tview.Grid {
func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlR:
refreshAllModules()
refreshAllWidgets()
case tcell.KeyTab:
FocusTracker.Next()
case tcell.KeyBacktab:
@ -96,9 +97,9 @@ func redrawApp(app *tview.Application) {
}
}
func refreshAllModules() {
for _, module := range Widgets {
go module.Refresh()
func refreshAllWidgets() {
for _, widget := range Widgets {
go widget.Refresh()
}
}
@ -151,6 +152,7 @@ func main() {
bamboohr.Config = Config
clocks.Config = Config
cmdrunner.Config = Config
gcal.Config = Config
git.Config = Config
github.Config = Config
@ -168,6 +170,7 @@ func main() {
Widgets = []wtf.Wtfable{
bamboohr.NewWidget(),
clocks.NewWidget(),
cmdrunner.NewWidget(),
gcal.NewWidget(),
git.NewWidget(app, pages),
github.NewWidget(app, pages),

View File

@ -85,3 +85,7 @@ func (widget *TextWidget) addView() {
widget.View = view
}
func (widget *TextWidget) UpdateRefreshedAt() {
widget.RefreshedAt = time.Now()
}