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

Merge branch 'master' of github.com:noxer/wtf into noxer-master

This commit is contained in:
Chris Cummer 2019-10-15 15:03:51 -04:00
commit b97cb8dd05
4 changed files with 22 additions and 57 deletions

View File

@ -8,7 +8,7 @@ import (
// MapToStrs takes a map of interfaces and returns a map of strings // MapToStrs takes a map of interfaces and returns a map of strings
func MapToStrs(aMap map[string]interface{}) map[string]string { func MapToStrs(aMap map[string]interface{}) map[string]string {
results := make(map[string]string) results := make(map[string]string, len(aMap))
for key, val := range aMap { for key, val := range aMap {
results[key] = val.(string) results[key] = val.(string)
@ -21,10 +21,10 @@ func MapToStrs(aMap map[string]interface{}) map[string]string {
// ToInts takes a slice of interfaces and returns a slice of ints // ToInts takes a slice of interfaces and returns a slice of ints
func ToInts(slice []interface{}) []int { func ToInts(slice []interface{}) []int {
results := []int{} results := make([]int, len(slice))
for _, val := range slice { for i, val := range slice {
results = append(results, val.(int)) results[i] = val.(int)
} }
return results return results
@ -32,14 +32,14 @@ func ToInts(slice []interface{}) []int {
// ToStrs takes a slice of interfaces and returns a slice of strings // ToStrs takes a slice of interfaces and returns a slice of strings
func ToStrs(slice []interface{}) []string { func ToStrs(slice []interface{}) []string {
results := []string{} results := make([]string, len(slice))
for _, val := range slice { for i, val := range slice {
switch val.(type) { switch t := val.(type) {
case int: case int:
results = append(results, strconv.Itoa(val.(int))) results[i] = strconv.Itoa(t)
case string: case string:
results = append(results, val.(string)) results[i] = t
} }
} }

View File

@ -25,10 +25,10 @@ func NameFromEmail(email string) string {
// > []string{"Test_user", "Other_user"} // > []string{"Test_user", "Other_user"}
// //
func NamesFromEmails(emails []string) []string { func NamesFromEmails(emails []string) []string {
names := []string{} names := make([]string, len(emails))
for _, email := range emails { for i, email := range emails {
names = append(names, NameFromEmail(email)) names[i] = NameFromEmail(email)
} }
return names return names

View File

@ -6,7 +6,7 @@ package utils
import ( import (
"errors" "errors"
"os/user" "os"
"path/filepath" "path/filepath"
) )
@ -37,14 +37,5 @@ func ExpandHomeDir(path string) (string, error) {
// Home returns the home directory for the executing user. // Home returns the home directory for the executing user.
// An error is returned if a home directory cannot be detected. // An error is returned if a home directory cannot be detected.
func Home() (string, error) { func Home() (string, error) {
currentUser, err := user.Current() return os.UserHomeDir()
if err != nil {
return "", err
}
if currentUser.HomeDir == "" {
return "", errors.New("cannot find user-specific home dir")
}
return currentUser.HomeDir, nil
} }

View File

@ -3,7 +3,6 @@ package utils
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"os/exec" "os/exec"
@ -48,26 +47,14 @@ func ExecuteCommand(cmd *exec.Cmd) string {
return "" return ""
} }
stdout, err := cmd.StdoutPipe() buf := &bytes.Buffer{}
if err != nil { cmd.Stdout = buf
return fmt.Sprintf("%v\n", err)
if err := cmd.Run(); err != nil {
return err.Error()
} }
if err := cmd.Start(); err != nil { return buf.String()
return fmt.Sprintf("%v\n", err)
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += string(b)
}
err = cmd.Wait()
if err != nil {
return fmt.Sprintf("%v\n", err)
}
return str
} }
// FindMatch takes a regex pattern and a string of data and returns back all the matches // FindMatch takes a regex pattern and a string of data and returns back all the matches
@ -130,21 +117,8 @@ func ReadFileBytes(filePath string) ([]byte, error) {
// ParseJson is a standard JSON reader from text // ParseJson is a standard JSON reader from text
func ParseJson(obj interface{}, text io.Reader) error { func ParseJson(obj interface{}, text io.Reader) error {
jsonStream, err := ioutil.ReadAll(text) d := json.NewDecoder(text)
if err != nil { return d.Decode(obj)
return err
}
decoder := json.NewDecoder(bytes.NewReader(jsonStream))
for {
if err := decoder.Decode(obj); err == io.EOF {
break
} else if err != nil {
return err
}
}
return nil
} }
// CalculateDimensions reads the module dimensions from the module and global config. The border is already substracted. // CalculateDimensions reads the module dimensions from the module and global config. The border is already substracted.