From e1e8af5e381fa805d98af4679dfa148fa0d39a58 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 31 Mar 2018 23:37:56 -0700 Subject: [PATCH] Display current git repo and branch --- git/client.go | 54 +++++++++++++++++++++++++++++++++++++++++--- git/git.go | 16 +++++++++++++ git/widget.go | 13 ++++++----- security/firewall.go | 34 ++++++++++++---------------- 4 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 git/git.go diff --git a/git/client.go b/git/client.go index 3c7a7e71..cdfc87c2 100644 --- a/git/client.go +++ b/git/client.go @@ -1,15 +1,63 @@ package git -import () +import ( + //"fmt" + "io/ioutil" + "os/exec" +) type Client struct { - Repository string + CommitCount int + Repository string } func NewClient() *Client { client := Client{ - Repository: "./Documents/Lendesk/core-api", + CommitCount: 10, + Repository: "/Users/Chris/Documents/Lendesk/core-api", } return &client } + +/* -------------------- Unexported Functions -------------------- */ + +func (client *Client) CurrentBranch() string { + arg := []string{"rev-parse", "--abbrev-ref", "HEAD"} + cmd := exec.Command("git", arg...) + str := executeCommand(cmd) + + return str +} + +func (client *Client) ChangedFiles() []string { + files := []string{} + + return files +} + +func (client *Client) Commits() []string { + files := []string{} + + return files +} + +/* -------------------- Unexported Functions -------------------- */ + +func executeCommand(cmd *exec.Cmd) string { + stdout, err := cmd.StdoutPipe() + if err != nil { + return "err" + } + + if err := cmd.Start(); err != nil { + return "err" + } + + var str string + if b, err := ioutil.ReadAll(stdout); err == nil { + str += string(b) + } + + return str +} diff --git a/git/git.go b/git/git.go new file mode 100644 index 00000000..5c4f3ed1 --- /dev/null +++ b/git/git.go @@ -0,0 +1,16 @@ +package git + +import () + +func Fetch() map[string][]string { + client := NewClient() + + result := make(map[string][]string) + + result["repo"] = []string{client.Repository} + result["branch"] = []string{client.CurrentBranch()} + result["changed"] = client.ChangedFiles() + result["commits"] = client.Commits() + + return result +} diff --git a/git/widget.go b/git/widget.go index 63b7b125..ed1f6d29 100644 --- a/git/widget.go +++ b/git/widget.go @@ -1,7 +1,7 @@ package git import ( - //"fmt" + "fmt" "time" "github.com/rivo/tview" @@ -31,13 +31,13 @@ func NewWidget() *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - //data := Fetch() + data := Fetch() - widget.View.SetTitle(" 🙈 Git ") + widget.View.SetTitle(" 🤞 Git ") widget.RefreshedAt = time.Now() widget.View.Clear() - //fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) + fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) } /* -------------------- Unexported Functions -------------------- */ @@ -52,9 +52,10 @@ func (widget *Widget) addView() { widget.View = view } -func (widget *Widget) contentFrom(data map[string]string) string { +func (widget *Widget) contentFrom(data map[string][]string) string { str := "\n" - str = str + "This is git" + str = str + fmt.Sprintf(" [green]%s[white] [grey]%s[white]\n", data["repo"][0], data["branch"][0]) + str = str + "\n" return str } diff --git a/security/firewall.go b/security/firewall.go index 384746f5..bcbcdaf7 100644 --- a/security/firewall.go +++ b/security/firewall.go @@ -8,36 +8,32 @@ import ( const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw" +/* -------------------- Exported Functions -------------------- */ + func FirewallState() string { cmd := exec.Command(osxFirewallCmd, "--getglobalstate") + str := executeCommand(cmd) - stdout, err := cmd.StdoutPipe() - if err != nil { - return firewallIcon("err") - } - - if err := cmd.Start(); err != nil { - return firewallIcon("err") - } - - var str string - if b, err := ioutil.ReadAll(stdout); err == nil { - str += string(b) - } - - return firewallIcon(str) + return str } func FirewallStealthState() string { cmd := exec.Command(osxFirewallCmd, "--getstealthmode") + str := executeCommand(cmd) + return str +} + +/* -------------------- Unexported Functions -------------------- */ + +func executeCommand(cmd *exec.Cmd) string { stdout, err := cmd.StdoutPipe() if err != nil { - return firewallIcon("err") + return firewallStr("err") } if err := cmd.Start(); err != nil { - return firewallIcon("err") + return firewallStr("err") } var str string @@ -45,10 +41,10 @@ func FirewallStealthState() string { str += string(b) } - return firewallIcon(str) + return firewallStr(str) } -func firewallIcon(str string) string { +func firewallStr(str string) string { icon := "[red]off[white]" if strings.Contains(str, "enabled") {