Skip to content

Commit c7229d5

Browse files
authored
Merge pull request #134 from polycube-network/pr/fix_polycubectl
polycubectl: fix reading from standard input
2 parents ddf6883 + 694e7e2 commit c7229d5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/polycubectl/cliargs/cliargs.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ func (cli *CLIArgs) GetHTTPRequest() (*httprequest.HTTPRequest, error) {
436436
var url string
437437

438438
url = cli.buildURL()
439+
var stat os.FileInfo
440+
var staterr error
441+
if os.Stdin != nil {
442+
stat, staterr = os.Stdin.Stat()
443+
}
439444

440445
// TODO: is command == "" required?
441446
if !cli.IsHelp && (cli.Command == AddCommand ||
@@ -445,8 +450,10 @@ func (cli *CLIArgs) GetHTTPRequest() (*httprequest.HTTPRequest, error) {
445450
url0, body0 := cli.buildSingleParamBody()
446451
url += url0
447452
body = body0
448-
} else {
449-
// if there is text on the standard input that'll used as the body
453+
} else if os.Stdin != nil && staterr == nil &&
454+
stat.Mode() & os.ModeCharDevice == 0 && stat.Size() > 0 {
455+
// os.ModeCharDevice flag is set when piping text to the stdin
456+
// use text in the standard input as body
450457
var buffer bytes.Buffer
451458
reader := bufio.NewReader(os.Stdin)
452459
text, _ := reader.ReadString('\n')
@@ -465,6 +472,8 @@ func (cli *CLIArgs) GetHTTPRequest() (*httprequest.HTTPRequest, error) {
465472
} else {
466473
body = cli.buildBody()
467474
}
475+
} else {
476+
body = cli.buildBody()
468477
}
469478
}
470479

0 commit comments

Comments
 (0)