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

ExecuteCommand() moved into the utils dumping ground

This commit is contained in:
Chris Cummer 2018-04-02 08:36:43 -07:00 committed by Chris Cummer
parent de7480b221
commit 0b8a063487
3 changed files with 12 additions and 50 deletions

View File

@ -59,7 +59,6 @@ func (widget *Widget) contentFrom(items []Item) string {
} }
str := "" str := ""
for _, item := range items { for _, item := range items {
str = str + widget.display(item) str = str + widget.display(item)
} }

View File

@ -1,10 +1,10 @@
package git package git
import ( import (
//"fmt"
"io/ioutil"
"os/exec" "os/exec"
"strings" "strings"
"github.com/senorprogrammer/wtf/wtf"
) )
type Client struct { type Client struct {
@ -26,7 +26,7 @@ func NewClient() *Client {
func (client *Client) CurrentBranch() string { func (client *Client) CurrentBranch() string {
arg := []string{"rev-parse", "--abbrev-ref", "HEAD"} arg := []string{"rev-parse", "--abbrev-ref", "HEAD"}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := executeCommand(cmd) str := wtf.ExecuteCommand(cmd)
return str return str
} }
@ -34,7 +34,7 @@ func (client *Client) CurrentBranch() string {
func (client *Client) ChangedFiles() []string { func (client *Client) ChangedFiles() []string {
arg := []string{"status", "--porcelain"} arg := []string{"status", "--porcelain"}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := executeCommand(cmd) str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n") data := strings.Split(str, "\n")
@ -44,29 +44,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]\""} arg := []string{"log", "--date=format:\"%b %d, %Y\"", "-n 10", "--pretty=format:\"[forestgreen]%h [white]%s [grey]%an on %cd[white]\""}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := executeCommand(cmd) str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n") data := strings.Split(str, "\n")
return data return data
} }
/* -------------------- 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
}

View File

@ -1,9 +1,10 @@
package security package security
import ( import (
"io/ioutil"
"os/exec" "os/exec"
"strings" "strings"
"github.com/senorprogrammer/wtf/wtf"
) )
const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw" const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw"
@ -12,39 +13,21 @@ const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw"
func FirewallState() string { func FirewallState() string {
cmd := exec.Command(osxFirewallCmd, "--getglobalstate") cmd := exec.Command(osxFirewallCmd, "--getglobalstate")
str := executeCommand(cmd) str := wtf.ExecuteCommand(cmd)
return str return status(str)
} }
func FirewallStealthState() string { func FirewallStealthState() string {
cmd := exec.Command(osxFirewallCmd, "--getstealthmode") cmd := exec.Command(osxFirewallCmd, "--getstealthmode")
str := executeCommand(cmd) str := wtf.ExecuteCommand(cmd)
return str return status(str)
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func executeCommand(cmd *exec.Cmd) string { func status(str string) string {
stdout, err := cmd.StdoutPipe()
if err != nil {
return firewallStr("err")
}
if err := cmd.Start(); err != nil {
return firewallStr("err")
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += string(b)
}
return firewallStr(str)
}
func firewallStr(str string) string {
icon := "[red]off[white]" icon := "[red]off[white]"
if strings.Contains(str, "enabled") { if strings.Contains(str, "enabled") {