mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Optimized ParseJson and ExecuteCommand
This commit is contained in:
parent
5b4a49d11f
commit
cdf37f053c
@ -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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user