Sfoglia il codice sorgente

Add support for abbreviated commands on interative command like

Support commands like "top10" or "web5"
Raul Silvera 8 anni fa
parent
commit
fbc3c87ded
2 ha cambiato i file con 34 aggiunte e 2 eliminazioni
  1. 14
    1
      internal/driver/interactive.go
  2. 20
    1
      internal/driver/interactive_test.go

+ 14
- 1
internal/driver/interactive.go Vedi File

@@ -218,7 +218,20 @@ func parseCommandLine(input []string) ([]string, variables, error) {
218 218
 	cmd, args := input[:1], input[1:]
219 219
 	name := cmd[0]
220 220
 
221
-	c := pprofCommands[cmd[0]]
221
+	c := pprofCommands[name]
222
+	if c == nil {
223
+		// Attempt splitting digits on abbreviated commands (eg top10)
224
+		for i := len(name); i > 0; i-- {
225
+			if !strings.ContainsAny(name[i-1:i], "0123456789") {
226
+				if n, d := name[:i], name[i:]; n != "" && d != "" {
227
+					cmd[0], args = n, append([]string{d}, args...)
228
+					name = n
229
+					c = pprofCommands[n]
230
+				}
231
+				break
232
+			}
233
+		}
234
+	}
222 235
 	if c == nil {
223 236
 		return nil, nil, fmt.Errorf("Unrecognized command: %q", name)
224 237
 	}

+ 20
- 1
internal/driver/interactive_test.go Vedi File

@@ -247,6 +247,16 @@ func TestInteractiveCommands(t *testing.T) {
247 247
 				"ignore":    "ignore",
248 248
 			},
249 249
 		},
250
+		{
251
+			"top10 --cum focus1 -ignore focus2",
252
+			map[string]string{
253
+				"functions": "true",
254
+				"nodecount": "10",
255
+				"cum":       "true",
256
+				"focus":     "focus1|focus2",
257
+				"ignore":    "ignore",
258
+			},
259
+		},
250 260
 		{
251 261
 			"dot",
252 262
 			map[string]string{
@@ -288,14 +298,23 @@ func TestInteractiveCommands(t *testing.T) {
288 298
 				"output":    "out",
289 299
 			},
290 300
 		},
301
+		{
302
+			"999",
303
+			nil, // Error
304
+		},
291 305
 	}
292 306
 
293 307
 	for _, tc := range testcases {
294 308
 		cmd, vars, err := parseCommandLine(strings.Fields(tc.input))
295
-		vars = applyCommandOverrides(cmd, vars)
309
+		if tc.want == nil && err != nil {
310
+			// Error expected
311
+			continue
312
+		}
296 313
 		if err != nil {
297 314
 			t.Errorf("failed on %q: %v", tc.input, err)
315
+			continue
298 316
 		}
317
+		vars = applyCommandOverrides(cmd, vars)
299 318
 
300 319
 		for n, want := range tc.want {
301 320
 			if got := vars[n].stringValue(); got != want {