-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
64 lines (59 loc) · 1.65 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"github.com/spf13/cobra"
"os"
"strconv"
)
//flags
var Csv string
var Source string
var Run = &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("You need to supply 3 arguments, [config file] [csv file to process] [rate in milliseconds]")
}
if len(args) < 2 {
return fmt.Errorf("You need to supply a path to a csv file")
}
if len(args) < 3 {
return fmt.Errorf("You need to supply a rate in Milliseconds denoting how fast you want to hit the endpoint")
}
return nil
},
Use: "run /path/to/config.json /path/to/file.csv int rate",
Short: "run http post to all rows in csv file",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
configtouse, err := LoadConfig(args[0])
rate, err := strconv.Atoi(args[2])
if err != nil {
return fmt.Errorf("could not convert string to int for rate")
}
csvfile := args[1]
fmt.Println(csvfile)
runningConfig = configtouse
if err != nil {
cmd.Println(err)
}
processCsv(configtouse, csvfile, rate)
return nil
},
}
func createConfig() *cobra.Command {
myCommand := &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "create-config /path/to/file",
Short: "generates boilerplate config file for you",
Long: "Will create the boilerplate config file for you, supply a filename to generate",
RunE: func(cmd *cobra.Command, args []string) error {
f, err := os.OpenFile(args[0], os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close()
if _, err = f.WriteString(configfiletoecho); err != nil {
panic(err)
}
return nil
},
}
return myCommand
}