Skip to content

Commit 4667fc1

Browse files
committed
"added ability to output curl command to multiple files"
1 parent b065cc9 commit 4667fc1

3 files changed

Lines changed: 103 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
release
33
.vscode
44
.idea
5+
.history
56

67
# Created by https://www.gitignore.io
78

cmd/curl.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ var (
3535
if err != nil {
3636
return err
3737
}
38-
return chkRunError(runCurl(c, args[0], curlHead, curlInsecure, curlHTTP2, from, nodeIDs, curlLimit))
38+
return chkRunError(runCurl(c, args[0], curlHead, curlInsecure, curlHTTP2, from, nodeIDs, curlLimit, fileOut))
3939
},
4040
}
4141

4242
curlHead bool
4343
curlInsecure bool
4444
curlHTTP2 bool
4545
curlLimit int
46+
fileOut string
4647
)
4748

4849
func initCurlCmd(parentCmd *cobra.Command) {
@@ -52,11 +53,13 @@ func initCurlCmd(parentCmd *cobra.Command) {
5253
curlCmd.Flags().BoolVarP(&curlInsecure, "insecure", "k", false, "Allow curl to proceed for server connections considered insecure")
5354
curlCmd.Flags().BoolVarP(&curlHTTP2, "http2", "", false, "Use HTTP version 2")
5455
curlCmd.Flags().IntVarP(&curlLimit, "limit", "L", 1, "The maximum number of nodes to use")
56+
curlCmd.Flags().StringVarP(&fileOut, "file", "f", "", "output to file")
5557

5658
parentCmd.AddCommand(curlCmd)
5759
}
5860

59-
func runCurl(c *perfops.Client, target string, head, insecure, http2 bool, from string, nodeIDs []int, limit int) error {
61+
func runCurl(c *perfops.Client, target string, head, insecure, http2 bool, from string, nodeIDs []int, limit int, fileOut string) error {
62+
6063
ctx := context.Background()
6164
curlReq := &perfops.CurlRequest{
6265
Target: target,
@@ -100,6 +103,7 @@ func runCurl(c *perfops.Client, target string, head, insecure, http2 bool, from
100103
if o, err = res.Output(); err != nil {
101104
return err
102105
}
106+
103107
if !outputJSON && o != nil {
104108
f.StopSpinner()
105109
internal.PrintOutput(f, o)
@@ -108,6 +112,10 @@ func runCurl(c *perfops.Client, target string, head, insecure, http2 bool, from
108112
break
109113
}
110114
}
115+
if len(fileOut) > 0 {
116+
f.StopSpinner()
117+
internal.OutputToFile(f, o, fileOut)
118+
}
111119
if outputJSON {
112120
f.StopSpinner()
113121
internal.PrintOutputJSON(o)

cmd/internal/runtest.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
package internal
1515

1616
import (
17+
"bufio"
1718
"bytes"
1819
"context"
1920
"encoding/json"
2021
"fmt"
2122
"io"
23+
"os"
2224
"strings"
2325
"sync"
2426
"time"
@@ -110,6 +112,93 @@ func RunTest(ctx context.Context, target, location string, nodeIDs []int, limit
110112
return nil
111113
}
112114

115+
func formatFileName(name string, index int) string {
116+
split := strings.Split(name, ".")
117+
118+
n := split[len(split)-2]
119+
split[len(split)-2] = fmt.Sprintf("%v-%x", n, index+1)
120+
121+
return strings.Join(split, ".")
122+
123+
}
124+
125+
// Outputs perfops response to a file
126+
func OutputToFile(f *Formatter, output *perfops.RunOutput, fileOut string) {
127+
if f.printID {
128+
f.Printf("Test ID: %v\n", output.ID)
129+
}
130+
spinner := f.s.Step()
131+
if !output.IsFinished() {
132+
f.Printf("%s", spinner)
133+
if len(output.Items) > 1 {
134+
finished := 0
135+
for _, item := range output.Items {
136+
if item.Result.IsFinished() {
137+
finished++
138+
}
139+
}
140+
f.Printf(" %d/%d", finished, len(output.Items))
141+
}
142+
f.Printf("\n")
143+
}
144+
for i, item := range output.Items {
145+
r := item.Result
146+
n := r.Node
147+
if item.Result.Message == "" {
148+
o := r.Output
149+
if o == "-2" {
150+
o = "The command timed-out. It either took too long to execute or we could not connect to your target at all."
151+
} else if a, ok := o.([]interface{}); ok {
152+
sb := strings.Builder{}
153+
for _, i := range a {
154+
sb.WriteString(fmt.Sprintf("%s\n", i))
155+
}
156+
s := sb.String()
157+
o = s[:len(s)-1]
158+
}
159+
160+
fileName := ""
161+
162+
if len(output.Items) > 1 {
163+
fileName = formatFileName(fileOut, i)
164+
} else {
165+
fileName = fileOut
166+
}
167+
168+
file, _ := os.Create(fileName)
169+
defer file.Close()
170+
171+
w := bufio.NewWriter(file)
172+
173+
fmt.Fprintf(w, "Node%d, AS%d, %s, %s\n%s\n", n.ID, n.AsNumber, n.City, n.Country.Name, o)
174+
175+
w.Flush()
176+
177+
} else if r.Message != "NO DATA" {
178+
fileName := ""
179+
180+
if len(output.Items) > 1 {
181+
fileName = formatFileName(fileOut, i)
182+
} else {
183+
fileName = fileOut
184+
}
185+
186+
file, _ := os.Create(fileName)
187+
defer file.Close()
188+
189+
w := bufio.NewWriter(file)
190+
191+
fmt.Fprintf(w, "Node%d, AS%d, %s, %s\n%s\n", n.ID, n.AsNumber, n.City, n.Country.Name, r.Message)
192+
193+
w.Flush()
194+
}
195+
if !item.Result.IsFinished() {
196+
f.Printf("%s\n", spinner)
197+
}
198+
}
199+
f.Flush(!output.IsFinished())
200+
}
201+
113202
// PrintOutput prints run items that have been data.
114203
func PrintOutput(f *Formatter, output *perfops.RunOutput) {
115204
if f.printID {
@@ -144,6 +233,9 @@ func PrintOutput(f *Formatter, output *perfops.RunOutput) {
144233
s := sb.String()
145234
o = s[:len(s)-1]
146235
}
236+
if len(output.Items) > 1 {
237+
fmt.Println("Greater Than One")
238+
}
147239
f.Printf("Node%d, AS%d, %s, %s\n%s\n", n.ID, n.AsNumber, n.City, n.Country.Name, o)
148240
} else if r.Message != "NO DATA" {
149241
f.Printf("Node%d, AS%d, %s, %s\n%s\n", n.ID, n.AsNumber, n.City, n.Country.Name, r.Message)

0 commit comments

Comments
 (0)