|
14 | 14 | package internal |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "bufio" |
17 | 18 | "bytes" |
18 | 19 | "context" |
19 | 20 | "encoding/json" |
20 | 21 | "fmt" |
21 | 22 | "io" |
| 23 | + "os" |
22 | 24 | "strings" |
23 | 25 | "sync" |
24 | 26 | "time" |
@@ -110,6 +112,93 @@ func RunTest(ctx context.Context, target, location string, nodeIDs []int, limit |
110 | 112 | return nil |
111 | 113 | } |
112 | 114 |
|
| 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 | + |
113 | 202 | // PrintOutput prints run items that have been data. |
114 | 203 | func PrintOutput(f *Formatter, output *perfops.RunOutput) { |
115 | 204 | if f.printID { |
@@ -144,6 +233,9 @@ func PrintOutput(f *Formatter, output *perfops.RunOutput) { |
144 | 233 | s := sb.String() |
145 | 234 | o = s[:len(s)-1] |
146 | 235 | } |
| 236 | + if len(output.Items) > 1 { |
| 237 | + fmt.Println("Greater Than One") |
| 238 | + } |
147 | 239 | f.Printf("Node%d, AS%d, %s, %s\n%s\n", n.ID, n.AsNumber, n.City, n.Country.Name, o) |
148 | 240 | } else if r.Message != "NO DATA" { |
149 | 241 | 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