Leverage ioutil.ReadAll() and ioutil.ReadFile() to simplify logic

Related to: #46
This commit is contained in:
pancho horrillo
2019-10-23 17:55:20 +02:00
parent 7674725658
commit ae3bd76434
+7 -14
View File
@@ -16,7 +16,7 @@
package cmd package cmd
import ( import (
"bytes" "io/ioutil"
"log" "log"
"os" "os"
@@ -58,24 +58,17 @@ func init() {
if len(args) > 1 && command == "" { if len(args) > 1 && command == "" {
commandFile := args[1] commandFile := args[1]
buf := new(bytes.Buffer) var buf []byte
var err error
if commandFile == "-" { if commandFile == "-" {
_, err := buf.ReadFrom(os.Stdin) buf, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
} else { } else {
file, err := os.Open(commandFile) buf, err = ioutil.ReadFile(commandFile)
}
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer file.Close() command = string(buf)
_, err = buf.ReadFrom(file)
if err != nil {
log.Fatal(err)
}
}
command = buf.String()
} }
if err := client.AddRoute(controlURL, urlPattern, method, entrypoint, command, os.Stdout); err != nil { if err := client.AddRoute(controlURL, urlPattern, method, entrypoint, command, os.Stdout); err != nil {