Skip to content

Commit 5d087b9

Browse files
cristianciuteafryckbos
authored andcommitted
added support for inserting data to cli stdin (#11)
* added support for inserting data to cli stdin * cleanup data insert documentation
1 parent 84f3420 commit 5d087b9

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/coscale/command/data.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package command
22

33
import (
4+
"bufio"
45
"coscale/api"
6+
"fmt"
57
"os"
68
)
79

@@ -81,12 +83,12 @@ Optional:
8183
},
8284
{
8385
Name: "insert",
84-
UsageLine: `data insert (--data)`,
86+
UsageLine: `data insert (--data <data> | --stdin)`,
8587
Long: `
8688
Insert data for metrics into the datastore.
8789
8890
The flags for data insert action are:
89-
Mandatory:
91+
Optional:
9092
--data
9193
To send data for DOUBLE metric data typ use the following format:
9294
"M<metric id>:S<subject Id>:<time>:<value/s>"
@@ -110,28 +112,33 @@ Mandatory:
110112
we want to show the total number of queued messages, but we also want to be able
111113
to split these into the number of queued messages per queue.
112114
eg: --data='M1:S1:-60:1.3:{"Queue":"q1","Data Center":"data center 1"};M2:S1:-60:1.2'
113-
114-
115-
Deprecated:
116-
--datapoint
117-
To send data for DOUBLE metric data type use the following format:
118-
"M<metric id>:S<subject Id>:<seconds ago>:<value/s>"
119-
eg: --datapoint="M1:S100:120:1.2
120-
121-
To send data for HISTOGRAM metric data type use the following format:
122-
"M<metric id>:S<subject Id>:<seconds ago>:[<no of samples>,<percentile width>,[<percentile data>]]"
123-
eg: --datapoint="M1:S1:60:[100,50,[1,2,3,4,5,6]]"
115+
--stdin
116+
Specify if the data will be interted on stdin. [default: false]
124117
`,
125118
Run: func(cmd *Command, args []string) {
126-
var datapoint string
127-
var data string
119+
var err error
120+
var datapoint, data string
121+
var stdin bool
128122

129123
cmd.Flag.Usage = func() { cmd.PrintUsage() }
130124

131125
cmd.Flag.StringVar(&datapoint, "datapoint", DEFAULT_STRING_FLAG_VALUE, "")
132126
cmd.Flag.StringVar(&data, "data", DEFAULT_STRING_FLAG_VALUE, "")
127+
cmd.Flag.BoolVar(&stdin, "stdin", false, "Specify if the data will be interted on stdin.")
133128
cmd.ParseArgs(args)
134129

130+
if stdin {
131+
message := fmt.Sprintf("%s\n\nPlease insert the data followed by a new line to submit...\n\n", cmd.Long)
132+
fmt.Fprintln(os.Stdout, message)
133+
134+
in := bufio.NewReader(os.Stdin)
135+
data, err = in.ReadString('\n')
136+
137+
if err != nil {
138+
cmd.PrintResult("", err)
139+
}
140+
}
141+
135142
if datapoint == DEFAULT_STRING_FLAG_VALUE && data == DEFAULT_STRING_FLAG_VALUE {
136143
cmd.PrintUsage()
137144
os.Exit(EXIT_FLAG_ERROR)

0 commit comments

Comments
 (0)