mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	A ton of cleanup around how config is handled and go rountines are executed
This commit is contained in:
		
							parent
							
								
									dd51994d8d
								
							
						
					
					
						commit
						71f8fc789f
					
				| @ -28,7 +28,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
|  | |||||||
| @ -8,7 +8,12 @@ wtf: | |||||||
|     refreshInterval: 300 |     refreshInterval: 300 | ||||||
|     secretFile: "~/.wtf/gcal/client_secret.json" |     secretFile: "~/.wtf/gcal/client_secret.json" | ||||||
|   git: |   git: | ||||||
|  |     commitCount: 10 | ||||||
|     refreshInterval: 8 |     refreshInterval: 8 | ||||||
|  |     repository: "/Users/chris/Documents/Lendesk/core-api" | ||||||
|  |   github: | ||||||
|  |     refreshInterval: 900 | ||||||
|  |     repo: "core-api" | ||||||
|   jira: |   jira: | ||||||
|     refreshInterval: 900 |     refreshInterval: 900 | ||||||
|   opsgenie: |   opsgenie: | ||||||
|  | |||||||
| @ -30,7 +30,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,28 +1,23 @@ | |||||||
| package git | package git | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"fmt" | ||||||
| 	"os/exec" | 	"os/exec" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"github.com/senorprogrammer/wtf/wtf" | 	"github.com/senorprogrammer/wtf/wtf" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Client struct { | type Client struct{} | ||||||
| 	CommitCount int |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| func NewClient() *Client { | func NewClient() *Client { | ||||||
| 	client := Client{ | 	return &Client{} | ||||||
| 		CommitCount: 10, |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return &client |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* -------------------- Unexported Functions -------------------- */ | /* -------------------- Exported Functions -------------------- */ | ||||||
| 
 | 
 | ||||||
| func (client *Client) Branch() string { | func (client *Client) Branch() string { | ||||||
| 	arg := []string{"rev-parse", "--abbrev-ref", "HEAD"} | 	arg := []string{client.gitDir(), client.workTree(), "rev-parse", "--abbrev-ref", "HEAD"} | ||||||
| 	cmd := exec.Command("git", arg...) | 	cmd := exec.Command("git", arg...) | ||||||
| 	str := wtf.ExecuteCommand(cmd) | 	str := wtf.ExecuteCommand(cmd) | ||||||
| 
 | 
 | ||||||
| @ -30,7 +25,7 @@ func (client *Client) Branch() string { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (client *Client) ChangedFiles() []string { | func (client *Client) ChangedFiles() []string { | ||||||
| 	arg := []string{"status", "--porcelain"} | 	arg := []string{client.gitDir(), client.workTree(), "status", "--porcelain"} | ||||||
| 	cmd := exec.Command("git", arg...) | 	cmd := exec.Command("git", arg...) | ||||||
| 	str := wtf.ExecuteCommand(cmd) | 	str := wtf.ExecuteCommand(cmd) | ||||||
| 
 | 
 | ||||||
| @ -40,7 +35,9 @@ func (client *Client) ChangedFiles() []string { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (client *Client) Commits() []string { | func (client *Client) Commits() []string { | ||||||
| 	arg := []string{"log", "--date=format:\"%b %d, %Y\"", "-n 10", "--pretty=format:\"[forestgreen]%h [white]%s [grey]%an on %cd[white]\""} | 	numStr := fmt.Sprintf("-n %d", Config.UInt("wtf.git.commitCount", 10)) | ||||||
|  | 
 | ||||||
|  | 	arg := []string{client.gitDir(), client.workTree(), "log", "--date=format:\"%b %d, %Y\"", numStr, "--pretty=format:\"[forestgreen]%h [white]%s [grey]%an on %cd[white]\""} | ||||||
| 	cmd := exec.Command("git", arg...) | 	cmd := exec.Command("git", arg...) | ||||||
| 	str := wtf.ExecuteCommand(cmd) | 	str := wtf.ExecuteCommand(cmd) | ||||||
| 
 | 
 | ||||||
| @ -50,9 +47,19 @@ func (client *Client) Commits() []string { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (client *Client) Repository() string { | func (client *Client) Repository() string { | ||||||
| 	arg := []string{"rev-parse", "--show-toplevel"} | 	arg := []string{client.gitDir(), client.workTree(), "rev-parse", "--show-toplevel"} | ||||||
| 	cmd := exec.Command("git", arg...) | 	cmd := exec.Command("git", arg...) | ||||||
| 	str := wtf.ExecuteCommand(cmd) | 	str := wtf.ExecuteCommand(cmd) | ||||||
| 
 | 
 | ||||||
| 	return str | 	return str | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | /* -------------------- Exported Functions -------------------- */ | ||||||
|  | 
 | ||||||
|  | func (client *Client) gitDir() string { | ||||||
|  | 	return fmt.Sprintf("--git-dir=%s/.git", Config.UString("wtf.git.repository")) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (client *Client) workTree() string { | ||||||
|  | 	return fmt.Sprintf("--work-tree=%s", Config.UString("wtf.git.repository")) | ||||||
|  | } | ||||||
|  | |||||||
| @ -29,7 +29,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										55
									
								
								github/widget.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								github/widget.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,55 @@ | |||||||
|  | package github | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
|  | 	"github.com/gdamore/tcell" | ||||||
|  | 	"github.com/olebedev/config" | ||||||
|  | 	"github.com/rivo/tview" | ||||||
|  | 	"github.com/senorprogrammer/wtf/wtf" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var Config *config.Config | ||||||
|  | 
 | ||||||
|  | type Widget struct { | ||||||
|  | 	wtf.BaseWidget | ||||||
|  | 	View *tview.TextView | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewWidget() *Widget { | ||||||
|  | 	widget := Widget{ | ||||||
|  | 		BaseWidget: wtf.BaseWidget{ | ||||||
|  | 			Name:        "Github", | ||||||
|  | 			RefreshedAt: time.Now(), | ||||||
|  | 			RefreshInt:  Config.UInt("wtf.github.refreshInterval", 900), | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	widget.addView() | ||||||
|  | 
 | ||||||
|  | 	return &widget | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /* -------------------- Exported Functions -------------------- */ | ||||||
|  | 
 | ||||||
|  | func (widget *Widget) Refresh() { | ||||||
|  | 	widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name)) | ||||||
|  | 	widget.RefreshedAt = time.Now() | ||||||
|  | 
 | ||||||
|  | 	widget.View.Clear() | ||||||
|  | 	fmt.Fprintf(widget.View, "%s", "github") | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /* -------------------- Unexported Functions -------------------- */ | ||||||
|  | 
 | ||||||
|  | func (widget *Widget) addView() { | ||||||
|  | 	view := tview.NewTextView() | ||||||
|  | 
 | ||||||
|  | 	view.SetBorder(true) | ||||||
|  | 	view.SetBorderColor(tcell.ColorGray) | ||||||
|  | 	view.SetDynamicColors(true) | ||||||
|  | 	view.SetTitle(widget.Name) | ||||||
|  | 
 | ||||||
|  | 	widget.View = view | ||||||
|  | } | ||||||
| @ -27,7 +27,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
| @ -35,7 +34,7 @@ func NewWidget() *Widget { | |||||||
| /* -------------------- Exported Functions -------------------- */ | /* -------------------- Exported Functions -------------------- */ | ||||||
| 
 | 
 | ||||||
| func (widget *Widget) Refresh() { | func (widget *Widget) Refresh() { | ||||||
| 	widget.View.SetTitle(" JIRA ") | 	widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name)) | ||||||
| 	widget.RefreshedAt = time.Now() | 	widget.RefreshedAt = time.Now() | ||||||
| 
 | 
 | ||||||
| 	widget.View.Clear() | 	widget.View.Clear() | ||||||
|  | |||||||
| @ -28,7 +28,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
|  | |||||||
| @ -28,7 +28,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
|  | |||||||
| @ -30,7 +30,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
|  | |||||||
| @ -29,7 +29,6 @@ func NewWidget() *Widget { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.addView() | 	widget.addView() | ||||||
| 	go wtf.Refresh(&widget) |  | ||||||
| 
 | 
 | ||||||
| 	return &widget | 	return &widget | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										22
									
								
								wtf.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								wtf.go
									
									
									
									
									
								
							| @ -8,12 +8,14 @@ import ( | |||||||
| 	"github.com/senorprogrammer/wtf/bamboohr" | 	"github.com/senorprogrammer/wtf/bamboohr" | ||||||
| 	"github.com/senorprogrammer/wtf/gcal" | 	"github.com/senorprogrammer/wtf/gcal" | ||||||
| 	"github.com/senorprogrammer/wtf/git" | 	"github.com/senorprogrammer/wtf/git" | ||||||
|  | 	"github.com/senorprogrammer/wtf/github" | ||||||
| 	"github.com/senorprogrammer/wtf/homedir" | 	"github.com/senorprogrammer/wtf/homedir" | ||||||
| 	"github.com/senorprogrammer/wtf/jira" | 	"github.com/senorprogrammer/wtf/jira" | ||||||
| 	"github.com/senorprogrammer/wtf/opsgenie" | 	"github.com/senorprogrammer/wtf/opsgenie" | ||||||
| 	"github.com/senorprogrammer/wtf/security" | 	"github.com/senorprogrammer/wtf/security" | ||||||
| 	"github.com/senorprogrammer/wtf/status" | 	"github.com/senorprogrammer/wtf/status" | ||||||
| 	"github.com/senorprogrammer/wtf/weather" | 	"github.com/senorprogrammer/wtf/weather" | ||||||
|  | 	"github.com/senorprogrammer/wtf/wtf" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var Config = loadConfig() | var Config = loadConfig() | ||||||
| @ -30,12 +32,7 @@ func loadConfig() *config.Config { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func refresher(stat *status.Widget, app *tview.Application) { | func refresher(stat *status.Widget, app *tview.Application) { | ||||||
| 	refreshInterval, err := Config.Int("wtf.refreshInterval") | 	tick := time.NewTicker(time.Duration(Config.UInt("wtf.refreshInterval", 1)) * time.Second) | ||||||
| 	if err != nil { |  | ||||||
| 		refreshInterval = 1 |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	tick := time.NewTicker(time.Duration(refreshInterval) * time.Second) |  | ||||||
| 	quit := make(chan struct{}) | 	quit := make(chan struct{}) | ||||||
| 
 | 
 | ||||||
| 	for { | 	for { | ||||||
| @ -53,30 +50,42 @@ func main() { | |||||||
| 	bamboohr.Config = Config | 	bamboohr.Config = Config | ||||||
| 	bamboo := bamboohr.NewWidget() | 	bamboo := bamboohr.NewWidget() | ||||||
| 	bamboo.Refresh() | 	bamboo.Refresh() | ||||||
|  | 	go wtf.Schedule(bamboo) | ||||||
| 
 | 
 | ||||||
| 	gcal.Config = Config | 	gcal.Config = Config | ||||||
| 	cal := gcal.NewWidget() | 	cal := gcal.NewWidget() | ||||||
| 	cal.Refresh() | 	cal.Refresh() | ||||||
|  | 	go wtf.Schedule(cal) | ||||||
| 
 | 
 | ||||||
| 	git.Config = Config | 	git.Config = Config | ||||||
| 	git := git.NewWidget() | 	git := git.NewWidget() | ||||||
| 	git.Refresh() | 	git.Refresh() | ||||||
|  | 	go wtf.Schedule(git) | ||||||
|  | 
 | ||||||
|  | 	github.Config = Config | ||||||
|  | 	github := github.NewWidget() | ||||||
|  | 	github.Refresh() | ||||||
|  | 	go wtf.Schedule(github) | ||||||
| 
 | 
 | ||||||
| 	jira.Config = Config | 	jira.Config = Config | ||||||
| 	jira := jira.NewWidget() | 	jira := jira.NewWidget() | ||||||
| 	jira.Refresh() | 	jira.Refresh() | ||||||
|  | 	go wtf.Schedule(jira) | ||||||
| 
 | 
 | ||||||
| 	opsgenie.Config = Config | 	opsgenie.Config = Config | ||||||
| 	opsgenie := opsgenie.NewWidget() | 	opsgenie := opsgenie.NewWidget() | ||||||
| 	opsgenie.Refresh() | 	opsgenie.Refresh() | ||||||
|  | 	go wtf.Schedule(opsgenie) | ||||||
| 
 | 
 | ||||||
| 	security.Config = Config | 	security.Config = Config | ||||||
| 	sec := security.NewWidget() | 	sec := security.NewWidget() | ||||||
| 	sec.Refresh() | 	sec.Refresh() | ||||||
|  | 	go wtf.Schedule(sec) | ||||||
| 
 | 
 | ||||||
| 	status.Config = Config | 	status.Config = Config | ||||||
| 	stat := status.NewWidget() | 	stat := status.NewWidget() | ||||||
| 	stat.Refresh() | 	stat.Refresh() | ||||||
|  | 	go wtf.Schedule(stat) | ||||||
| 
 | 
 | ||||||
| 	weather.Config = Config | 	weather.Config = Config | ||||||
| 	weather := weather.NewWidget() | 	weather := weather.NewWidget() | ||||||
| @ -90,6 +99,7 @@ func main() { | |||||||
| 	grid.AddItem(bamboo.View, 0, 0, 2, 1, 0, 0, false) | 	grid.AddItem(bamboo.View, 0, 0, 2, 1, 0, 0, false) | ||||||
| 	grid.AddItem(cal.View, 2, 1, 4, 1, 0, 0, false) | 	grid.AddItem(cal.View, 2, 1, 4, 1, 0, 0, false) | ||||||
| 	grid.AddItem(git.View, 0, 2, 3, 1, 0, 0, false) | 	grid.AddItem(git.View, 0, 2, 3, 1, 0, 0, false) | ||||||
|  | 	grid.AddItem(github.View, 3, 2, 3, 1, 0, 0, false) | ||||||
| 	grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false) | 	grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false) | ||||||
| 	grid.AddItem(sec.View, 1, 1, 1, 1, 0, 0, false) | 	grid.AddItem(sec.View, 1, 1, 1, 1, 0, 0, false) | ||||||
| 	grid.AddItem(opsgenie.View, 2, 0, 3, 1, 0, 0, false) | 	grid.AddItem(opsgenie.View, 2, 0, 3, 1, 0, 0, false) | ||||||
|  | |||||||
| @ -4,12 +4,12 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Refresher interface { | type Scheduler interface { | ||||||
| 	Refresh() | 	Refresh() | ||||||
| 	RefreshInterval() int | 	RefreshInterval() int | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func Refresh(widget Refresher) { | func Schedule(widget Scheduler) { | ||||||
| 	tick := time.NewTicker(time.Duration(widget.RefreshInterval()) * time.Second) | 	tick := time.NewTicker(time.Duration(widget.RefreshInterval()) * time.Second) | ||||||
| 	quit := make(chan struct{}) | 	quit := make(chan struct{}) | ||||||
| 
 | 
 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user