Skip to content

Commit 074ece5

Browse files
richiMarchimauriciovasquezbernal
authored andcommitted
polycubectl: add reading from standard input capability
This feature allows read the contents of a command from a file, for instance: polycubectl helloworld add hw0 < hw0_conf.json Signed-off-by: Riccardo Marchi <riccardo.marchi4@gmail.com>
1 parent fbf9bc9 commit 074ece5

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/polycubectl/aliases.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package main
1818

1919
import (
2020
"fmt"
21-
//"reflect"
2221
"github.com/polycube-network/polycube/src/polycubectl/cliargs"
2322
)
2423

src/polycubectl/cliargs/cliargs.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
"strconv"
2222
"strings"
2323

24+
"os"
25+
"bufio"
26+
"bytes"
27+
"github.com/ghodss/yaml"
28+
2429
"github.com/polycube-network/polycube/src/polycubectl/config"
2530
"github.com/polycube-network/polycube/src/polycubectl/httprequest"
2631

@@ -441,7 +446,25 @@ func (cli *CLIArgs) GetHTTPRequest() (*httprequest.HTTPRequest, error) {
441446
url += url0
442447
body = body0
443448
} else {
444-
body = cli.buildBody()
449+
// if there is text on the standard input that'll used as the body
450+
var buffer bytes.Buffer
451+
reader := bufio.NewReader(os.Stdin)
452+
text, _ := reader.ReadString('\n')
453+
for text != "" {
454+
buffer.WriteString(text)
455+
text, _ = reader.ReadString('\n')
456+
}
457+
458+
if buffer.Len() > 0 {
459+
jsonBuff, err := yaml.YAMLToJSON(buffer.Bytes())
460+
if err != nil {
461+
return nil, err
462+
} else {
463+
body = jsonBuff
464+
}
465+
} else {
466+
body = cli.buildBody()
467+
}
445468
}
446469
}
447470

0 commit comments

Comments
 (0)