-
-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into githubmultisource
- Loading branch information
Showing
8 changed files
with
106 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ builds: | |
goos: | ||
- darwin | ||
- linux | ||
- windows | ||
goarch: | ||
- 386 | ||
- amd64 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"os/exec" | ||
"regexp" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/wtfutil/wtf/utils" | ||
|
@@ -21,6 +22,14 @@ const TimestampFormat = "2006-01-02T15:04:05-0700" | |
|
||
var OpenFileUtil = "open" | ||
|
||
// CenterText takes a string and a width and pads the left and right of the string with | ||
// empty spaces to ensure that the string is in the middle of the returned value | ||
// | ||
// Example: | ||
// | ||
// x := CenterText("cat", 11) | ||
// > " cat " | ||
// | ||
func CenterText(str string, width int) string { | ||
if width < 0 { | ||
width = 0 | ||
|
@@ -29,6 +38,7 @@ func CenterText(str string, width int) string { | |
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str)) | ||
} | ||
|
||
// ExecuteCommand executes an external command on the local machine as the current user | ||
func ExecuteCommand(cmd *exec.Cmd) string { | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
|
@@ -52,6 +62,14 @@ func ExecuteCommand(cmd *exec.Cmd) string { | |
return str | ||
} | ||
|
||
// Exclude takes a slice of strings and a target string and returns the contents of the original | ||
// slice of strings without the target string in it | ||
// | ||
// Example: | ||
// | ||
// x := Exclude([]string{"cat", "dog", "rat"}, "dog") | ||
// > []string{"cat", "rat"} | ||
// | ||
func Exclude(strs []string, val string) bool { | ||
for _, str := range strs { | ||
if val == str { | ||
|
@@ -61,16 +79,33 @@ func Exclude(strs []string, val string) bool { | |
return true | ||
} | ||
|
||
// FindMatch takes a regex pattern and a string of data and returns back all the matches | ||
// in that string | ||
func FindMatch(pattern string, data string) [][]string { | ||
r := regexp.MustCompile(pattern) | ||
return r.FindAllStringSubmatch(data, -1) | ||
} | ||
|
||
// NameFromEmail takes an email address and returns the part that comes before the @ symbol | ||
// | ||
// Example: | ||
// | ||
// NameFromEmail("[email protected]") | ||
// > "Test_user" | ||
// | ||
func NameFromEmail(email string) string { | ||
parts := strings.Split(email, "@") | ||
return strings.Title(strings.Replace(parts[0], ".", " ", -1)) | ||
} | ||
|
||
// NamesFromEmails takes a slice of email addresses and returns a slice of the parts that | ||
// come before the @ symbol | ||
// | ||
// Example: | ||
// | ||
// NamesFromEmail("[email protected]", "[email protected]") | ||
// > []string{"Test_user", "Other_user"} | ||
// | ||
func NamesFromEmails(emails []string) []string { | ||
names := []string{} | ||
|
||
|
@@ -112,6 +147,7 @@ func PadRow(offset int, max int) string { | |
return strings.Repeat(" ", padSize) | ||
} | ||
|
||
// ReadFileBytes reads the contents of a file and returns those contents as a slice of bytes | ||
func ReadFileBytes(filePath string) ([]byte, error) { | ||
fileData, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
|
@@ -123,6 +159,7 @@ func ReadFileBytes(filePath string) ([]byte, error) { | |
|
||
/* -------------------- Map Conversion -------------------- */ | ||
|
||
// MapToStrs takes a map of interfaces and returns a map of strings | ||
func MapToStrs(aMap map[string]interface{}) map[string]string { | ||
results := make(map[string]string) | ||
|
||
|
@@ -135,6 +172,7 @@ func MapToStrs(aMap map[string]interface{}) map[string]string { | |
|
||
/* -------------------- Slice Conversion -------------------- */ | ||
|
||
// ToInts takes a slice of interfaces and returns a slice of ints | ||
func ToInts(slice []interface{}) []int { | ||
results := []int{} | ||
|
||
|
@@ -145,11 +183,17 @@ func ToInts(slice []interface{}) []int { | |
return results | ||
} | ||
|
||
// ToStrs takes a slice of interfaces and returns a slice of strings | ||
func ToStrs(slice []interface{}) []string { | ||
results := []string{} | ||
|
||
for _, val := range slice { | ||
results = append(results, val.(string)) | ||
switch val.(type) { | ||
case int: | ||
results = append(results, strconv.Itoa(val.(int))) | ||
case string: | ||
results = append(results, val.(string)) | ||
} | ||
} | ||
|
||
return results | ||
|