From ae3bd76434299ce40ad37dc51921363e90de6ea4 Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Wed, 23 Oct 2019 17:55:20 +0200 Subject: [PATCH] Leverage ioutil.ReadAll() and ioutil.ReadFile() to simplify logic Related to: #46 --- internal/cmd/route.go | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/internal/cmd/route.go b/internal/cmd/route.go index 4c5c877..51d9499 100644 --- a/internal/cmd/route.go +++ b/internal/cmd/route.go @@ -16,7 +16,7 @@ package cmd import ( - "bytes" + "io/ioutil" "log" "os" @@ -58,24 +58,17 @@ func init() { if len(args) > 1 && command == "" { commandFile := args[1] - buf := new(bytes.Buffer) + var buf []byte + var err error if commandFile == "-" { - _, err := buf.ReadFrom(os.Stdin) - if err != nil { - log.Fatal(err) - } + buf, err = ioutil.ReadAll(os.Stdin) } else { - file, err := os.Open(commandFile) - if err != nil { - log.Fatal(err) - } - defer file.Close() - _, err = buf.ReadFrom(file) - if err != nil { - log.Fatal(err) - } + buf, err = ioutil.ReadFile(commandFile) } - command = buf.String() + if err != nil { + log.Fatal(err) + } + command = string(buf) } if err := client.AddRoute(controlURL, urlPattern, method, entrypoint, command, os.Stdout); err != nil {