Skip to content

Commit

Permalink
Merge pull request #134 from polycube-network/pr/fix_polycubectl
Browse files Browse the repository at this point in the history
polycubectl: fix reading from standard input
  • Loading branch information
frisso committed May 28, 2019
2 parents ddf6883 + 694e7e2 commit c7229d5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/polycubectl/cliargs/cliargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ func (cli *CLIArgs) GetHTTPRequest() (*httprequest.HTTPRequest, error) {
var url string

url = cli.buildURL()
var stat os.FileInfo
var staterr error
if os.Stdin != nil {
stat, staterr = os.Stdin.Stat()
}

// TODO: is command == "" required?
if !cli.IsHelp && (cli.Command == AddCommand ||
Expand All @@ -445,8 +450,10 @@ func (cli *CLIArgs) GetHTTPRequest() (*httprequest.HTTPRequest, error) {
url0, body0 := cli.buildSingleParamBody()
url += url0
body = body0
} else {
// if there is text on the standard input that'll used as the body
} else if os.Stdin != nil && staterr == nil &&
stat.Mode() & os.ModeCharDevice == 0 && stat.Size() > 0 {
// os.ModeCharDevice flag is set when piping text to the stdin
// use text in the standard input as body
var buffer bytes.Buffer
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
Expand All @@ -465,6 +472,8 @@ func (cli *CLIArgs) GetHTTPRequest() (*httprequest.HTTPRequest, error) {
} else {
body = cli.buildBody()
}
} else {
body = cli.buildBody()
}
}

Expand Down

0 comments on commit c7229d5

Please sign in to comment.