From ca18a14d43c147257e66c701b535bf914928696f Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Fri, 11 Oct 2019 23:13:18 +0200 Subject: [PATCH 1/6] Preallocate slices and remove double type assertion --- utils/conversions.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/utils/conversions.go b/utils/conversions.go index f8b1542b..133ddb5d 100644 --- a/utils/conversions.go +++ b/utils/conversions.go @@ -8,7 +8,7 @@ import ( // 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) + results := make(map[string]string, len(aMap)) for key, val := range aMap { 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 func ToInts(slice []interface{}) []int { - results := []int{} + results := make([]int, len(slice)) - for _, val := range slice { - results = append(results, val.(int)) + for i, val := range slice { + results[i] = val.(int) } return results @@ -32,14 +32,14 @@ func ToInts(slice []interface{}) []int { // ToStrs takes a slice of interfaces and returns a slice of strings func ToStrs(slice []interface{}) []string { - results := []string{} + results := make([]string, len(slice)) - for _, val := range slice { - switch val.(type) { + for i, val := range slice { + switch t := val.(type) { case int: - results = append(results, strconv.Itoa(val.(int))) + results[i] = strconv.Itoa(t) case string: - results = append(results, val.(string)) + results[i] = t } } From 5b4a49d11f1d6ffb1e19bd2f72d6ae4b796efd73 Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Fri, 11 Oct 2019 23:13:38 +0200 Subject: [PATCH 2/6] Preallocate slice --- utils/email_addresses.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/email_addresses.go b/utils/email_addresses.go index e6273f63..e1ca846f 100644 --- a/utils/email_addresses.go +++ b/utils/email_addresses.go @@ -25,10 +25,10 @@ func NameFromEmail(email string) string { // > []string{"Test_user", "Other_user"} // func NamesFromEmails(emails []string) []string { - names := []string{} + names := make([]string, len(emails)) - for _, email := range emails { - names = append(names, NameFromEmail(email)) + for i, email := range emails { + names[i] = NameFromEmail(email) } return names From cdf37f053c63800bec9f4fecebf79e2fa0049354 Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Fri, 11 Oct 2019 23:17:01 +0200 Subject: [PATCH 3/6] Optimized ParseJson and ExecuteCommand --- utils/utils.go | 42 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index d5bf07a9..50983e86 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,7 +3,6 @@ package utils import ( "bytes" "encoding/json" - "fmt" "io" "io/ioutil" "os/exec" @@ -48,26 +47,14 @@ func ExecuteCommand(cmd *exec.Cmd) string { return "" } - stdout, err := cmd.StdoutPipe() - if err != nil { - return fmt.Sprintf("%v\n", err) + buf := &bytes.Buffer{} + cmd.Stdout = buf + + if err := cmd.Run(); err != nil { + return err.Error() } - if err := cmd.Start(); err != nil { - 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 + return buf.String() } // 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 func ParseJson(obj interface{}, text io.Reader) error { - jsonStream, err := ioutil.ReadAll(text) - if err != nil { - 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 + d := json.NewDecoder(text) + return d.Decode(obj) } // CalculateDimensions reads the module dimensions from the module and global config. The border is already substracted. From d3b6bc503a583bd2d841a9fba439b9f7e4056ff0 Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Fri, 11 Oct 2019 23:18:08 +0200 Subject: [PATCH 4/6] Call the os.UserHomeDir in Home --- utils/homedir.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/utils/homedir.go b/utils/homedir.go index 9a67a7b0..5a5d0fd2 100644 --- a/utils/homedir.go +++ b/utils/homedir.go @@ -6,7 +6,7 @@ package utils import ( "errors" - "os/user" + "os" "path/filepath" ) @@ -37,14 +37,5 @@ func ExpandHomeDir(path string) (string, error) { // Home returns the home directory for the executing user. // An error is returned if a home directory cannot be detected. func Home() (string, error) { - currentUser, err := user.Current() - if err != nil { - return "", err - } - - if currentUser.HomeDir == "" { - return "", errors.New("cannot find user-specific home dir") - } - - return currentUser.HomeDir, nil + return os.UserHomeDir() } From 270daf31ea007147e1b04770e501688472750a7e Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Fri, 11 Oct 2019 23:36:21 +0200 Subject: [PATCH 5/6] Revert "Call the os.UserHomeDir in Home" This reverts commit d3b6bc503a583bd2d841a9fba439b9f7e4056ff0. --- utils/homedir.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/utils/homedir.go b/utils/homedir.go index 5a5d0fd2..9a67a7b0 100644 --- a/utils/homedir.go +++ b/utils/homedir.go @@ -6,7 +6,7 @@ package utils import ( "errors" - "os" + "os/user" "path/filepath" ) @@ -37,5 +37,14 @@ func ExpandHomeDir(path string) (string, error) { // Home returns the home directory for the executing user. // An error is returned if a home directory cannot be detected. func Home() (string, error) { - return os.UserHomeDir() + currentUser, err := user.Current() + if err != nil { + return "", err + } + + if currentUser.HomeDir == "" { + return "", errors.New("cannot find user-specific home dir") + } + + return currentUser.HomeDir, nil } From 226796099c2588a56960613aa216c1eac87d0f24 Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Tue, 15 Oct 2019 11:09:03 +0200 Subject: [PATCH 6/6] Revert "Revert "Call the os.UserHomeDir in Home"" This reverts commit 270daf31ea007147e1b04770e501688472750a7e. --- utils/homedir.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/utils/homedir.go b/utils/homedir.go index 9a67a7b0..5a5d0fd2 100644 --- a/utils/homedir.go +++ b/utils/homedir.go @@ -6,7 +6,7 @@ package utils import ( "errors" - "os/user" + "os" "path/filepath" ) @@ -37,14 +37,5 @@ func ExpandHomeDir(path string) (string, error) { // Home returns the home directory for the executing user. // An error is returned if a home directory cannot be detected. func Home() (string, error) { - currentUser, err := user.Current() - if err != nil { - return "", err - } - - if currentUser.HomeDir == "" { - return "", errors.New("cannot find user-specific home dir") - } - - return currentUser.HomeDir, nil + return os.UserHomeDir() }