Best Gauge code snippet using cmd.execute
custom_completions_test.go
Source:custom_completions_test.go
...59 Run: emptyRun,60 }61 rootCmd.AddCommand(childCmd1, childCmd2, hiddenCmd, deprecatedCmd, aliasedCmd)62 // Test that sub-command names are completed63 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")64 if err != nil {65 t.Errorf("Unexpected error: %v", err)66 }67 expected := strings.Join([]string{68 "aliased",69 "firstChild",70 "help",71 "secondChild",72 ":4",73 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")74 if output != expected {75 t.Errorf("expected: %q, got: %q", expected, output)76 }77 // Test that sub-command names are completed with prefix78 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "s")79 if err != nil {80 t.Errorf("Unexpected error: %v", err)81 }82 expected = strings.Join([]string{83 "secondChild",84 ":4",85 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")86 if output != expected {87 t.Errorf("expected: %q, got: %q", expected, output)88 }89 // Test that even with no valid sub-command matches, hidden, deprecated and90 // aliases are not completed91 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "test")92 if err != nil {93 t.Errorf("Unexpected error: %v", err)94 }95 expected = strings.Join([]string{96 ":4",97 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")98 if output != expected {99 t.Errorf("expected: %q, got: %q", expected, output)100 }101 // Test that sub-command names are completed with description102 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "")103 if err != nil {104 t.Errorf("Unexpected error: %v", err)105 }106 expected = strings.Join([]string{107 "aliased\tA command with aliases",108 "firstChild\tFirst command",109 "help\tHelp about any command",110 "secondChild",111 ":4",112 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")113 if output != expected {114 t.Errorf("expected: %q, got: %q", expected, output)115 }116}117func TestNoCmdNameCompletionInGo(t *testing.T) {118 rootCmd := &Command{119 Use: "root",120 Run: emptyRun,121 }122 rootCmd.Flags().String("localroot", "", "local root flag")123 childCmd1 := &Command{124 Use: "childCmd1",125 Short: "First command",126 Args: MinimumNArgs(0),127 Run: emptyRun,128 }129 rootCmd.AddCommand(childCmd1)130 childCmd1.PersistentFlags().StringP("persistent", "p", "", "persistent flag")131 persistentFlag := childCmd1.PersistentFlags().Lookup("persistent")132 childCmd1.Flags().StringP("nonPersistent", "n", "", "non-persistent flag")133 nonPersistentFlag := childCmd1.Flags().Lookup("nonPersistent")134 childCmd2 := &Command{135 Use: "childCmd2",136 Run: emptyRun,137 }138 childCmd1.AddCommand(childCmd2)139 // Test that sub-command names are not completed if there is an argument already140 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "arg1", "")141 if err != nil {142 t.Errorf("Unexpected error: %v", err)143 }144 expected := strings.Join([]string{145 ":0",146 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")147 if output != expected {148 t.Errorf("expected: %q, got: %q", expected, output)149 }150 // Test that sub-command names are not completed if a local non-persistent flag is present151 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--nonPersistent", "value", "")152 if err != nil {153 t.Errorf("Unexpected error: %v", err)154 }155 // Reset the flag for the next command156 nonPersistentFlag.Changed = false157 expected = strings.Join([]string{158 ":0",159 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")160 if output != expected {161 t.Errorf("expected: %q, got: %q", expected, output)162 }163 // Test that sub-command names are completed if a local non-persistent flag is present and TraverseChildren is set to true164 // set TraverseChildren to true on the root cmd165 rootCmd.TraverseChildren = true166 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "")167 if err != nil {168 t.Errorf("Unexpected error: %v", err)169 }170 // Reset TraverseChildren for next command171 rootCmd.TraverseChildren = false172 expected = strings.Join([]string{173 "childCmd1",174 "help",175 ":4",176 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")177 if output != expected {178 t.Errorf("expected: %q, got: %q", expected, output)179 }180 // Test that sub-command names from a child cmd are completed if a local non-persistent flag is present181 // and TraverseChildren is set to true on the root cmd182 rootCmd.TraverseChildren = true183 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "--nonPersistent", "value", "")184 if err != nil {185 t.Errorf("Unexpected error: %v", err)186 }187 // Reset TraverseChildren for next command188 rootCmd.TraverseChildren = false189 // Reset the flag for the next command190 nonPersistentFlag.Changed = false191 expected = strings.Join([]string{192 "childCmd2",193 ":4",194 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")195 if output != expected {196 t.Errorf("expected: %q, got: %q", expected, output)197 }198 // Test that we don't use Traverse when we shouldn't.199 // This command should not return a completion since the command line is invalid without TraverseChildren.200 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "")201 if err != nil {202 t.Errorf("Unexpected error: %v", err)203 }204 expected = strings.Join([]string{205 ":0",206 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")207 if output != expected {208 t.Errorf("expected: %q, got: %q", expected, output)209 }210 // Test that sub-command names are not completed if a local non-persistent short flag is present211 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "-n", "value", "")212 if err != nil {213 t.Errorf("Unexpected error: %v", err)214 }215 // Reset the flag for the next command216 nonPersistentFlag.Changed = false217 expected = strings.Join([]string{218 ":0",219 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")220 if output != expected {221 t.Errorf("expected: %q, got: %q", expected, output)222 }223 // Test that sub-command names are completed with a persistent flag224 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--persistent", "value", "")225 if err != nil {226 t.Errorf("Unexpected error: %v", err)227 }228 // Reset the flag for the next command229 persistentFlag.Changed = false230 expected = strings.Join([]string{231 "childCmd2",232 ":4",233 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")234 if output != expected {235 t.Errorf("expected: %q, got: %q", expected, output)236 }237 // Test that sub-command names are completed with a persistent short flag238 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "-p", "value", "")239 if err != nil {240 t.Errorf("Unexpected error: %v", err)241 }242 // Reset the flag for the next command243 persistentFlag.Changed = false244 expected = strings.Join([]string{245 "childCmd2",246 ":4",247 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")248 if output != expected {249 t.Errorf("expected: %q, got: %q", expected, output)250 }251}252func TestValidArgsCompletionInGo(t *testing.T) {253 rootCmd := &Command{254 Use: "root",255 ValidArgs: []string{"one", "two", "three"},256 Args: MinimumNArgs(1),257 }258 // Test that validArgs are completed259 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")260 if err != nil {261 t.Errorf("Unexpected error: %v", err)262 }263 expected := strings.Join([]string{264 "one",265 "two",266 "three",267 ":4",268 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")269 if output != expected {270 t.Errorf("expected: %q, got: %q", expected, output)271 }272 // Test that validArgs are completed with prefix273 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "o")274 if err != nil {275 t.Errorf("Unexpected error: %v", err)276 }277 expected = strings.Join([]string{278 "one",279 ":4",280 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")281 if output != expected {282 t.Errorf("expected: %q, got: %q", expected, output)283 }284 // Test that validArgs don't repeat285 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "one", "")286 if err != nil {287 t.Errorf("Unexpected error: %v", err)288 }289 expected = strings.Join([]string{290 ":0",291 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")292 if output != expected {293 t.Errorf("expected: %q, got: %q", expected, output)294 }295}296func TestValidArgsAndCmdCompletionInGo(t *testing.T) {297 rootCmd := &Command{298 Use: "root",299 ValidArgs: []string{"one", "two"},300 Run: emptyRun,301 }302 childCmd := &Command{303 Use: "thechild",304 Run: emptyRun,305 }306 rootCmd.AddCommand(childCmd)307 // Test that both sub-commands and validArgs are completed308 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")309 if err != nil {310 t.Errorf("Unexpected error: %v", err)311 }312 expected := strings.Join([]string{313 "help",314 "thechild",315 "one",316 "two",317 ":4",318 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")319 if output != expected {320 t.Errorf("expected: %q, got: %q", expected, output)321 }322 // Test that both sub-commands and validArgs are completed with prefix323 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")324 if err != nil {325 t.Errorf("Unexpected error: %v", err)326 }327 expected = strings.Join([]string{328 "thechild",329 "two",330 ":4",331 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")332 if output != expected {333 t.Errorf("expected: %q, got: %q", expected, output)334 }335}336func TestValidArgsFuncAndCmdCompletionInGo(t *testing.T) {337 rootCmd := &Command{338 Use: "root",339 ValidArgsFunction: validArgsFunc,340 Run: emptyRun,341 }342 childCmd := &Command{343 Use: "thechild",344 Short: "The child command",345 Run: emptyRun,346 }347 rootCmd.AddCommand(childCmd)348 // Test that both sub-commands and validArgsFunction are completed349 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")350 if err != nil {351 t.Errorf("Unexpected error: %v", err)352 }353 expected := strings.Join([]string{354 "help",355 "thechild",356 "one",357 "two",358 ":0",359 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")360 if output != expected {361 t.Errorf("expected: %q, got: %q", expected, output)362 }363 // Test that both sub-commands and validArgs are completed with prefix364 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")365 if err != nil {366 t.Errorf("Unexpected error: %v", err)367 }368 expected = strings.Join([]string{369 "thechild",370 "two",371 ":0",372 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")373 if output != expected {374 t.Errorf("expected: %q, got: %q", expected, output)375 }376 // Test that both sub-commands and validArgs are completed with description377 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "t")378 if err != nil {379 t.Errorf("Unexpected error: %v", err)380 }381 expected = strings.Join([]string{382 "thechild\tThe child command",383 "two\tThe second",384 ":0",385 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")386 if output != expected {387 t.Errorf("expected: %q, got: %q", expected, output)388 }389}390func TestFlagNameCompletionInGo(t *testing.T) {391 rootCmd := &Command{392 Use: "root",393 Run: emptyRun,394 }395 childCmd := &Command{396 Use: "childCmd",397 Run: emptyRun,398 }399 rootCmd.AddCommand(childCmd)400 rootCmd.Flags().IntP("first", "f", -1, "first flag")401 rootCmd.PersistentFlags().BoolP("second", "s", false, "second flag")402 childCmd.Flags().String("subFlag", "", "sub flag")403 // Test that flag names are not shown if the user has not given the '-' prefix404 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")405 if err != nil {406 t.Errorf("Unexpected error: %v", err)407 }408 expected := strings.Join([]string{409 "childCmd",410 "help",411 ":4",412 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")413 if output != expected {414 t.Errorf("expected: %q, got: %q", expected, output)415 }416 // Test that flag names are completed417 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-")418 if err != nil {419 t.Errorf("Unexpected error: %v", err)420 }421 expected = strings.Join([]string{422 "--first",423 "-f",424 "--second",425 "-s",426 ":4",427 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")428 if output != expected {429 t.Errorf("expected: %q, got: %q", expected, output)430 }431 // Test that flag names are completed when a prefix is given432 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--f")433 if err != nil {434 t.Errorf("Unexpected error: %v", err)435 }436 expected = strings.Join([]string{437 "--first",438 ":4",439 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")440 if output != expected {441 t.Errorf("expected: %q, got: %q", expected, output)442 }443 // Test that flag names are completed in a sub-cmd444 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "-")445 if err != nil {446 t.Errorf("Unexpected error: %v", err)447 }448 expected = strings.Join([]string{449 "--second",450 "-s",451 "--subFlag",452 ":4",453 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")454 if output != expected {455 t.Errorf("expected: %q, got: %q", expected, output)456 }457}458func TestFlagNameCompletionInGoWithDesc(t *testing.T) {459 rootCmd := &Command{460 Use: "root",461 Run: emptyRun,462 }463 childCmd := &Command{464 Use: "childCmd",465 Short: "first command",466 Run: emptyRun,467 }468 rootCmd.AddCommand(childCmd)469 rootCmd.Flags().IntP("first", "f", -1, "first flag\nlonger description for flag")470 rootCmd.PersistentFlags().BoolP("second", "s", false, "second flag")471 childCmd.Flags().String("subFlag", "", "sub flag")472 // Test that flag names are not shown if the user has not given the '-' prefix473 output, err := executeCommand(rootCmd, ShellCompRequestCmd, "")474 if err != nil {475 t.Errorf("Unexpected error: %v", err)476 }477 expected := strings.Join([]string{478 "childCmd\tfirst command",479 "help\tHelp about any command",480 ":4",481 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")482 if output != expected {483 t.Errorf("expected: %q, got: %q", expected, output)484 }485 // Test that flag names are completed486 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "-")487 if err != nil {488 t.Errorf("Unexpected error: %v", err)489 }490 expected = strings.Join([]string{491 "--first\tfirst flag",492 "-f\tfirst flag",493 "--second\tsecond flag",494 "-s\tsecond flag",495 ":4",496 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")497 if output != expected {498 t.Errorf("expected: %q, got: %q", expected, output)499 }500 // Test that flag names are completed when a prefix is given501 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--f")502 if err != nil {503 t.Errorf("Unexpected error: %v", err)504 }505 expected = strings.Join([]string{506 "--first\tfirst flag",507 ":4",508 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")509 if output != expected {510 t.Errorf("expected: %q, got: %q", expected, output)511 }512 // Test that flag names are completed in a sub-cmd513 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "childCmd", "-")514 if err != nil {515 t.Errorf("Unexpected error: %v", err)516 }517 expected = strings.Join([]string{518 "--second\tsecond flag",519 "-s\tsecond flag",520 "--subFlag\tsub flag",521 ":4",522 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")523 if output != expected {524 t.Errorf("expected: %q, got: %q", expected, output)525 }526}527func TestFlagNameCompletionRepeat(t *testing.T) {528 rootCmd := &Command{529 Use: "root",530 Run: emptyRun,531 }532 childCmd := &Command{533 Use: "childCmd",534 Short: "first command",535 Run: emptyRun,536 }537 rootCmd.AddCommand(childCmd)538 rootCmd.Flags().IntP("first", "f", -1, "first flag")539 firstFlag := rootCmd.Flags().Lookup("first")540 rootCmd.Flags().BoolP("second", "s", false, "second flag")541 secondFlag := rootCmd.Flags().Lookup("second")542 rootCmd.Flags().StringArrayP("array", "a", nil, "array flag")543 arrayFlag := rootCmd.Flags().Lookup("array")544 rootCmd.Flags().IntSliceP("slice", "l", nil, "slice flag")545 sliceFlag := rootCmd.Flags().Lookup("slice")546 rootCmd.Flags().BoolSliceP("bslice", "b", nil, "bool slice flag")547 bsliceFlag := rootCmd.Flags().Lookup("bslice")548 // Test that flag names are not repeated unless they are an array or slice549 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--")550 if err != nil {551 t.Errorf("Unexpected error: %v", err)552 }553 // Reset the flag for the next command554 firstFlag.Changed = false555 expected := strings.Join([]string{556 "--array",557 "--bslice",558 "--second",559 "--slice",560 ":4",561 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")562 if output != expected {563 t.Errorf("expected: %q, got: %q", expected, output)564 }565 // Test that flag names are not repeated unless they are an array or slice566 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--second=false", "--")567 if err != nil {568 t.Errorf("Unexpected error: %v", err)569 }570 // Reset the flag for the next command571 firstFlag.Changed = false572 secondFlag.Changed = false573 expected = strings.Join([]string{574 "--array",575 "--bslice",576 "--slice",577 ":4",578 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")579 if output != expected {580 t.Errorf("expected: %q, got: %q", expected, output)581 }582 // Test that flag names are not repeated unless they are an array or slice583 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--slice", "1", "--slice=2", "--array", "val", "--bslice", "true", "--")584 if err != nil {585 t.Errorf("Unexpected error: %v", err)586 }587 // Reset the flag for the next command588 sliceFlag.Changed = false589 arrayFlag.Changed = false590 bsliceFlag.Changed = false591 expected = strings.Join([]string{592 "--array",593 "--bslice",594 "--first",595 "--second",596 "--slice",597 ":4",598 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")599 if output != expected {600 t.Errorf("expected: %q, got: %q", expected, output)601 }602 // Test that flag names are not repeated unless they are an array or slice, using shortname603 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-a", "val", "-")604 if err != nil {605 t.Errorf("Unexpected error: %v", err)606 }607 // Reset the flag for the next command608 sliceFlag.Changed = false609 arrayFlag.Changed = false610 expected = strings.Join([]string{611 "--array",612 "-a",613 "--bslice",614 "-b",615 "--first",616 "-f",617 "--second",618 "-s",619 "--slice",620 "-l",621 ":4",622 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")623 if output != expected {624 t.Errorf("expected: %q, got: %q", expected, output)625 }626 // Test that flag names are not repeated unless they are an array or slice, using shortname with prefix627 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-a", "val", "-a")628 if err != nil {629 t.Errorf("Unexpected error: %v", err)630 }631 // Reset the flag for the next command632 sliceFlag.Changed = false633 arrayFlag.Changed = false634 expected = strings.Join([]string{635 "-a",636 ":4",637 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")638 if output != expected {639 t.Errorf("expected: %q, got: %q", expected, output)640 }641}642func TestRequiredFlagNameCompletionInGo(t *testing.T) {643 rootCmd := &Command{644 Use: "root",645 ValidArgs: []string{"realArg"},646 Run: emptyRun,647 }648 childCmd := &Command{649 Use: "childCmd",650 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {651 return []string{"subArg"}, ShellCompDirectiveNoFileComp652 },653 Run: emptyRun,654 }655 rootCmd.AddCommand(childCmd)656 rootCmd.Flags().IntP("requiredFlag", "r", -1, "required flag")657 assertNoErr(t, rootCmd.MarkFlagRequired("requiredFlag"))658 requiredFlag := rootCmd.Flags().Lookup("requiredFlag")659 rootCmd.PersistentFlags().IntP("requiredPersistent", "p", -1, "required persistent")660 assertNoErr(t, rootCmd.MarkPersistentFlagRequired("requiredPersistent"))661 requiredPersistent := rootCmd.PersistentFlags().Lookup("requiredPersistent")662 rootCmd.Flags().StringP("release", "R", "", "Release name")663 childCmd.Flags().BoolP("subRequired", "s", false, "sub required flag")664 assertNoErr(t, childCmd.MarkFlagRequired("subRequired"))665 childCmd.Flags().BoolP("subNotRequired", "n", false, "sub not required flag")666 // Test that a required flag is suggested even without the - prefix667 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")668 if err != nil {669 t.Errorf("Unexpected error: %v", err)670 }671 expected := strings.Join([]string{672 "childCmd",673 "help",674 "--requiredFlag",675 "-r",676 "--requiredPersistent",677 "-p",678 "realArg",679 ":4",680 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")681 if output != expected {682 t.Errorf("expected: %q, got: %q", expected, output)683 }684 // Test that a required flag is suggested without other flags when using the '-' prefix685 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-")686 if err != nil {687 t.Errorf("Unexpected error: %v", err)688 }689 expected = strings.Join([]string{690 "--requiredFlag",691 "-r",692 "--requiredPersistent",693 "-p",694 ":4",695 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")696 if output != expected {697 t.Errorf("expected: %q, got: %q", expected, output)698 }699 // Test that if no required flag matches, the normal flags are suggested700 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--relea")701 if err != nil {702 t.Errorf("Unexpected error: %v", err)703 }704 expected = strings.Join([]string{705 "--release",706 ":4",707 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")708 if output != expected {709 t.Errorf("expected: %q, got: %q", expected, output)710 }711 // Test required flags for sub-commands712 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "")713 if err != nil {714 t.Errorf("Unexpected error: %v", err)715 }716 expected = strings.Join([]string{717 "--requiredPersistent",718 "-p",719 "--subRequired",720 "-s",721 "subArg",722 ":4",723 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")724 if output != expected {725 t.Errorf("expected: %q, got: %q", expected, output)726 }727 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "-")728 if err != nil {729 t.Errorf("Unexpected error: %v", err)730 }731 expected = strings.Join([]string{732 "--requiredPersistent",733 "-p",734 "--subRequired",735 "-s",736 ":4",737 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")738 if output != expected {739 t.Errorf("expected: %q, got: %q", expected, output)740 }741 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "--subNot")742 if err != nil {743 t.Errorf("Unexpected error: %v", err)744 }745 expected = strings.Join([]string{746 "--subNotRequired",747 ":4",748 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")749 if output != expected {750 t.Errorf("expected: %q, got: %q", expected, output)751 }752 // Test that when a required flag is present, it is not suggested anymore753 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredFlag", "1", "")754 if err != nil {755 t.Errorf("Unexpected error: %v", err)756 }757 // Reset the flag for the next command758 requiredFlag.Changed = false759 expected = strings.Join([]string{760 "--requiredPersistent",761 "-p",762 "realArg",763 ":4",764 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")765 if output != expected {766 t.Errorf("expected: %q, got: %q", expected, output)767 }768 // Test that when a persistent required flag is present, it is not suggested anymore769 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredPersistent", "1", "")770 if err != nil {771 t.Errorf("Unexpected error: %v", err)772 }773 // Reset the flag for the next command774 requiredPersistent.Changed = false775 expected = strings.Join([]string{776 "childCmd",777 "help",778 "--requiredFlag",779 "-r",780 "realArg",781 ":4",782 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")783 if output != expected {784 t.Errorf("expected: %q, got: %q", expected, output)785 }786 // Test that when all required flags are present, normal completion is done787 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredFlag", "1", "--requiredPersistent", "1", "")788 if err != nil {789 t.Errorf("Unexpected error: %v", err)790 }791 // Reset the flags for the next command792 requiredFlag.Changed = false793 requiredPersistent.Changed = false794 expected = strings.Join([]string{795 "realArg",796 ":4",797 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")798 if output != expected {799 t.Errorf("expected: %q, got: %q", expected, output)800 }801}802func TestFlagFileExtFilterCompletionInGo(t *testing.T) {803 rootCmd := &Command{804 Use: "root",805 Run: emptyRun,806 }807 // No extensions. Should be ignored.808 rootCmd.Flags().StringP("file", "f", "", "file flag")809 assertNoErr(t, rootCmd.MarkFlagFilename("file"))810 // Single extension811 rootCmd.Flags().StringP("log", "l", "", "log flag")812 assertNoErr(t, rootCmd.MarkFlagFilename("log", "log"))813 // Multiple extensions814 rootCmd.Flags().StringP("yaml", "y", "", "yaml flag")815 assertNoErr(t, rootCmd.MarkFlagFilename("yaml", "yaml", "yml"))816 // Directly using annotation817 rootCmd.Flags().StringP("text", "t", "", "text flag")818 assertNoErr(t, rootCmd.Flags().SetAnnotation("text", BashCompFilenameExt, []string{"txt"}))819 // Test that the completion logic returns the proper info for the completion820 // script to handle the file filtering821 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--file", "")822 if err != nil {823 t.Errorf("Unexpected error: %v", err)824 }825 expected := strings.Join([]string{826 ":0",827 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")828 if output != expected {829 t.Errorf("expected: %q, got: %q", expected, output)830 }831 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--log", "")832 if err != nil {833 t.Errorf("Unexpected error: %v", err)834 }835 expected = strings.Join([]string{836 "log",837 ":8",838 "Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")839 if output != expected {840 t.Errorf("expected: %q, got: %q", expected, output)841 }842 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--yaml", "")843 if err != nil {844 t.Errorf("Unexpected error: %v", err)845 }846 expected = strings.Join([]string{847 "yaml", "yml",848 ":8",849 "Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")850 if output != expected {851 t.Errorf("expected: %q, got: %q", expected, output)852 }853 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--yaml=")854 if err != nil {855 t.Errorf("Unexpected error: %v", err)856 }857 expected = strings.Join([]string{858 "yaml", "yml",859 ":8",860 "Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")861 if output != expected {862 t.Errorf("expected: %q, got: %q", expected, output)863 }864 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-y", "")865 if err != nil {866 t.Errorf("Unexpected error: %v", err)867 }868 expected = strings.Join([]string{869 "yaml", "yml",870 ":8",871 "Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")872 if output != expected {873 t.Errorf("expected: %q, got: %q", expected, output)874 }875 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-y=")876 if err != nil {877 t.Errorf("Unexpected error: %v", err)878 }879 expected = strings.Join([]string{880 "yaml", "yml",881 ":8",882 "Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")883 if output != expected {884 t.Errorf("expected: %q, got: %q", expected, output)885 }886 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--text", "")887 if err != nil {888 t.Errorf("Unexpected error: %v", err)889 }890 expected = strings.Join([]string{891 "txt",892 ":8",893 "Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")894 if output != expected {895 t.Errorf("expected: %q, got: %q", expected, output)896 }897}898func TestFlagDirFilterCompletionInGo(t *testing.T) {899 rootCmd := &Command{900 Use: "root",901 Run: emptyRun,902 }903 // Filter directories904 rootCmd.Flags().StringP("dir", "d", "", "dir flag")905 assertNoErr(t, rootCmd.MarkFlagDirname("dir"))906 // Filter directories within a directory907 rootCmd.Flags().StringP("subdir", "s", "", "subdir")908 assertNoErr(t, rootCmd.Flags().SetAnnotation("subdir", BashCompSubdirsInDir, []string{"themes"}))909 // Multiple directory specification get ignored910 rootCmd.Flags().StringP("manydir", "m", "", "manydir")911 assertNoErr(t, rootCmd.Flags().SetAnnotation("manydir", BashCompSubdirsInDir, []string{"themes", "colors"}))912 // Test that the completion logic returns the proper info for the completion913 // script to handle the directory filtering914 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--dir", "")915 if err != nil {916 t.Errorf("Unexpected error: %v", err)917 }918 expected := strings.Join([]string{919 ":16",920 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")921 if output != expected {922 t.Errorf("expected: %q, got: %q", expected, output)923 }924 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-d", "")925 if err != nil {926 t.Errorf("Unexpected error: %v", err)927 }928 expected = strings.Join([]string{929 ":16",930 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")931 if output != expected {932 t.Errorf("expected: %q, got: %q", expected, output)933 }934 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--subdir", "")935 if err != nil {936 t.Errorf("Unexpected error: %v", err)937 }938 expected = strings.Join([]string{939 "themes",940 ":16",941 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")942 if output != expected {943 t.Errorf("expected: %q, got: %q", expected, output)944 }945 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--subdir=")946 if err != nil {947 t.Errorf("Unexpected error: %v", err)948 }949 expected = strings.Join([]string{950 "themes",951 ":16",952 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")953 if output != expected {954 t.Errorf("expected: %q, got: %q", expected, output)955 }956 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "")957 if err != nil {958 t.Errorf("Unexpected error: %v", err)959 }960 expected = strings.Join([]string{961 "themes",962 ":16",963 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")964 if output != expected {965 t.Errorf("expected: %q, got: %q", expected, output)966 }967 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s=")968 if err != nil {969 t.Errorf("Unexpected error: %v", err)970 }971 expected = strings.Join([]string{972 "themes",973 ":16",974 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")975 if output != expected {976 t.Errorf("expected: %q, got: %q", expected, output)977 }978 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--manydir", "")979 if err != nil {980 t.Errorf("Unexpected error: %v", err)981 }982 expected = strings.Join([]string{983 ":16",984 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")985 if output != expected {986 t.Errorf("expected: %q, got: %q", expected, output)987 }988}989func TestValidArgsFuncSingleCmd(t *testing.T) {990 rootCmd := &Command{991 Use: "root",992 ValidArgsFunction: validArgsFunc,993 Run: emptyRun,994 }995 // Test completing an empty string996 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")997 if err != nil {998 t.Errorf("Unexpected error: %v", err)999 }1000 expected := strings.Join([]string{1001 "one",1002 "two",1003 ":0",1004 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1005 if output != expected {1006 t.Errorf("expected: %q, got: %q", expected, output)1007 }1008 // Check completing with a prefix1009 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")1010 if err != nil {1011 t.Errorf("Unexpected error: %v", err)1012 }1013 expected = strings.Join([]string{1014 "two",1015 ":0",1016 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1017 if output != expected {1018 t.Errorf("expected: %q, got: %q", expected, output)1019 }1020}1021func TestValidArgsFuncSingleCmdInvalidArg(t *testing.T) {1022 rootCmd := &Command{1023 Use: "root",1024 // If we don't specify a value for Args, this test fails.1025 // This is only true for a root command without any subcommands, and is caused1026 // by the fact that the __complete command becomes a subcommand when there should not be one.1027 // The problem is in the implementation of legacyArgs().1028 Args: MinimumNArgs(1),1029 ValidArgsFunction: validArgsFunc,1030 Run: emptyRun,1031 }1032 // Check completing with wrong number of args1033 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "unexpectedArg", "t")1034 if err != nil {1035 t.Errorf("Unexpected error: %v", err)1036 }1037 expected := strings.Join([]string{1038 ":4",1039 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1040 if output != expected {1041 t.Errorf("expected: %q, got: %q", expected, output)1042 }1043}1044func TestValidArgsFuncChildCmds(t *testing.T) {1045 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1046 child1Cmd := &Command{1047 Use: "child1",1048 ValidArgsFunction: validArgsFunc,1049 Run: emptyRun,1050 }1051 child2Cmd := &Command{1052 Use: "child2",1053 ValidArgsFunction: validArgsFunc2,1054 Run: emptyRun,1055 }1056 rootCmd.AddCommand(child1Cmd, child2Cmd)1057 // Test completion of first sub-command with empty argument1058 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "")1059 if err != nil {1060 t.Errorf("Unexpected error: %v", err)1061 }1062 expected := strings.Join([]string{1063 "one",1064 "two",1065 ":0",1066 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1067 if output != expected {1068 t.Errorf("expected: %q, got: %q", expected, output)1069 }1070 // Test completion of first sub-command with a prefix to complete1071 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "t")1072 if err != nil {1073 t.Errorf("Unexpected error: %v", err)1074 }1075 expected = strings.Join([]string{1076 "two",1077 ":0",1078 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1079 if output != expected {1080 t.Errorf("expected: %q, got: %q", expected, output)1081 }1082 // Check completing with wrong number of args1083 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "unexpectedArg", "t")1084 if err != nil {1085 t.Errorf("Unexpected error: %v", err)1086 }1087 expected = strings.Join([]string{1088 ":4",1089 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1090 if output != expected {1091 t.Errorf("expected: %q, got: %q", expected, output)1092 }1093 // Test completion of second sub-command with empty argument1094 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "")1095 if err != nil {1096 t.Errorf("Unexpected error: %v", err)1097 }1098 expected = strings.Join([]string{1099 "three",1100 "four",1101 ":0",1102 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1103 if output != expected {1104 t.Errorf("expected: %q, got: %q", expected, output)1105 }1106 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "t")1107 if err != nil {1108 t.Errorf("Unexpected error: %v", err)1109 }1110 expected = strings.Join([]string{1111 "three",1112 ":0",1113 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1114 if output != expected {1115 t.Errorf("expected: %q, got: %q", expected, output)1116 }1117 // Check completing with wrong number of args1118 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "unexpectedArg", "t")1119 if err != nil {1120 t.Errorf("Unexpected error: %v", err)1121 }1122 expected = strings.Join([]string{1123 ":4",1124 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1125 if output != expected {1126 t.Errorf("expected: %q, got: %q", expected, output)1127 }1128}1129func TestValidArgsFuncAliases(t *testing.T) {1130 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1131 child := &Command{1132 Use: "child",1133 Aliases: []string{"son", "daughter"},1134 ValidArgsFunction: validArgsFunc,1135 Run: emptyRun,1136 }1137 rootCmd.AddCommand(child)1138 // Test completion of first sub-command with empty argument1139 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "son", "")1140 if err != nil {1141 t.Errorf("Unexpected error: %v", err)1142 }1143 expected := strings.Join([]string{1144 "one",1145 "two",1146 ":0",1147 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1148 if output != expected {1149 t.Errorf("expected: %q, got: %q", expected, output)1150 }1151 // Test completion of first sub-command with a prefix to complete1152 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "daughter", "t")1153 if err != nil {1154 t.Errorf("Unexpected error: %v", err)1155 }1156 expected = strings.Join([]string{1157 "two",1158 ":0",1159 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1160 if output != expected {1161 t.Errorf("expected: %q, got: %q", expected, output)1162 }1163 // Check completing with wrong number of args1164 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "son", "unexpectedArg", "t")1165 if err != nil {1166 t.Errorf("Unexpected error: %v", err)1167 }1168 expected = strings.Join([]string{1169 ":4",1170 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1171 if output != expected {1172 t.Errorf("expected: %q, got: %q", expected, output)1173 }1174}1175func TestValidArgsFuncInBashScript(t *testing.T) {1176 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1177 child := &Command{1178 Use: "child",1179 ValidArgsFunction: validArgsFunc,1180 Run: emptyRun,1181 }1182 rootCmd.AddCommand(child)1183 buf := new(bytes.Buffer)1184 assertNoErr(t, rootCmd.GenBashCompletion(buf))1185 output := buf.String()1186 check(t, output, "has_completion_function=1")1187}1188func TestNoValidArgsFuncInBashScript(t *testing.T) {1189 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1190 child := &Command{1191 Use: "child",1192 Run: emptyRun,1193 }1194 rootCmd.AddCommand(child)1195 buf := new(bytes.Buffer)1196 assertNoErr(t, rootCmd.GenBashCompletion(buf))1197 output := buf.String()1198 checkOmit(t, output, "has_completion_function=1")1199}1200func TestCompleteCmdInBashScript(t *testing.T) {1201 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1202 child := &Command{1203 Use: "child",1204 ValidArgsFunction: validArgsFunc,1205 Run: emptyRun,1206 }1207 rootCmd.AddCommand(child)1208 buf := new(bytes.Buffer)1209 assertNoErr(t, rootCmd.GenBashCompletion(buf))1210 output := buf.String()1211 check(t, output, ShellCompNoDescRequestCmd)1212}1213func TestCompleteNoDesCmdInZshScript(t *testing.T) {1214 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1215 child := &Command{1216 Use: "child",1217 ValidArgsFunction: validArgsFunc,1218 Run: emptyRun,1219 }1220 rootCmd.AddCommand(child)1221 buf := new(bytes.Buffer)1222 assertNoErr(t, rootCmd.GenZshCompletionNoDesc(buf))1223 output := buf.String()1224 check(t, output, ShellCompNoDescRequestCmd)1225}1226func TestCompleteCmdInZshScript(t *testing.T) {1227 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1228 child := &Command{1229 Use: "child",1230 ValidArgsFunction: validArgsFunc,1231 Run: emptyRun,1232 }1233 rootCmd.AddCommand(child)1234 buf := new(bytes.Buffer)1235 assertNoErr(t, rootCmd.GenZshCompletion(buf))1236 output := buf.String()1237 check(t, output, ShellCompRequestCmd)1238 checkOmit(t, output, ShellCompNoDescRequestCmd)1239}1240func TestFlagCompletionInGo(t *testing.T) {1241 rootCmd := &Command{1242 Use: "root",1243 Run: emptyRun,1244 }1245 rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")1246 assertNoErr(t, rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1247 completions := []string{}1248 for _, comp := range []string{"1\tThe first", "2\tThe second", "10\tThe tenth"} {1249 if strings.HasPrefix(comp, toComplete) {1250 completions = append(completions, comp)1251 }1252 }1253 return completions, ShellCompDirectiveDefault1254 }))1255 rootCmd.Flags().String("filename", "", "Enter a filename")1256 assertNoErr(t, rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1257 completions := []string{}1258 for _, comp := range []string{"file.yaml\tYAML format", "myfile.json\tJSON format", "file.xml\tXML format"} {1259 if strings.HasPrefix(comp, toComplete) {1260 completions = append(completions, comp)1261 }1262 }1263 return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp1264 }))1265 // Test completing an empty string1266 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "")1267 if err != nil {1268 t.Errorf("Unexpected error: %v", err)1269 }1270 expected := strings.Join([]string{1271 "1",1272 "2",1273 "10",1274 ":0",1275 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1276 if output != expected {1277 t.Errorf("expected: %q, got: %q", expected, output)1278 }1279 // Check completing with a prefix1280 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "1")1281 if err != nil {1282 t.Errorf("Unexpected error: %v", err)1283 }1284 expected = strings.Join([]string{1285 "1",1286 "10",1287 ":0",1288 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1289 if output != expected {1290 t.Errorf("expected: %q, got: %q", expected, output)1291 }1292 // Test completing an empty string1293 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--filename", "")1294 if err != nil {1295 t.Errorf("Unexpected error: %v", err)1296 }1297 expected = strings.Join([]string{1298 "file.yaml",1299 "myfile.json",1300 "file.xml",1301 ":6",1302 "Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")1303 if output != expected {1304 t.Errorf("expected: %q, got: %q", expected, output)1305 }1306 // Check completing with a prefix1307 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--filename", "f")1308 if err != nil {1309 t.Errorf("Unexpected error: %v", err)1310 }1311 expected = strings.Join([]string{1312 "file.yaml",1313 "file.xml",1314 ":6",1315 "Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")1316 if output != expected {1317 t.Errorf("expected: %q, got: %q", expected, output)1318 }1319}1320func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) {1321 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1322 child1Cmd := &Command{1323 Use: "child1",1324 ValidArgsFunction: validArgsFunc,1325 Run: emptyRun,1326 }1327 child2Cmd := &Command{1328 Use: "child2",1329 ValidArgsFunction: validArgsFunc2,1330 Run: emptyRun,1331 }1332 rootCmd.AddCommand(child1Cmd, child2Cmd)1333 // Test completion of first sub-command with empty argument1334 output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child1", "")1335 if err != nil {1336 t.Errorf("Unexpected error: %v", err)1337 }1338 expected := strings.Join([]string{1339 "one\tThe first",1340 "two\tThe second",1341 ":0",1342 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1343 if output != expected {1344 t.Errorf("expected: %q, got: %q", expected, output)1345 }1346 // Test completion of first sub-command with a prefix to complete1347 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "t")1348 if err != nil {1349 t.Errorf("Unexpected error: %v", err)1350 }1351 expected = strings.Join([]string{1352 "two\tThe second",1353 ":0",1354 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1355 if output != expected {1356 t.Errorf("expected: %q, got: %q", expected, output)1357 }1358 // Check completing with wrong number of args1359 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "unexpectedArg", "t")1360 if err != nil {1361 t.Errorf("Unexpected error: %v", err)1362 }1363 expected = strings.Join([]string{1364 ":4",1365 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1366 if output != expected {1367 t.Errorf("expected: %q, got: %q", expected, output)1368 }1369 // Test completion of second sub-command with empty argument1370 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "")1371 if err != nil {1372 t.Errorf("Unexpected error: %v", err)1373 }1374 expected = strings.Join([]string{1375 "three\tThe third",1376 "four\tThe fourth",1377 ":0",1378 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1379 if output != expected {1380 t.Errorf("expected: %q, got: %q", expected, output)1381 }1382 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "t")1383 if err != nil {1384 t.Errorf("Unexpected error: %v", err)1385 }1386 expected = strings.Join([]string{1387 "three\tThe third",1388 ":0",1389 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1390 if output != expected {1391 t.Errorf("expected: %q, got: %q", expected, output)1392 }1393 // Check completing with wrong number of args1394 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "unexpectedArg", "t")1395 if err != nil {1396 t.Errorf("Unexpected error: %v", err)1397 }1398 expected = strings.Join([]string{1399 ":4",1400 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1401 if output != expected {1402 t.Errorf("expected: %q, got: %q", expected, output)1403 }1404}1405func TestFlagCompletionInGoWithDesc(t *testing.T) {1406 rootCmd := &Command{1407 Use: "root",1408 Run: emptyRun,1409 }1410 rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")1411 assertNoErr(t, rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1412 completions := []string{}1413 for _, comp := range []string{"1\tThe first", "2\tThe second", "10\tThe tenth"} {1414 if strings.HasPrefix(comp, toComplete) {1415 completions = append(completions, comp)1416 }1417 }1418 return completions, ShellCompDirectiveDefault1419 }))1420 rootCmd.Flags().String("filename", "", "Enter a filename")1421 assertNoErr(t, rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1422 completions := []string{}1423 for _, comp := range []string{"file.yaml\tYAML format", "myfile.json\tJSON format", "file.xml\tXML format"} {1424 if strings.HasPrefix(comp, toComplete) {1425 completions = append(completions, comp)1426 }1427 }1428 return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp1429 }))1430 // Test completing an empty string1431 output, err := executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "")1432 if err != nil {1433 t.Errorf("Unexpected error: %v", err)1434 }1435 expected := strings.Join([]string{1436 "1\tThe first",1437 "2\tThe second",1438 "10\tThe tenth",1439 ":0",1440 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1441 if output != expected {1442 t.Errorf("expected: %q, got: %q", expected, output)1443 }1444 // Check completing with a prefix1445 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "1")1446 if err != nil {1447 t.Errorf("Unexpected error: %v", err)1448 }1449 expected = strings.Join([]string{1450 "1\tThe first",1451 "10\tThe tenth",1452 ":0",1453 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")1454 if output != expected {1455 t.Errorf("expected: %q, got: %q", expected, output)1456 }1457 // Test completing an empty string1458 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--filename", "")1459 if err != nil {1460 t.Errorf("Unexpected error: %v", err)1461 }1462 expected = strings.Join([]string{1463 "file.yaml\tYAML format",1464 "myfile.json\tJSON format",1465 "file.xml\tXML format",1466 ":6",1467 "Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")1468 if output != expected {1469 t.Errorf("expected: %q, got: %q", expected, output)1470 }1471 // Check completing with a prefix1472 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--filename", "f")1473 if err != nil {1474 t.Errorf("Unexpected error: %v", err)1475 }1476 expected = strings.Join([]string{1477 "file.yaml\tYAML format",1478 "file.xml\tXML format",1479 ":6",1480 "Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")1481 if output != expected {1482 t.Errorf("expected: %q, got: %q", expected, output)1483 }1484}1485func TestValidArgsNotValidArgsFunc(t *testing.T) {1486 rootCmd := &Command{1487 Use: "root",1488 ValidArgs: []string{"one", "two"},1489 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1490 return []string{"three", "four"}, ShellCompDirectiveNoFileComp1491 },1492 Run: emptyRun,1493 }1494 // Test that if both ValidArgs and ValidArgsFunction are present1495 // only ValidArgs is considered1496 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")1497 if err != nil {1498 t.Errorf("Unexpected error: %v", err)1499 }1500 expected := strings.Join([]string{1501 "one",1502 "two",1503 ":4",1504 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1505 if output != expected {1506 t.Errorf("expected: %q, got: %q", expected, output)1507 }1508 // Check completing with a prefix1509 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")1510 if err != nil {1511 t.Errorf("Unexpected error: %v", err)1512 }1513 expected = strings.Join([]string{1514 "two",1515 ":4",1516 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1517 if output != expected {1518 t.Errorf("expected: %q, got: %q", expected, output)1519 }1520}1521func TestArgAliasesCompletionInGo(t *testing.T) {1522 rootCmd := &Command{1523 Use: "root",1524 Args: OnlyValidArgs,1525 ValidArgs: []string{"one", "two", "three"},1526 ArgAliases: []string{"un", "deux", "trois"},1527 Run: emptyRun,1528 }1529 // Test that argaliases are not completed when there are validargs that match1530 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")1531 if err != nil {1532 t.Errorf("Unexpected error: %v", err)1533 }1534 expected := strings.Join([]string{1535 "one",1536 "two",1537 "three",1538 ":4",1539 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1540 if output != expected {1541 t.Errorf("expected: %q, got: %q", expected, output)1542 }1543 // Test that argaliases are not completed when there are validargs that match using a prefix1544 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")1545 if err != nil {1546 t.Errorf("Unexpected error: %v", err)1547 }1548 expected = strings.Join([]string{1549 "two",1550 "three",1551 ":4",1552 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1553 if output != expected {1554 t.Errorf("expected: %q, got: %q", expected, output)1555 }1556 // Test that argaliases are completed when there are no validargs that match1557 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "tr")1558 if err != nil {1559 t.Errorf("Unexpected error: %v", err)1560 }1561 expected = strings.Join([]string{1562 "trois",1563 ":4",1564 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1565 if output != expected {1566 t.Errorf("expected: %q, got: %q", expected, output)1567 }1568}1569func TestCompleteHelp(t *testing.T) {1570 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1571 child1Cmd := &Command{1572 Use: "child1",1573 Run: emptyRun,1574 }1575 child2Cmd := &Command{1576 Use: "child2",1577 Run: emptyRun,1578 }1579 rootCmd.AddCommand(child1Cmd, child2Cmd)1580 child3Cmd := &Command{1581 Use: "child3",1582 Run: emptyRun,1583 }1584 child1Cmd.AddCommand(child3Cmd)1585 // Test that completion includes the help command1586 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")1587 if err != nil {1588 t.Errorf("Unexpected error: %v", err)1589 }1590 expected := strings.Join([]string{1591 "child1",1592 "child2",1593 "help",1594 ":4",1595 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1596 if output != expected {1597 t.Errorf("expected: %q, got: %q", expected, output)1598 }1599 // Test sub-commands are completed on first level of help command1600 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "help", "")1601 if err != nil {1602 t.Errorf("Unexpected error: %v", err)1603 }1604 expected = strings.Join([]string{1605 "child1",1606 "child2",1607 "help", // "<program> help help" is a valid command, so should be completed1608 ":4",1609 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1610 if output != expected {1611 t.Errorf("expected: %q, got: %q", expected, output)1612 }1613 // Test sub-commands are completed on first level of help command1614 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "help", "child1", "")1615 if err != nil {1616 t.Errorf("Unexpected error: %v", err)1617 }1618 expected = strings.Join([]string{1619 "child3",1620 ":4",1621 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")1622 if output != expected {1623 t.Errorf("expected: %q, got: %q", expected, output)1624 }1625}...
cmd.go
Source:cmd.go
1/*2http://tools.ietf.org/html/rfc9593http://www.faqs.org/rfcs/rfc2389.html4http://www.faqs.org/rfcs/rfc959.html5http://tools.ietf.org/html/rfc24286*/7package server8import (9 "fmt"10 "strconv"11 "strings"12 "log"13)14type Command interface {15 IsExtend() bool16 RequireParam() bool17 RequireAuth() bool18 Execute(*Conn, string)19}20type commandMap map[string]Command21var (22 commands = commandMap{23 "ADAT": commandAdat{},24 "ALLO": commandAllo{},25 "APPE": commandAppe{},26 "AUTH": commandAuth{},27 "CDUP": commandCdup{},28 "CWD": commandCwd{},29 "CCC": commandCcc{},30 "CONF": commandConf{},31 "DELE": commandDele{},32 "ENC": commandEnc{},33 "EPRT": commandEprt{},34 "EPSV": commandEpsv{},35 "FEAT": commandFeat{},36 "LIST": commandList{},37 "NLST": commandNlst{},38 "MDTM": commandMdtm{},39 "MIC": commandMic{},40 "MKD": commandMkd{},41 "MODE": commandMode{},42 "NOOP": commandNoop{},43 "OPTS": commandOpts{},44 "PASS": commandPass{},45 "PASV": commandPasv{},46 "PBSZ": commandPbsz{},47 "PORT": commandPort{},48 "PROT": commandProt{},49 "PWD": commandPwd{},50 "QUIT": commandQuit{},51 "RETR": commandRetr{},52 "REST": commandRest{},53 "RNFR": commandRnfr{},54 "RNTO": commandRnto{},55 "RMD": commandRmd{},56 "SIZE": commandSize{},57 "STOR": commandStor{},58 "STRU": commandStru{},59 "SYST": commandSyst{},60 "TYPE": commandType{},61 "USER": commandUser{},62 "XCUP": commandCdup{},63 "XCWD": commandCwd{},64 "XPWD": commandPwd{},65 "XRMD": commandRmd{},66 }67)68// commandAllo responds to the ALLO FTP command.69//70// This is essentially a ping from the client so we just respond with an71// basic OK message.72type commandAllo struct{}73func (cmd commandAllo) IsExtend() bool {74 return false75}76func (cmd commandAllo) RequireParam() bool {77 return false78}79func (cmd commandAllo) RequireAuth() bool {80 return false81}82func (cmd commandAllo) Execute(conn *Conn, param string) {83 conn.writeMessage(202, "Obsolete")84}85type commandAppe struct{}86func (cmd commandAppe) IsExtend() bool {87 return false88}89func (cmd commandAppe) RequireParam() bool {90 return false91}92func (cmd commandAppe) RequireAuth() bool {93 return true94}95func (cmd commandAppe) Execute(conn *Conn, param string) {96 conn.appendData = true97 conn.writeMessage(202, "Obsolete")98}99type commandOpts struct{}100func (cmd commandOpts) IsExtend() bool {101 return false102}103func (cmd commandOpts) RequireParam() bool {104 return false105}106func (cmd commandOpts) RequireAuth() bool {107 return false108}109func (cmd commandOpts) Execute(conn *Conn, param string) {110 parts := strings.Fields(param)111 if len(parts) != 2 {112 conn.writeMessage(550, "Unknow params")113 return114 }115 if strings.ToUpper(parts[0]) != "UTF8" {116 conn.writeMessage(550, "Unknow params")117 return118 }119 if strings.ToUpper(parts[1]) == "ON" {120 conn.writeMessage(200, "UTF8 mode enabled")121 } else {122 conn.writeMessage(550, "Unsupported non-utf8 mode")123 }124}125type commandFeat struct{}126func (cmd commandFeat) IsExtend() bool {127 return false128}129func (cmd commandFeat) RequireParam() bool {130 return false131}132func (cmd commandFeat) RequireAuth() bool {133 return false134}135var (136 feats = "211-Extensions supported:\n%s211 END"137 featCmds = ""138)139func init() {140 for k, v := range commands {141 if v.IsExtend() {142 featCmds = featCmds + " " + k + "\n"143 }144 }145}146func (cmd commandFeat) Execute(conn *Conn, param string) {147 if conn.tlsConfig != nil {148 featCmds += " AUTH TLS\n PBSZ\n PROT\n"149 }150 conn.writeMessage(211, fmt.Sprintf(feats, featCmds))151}152// cmdCdup responds to the CDUP FTP command.153//154// Allows the client change their current directory to the parent.155type commandCdup struct{}156func (cmd commandCdup) IsExtend() bool {157 return false158}159func (cmd commandCdup) RequireParam() bool {160 return false161}162func (cmd commandCdup) RequireAuth() bool {163 return true164}165func (cmd commandCdup) Execute(conn *Conn, param string) {166 otherCmd := &commandCwd{}167 otherCmd.Execute(conn, "..")168}169// commandCwd responds to the CWD FTP command. It allows the client to change the170// current working directory.171type commandCwd struct{}172func (cmd commandCwd) IsExtend() bool {173 return false174}175func (cmd commandCwd) RequireParam() bool {176 return true177}178func (cmd commandCwd) RequireAuth() bool {179 return true180}181func (cmd commandCwd) Execute(conn *Conn, param string) {182 path := conn.buildPath(param)183 err := conn.driver.ChangeDir(path)184 if err == nil {185 conn.namePrefix = path186 conn.writeMessage(250, "Directory changed to "+path)187 } else {188 conn.writeMessage(450, "Action not taken "+err.Error())189 }190}191// commandDele responds to the DELE FTP command. It allows the client to delete192// a file193type commandDele struct{}194func (cmd commandDele) IsExtend() bool {195 return false196}197func (cmd commandDele) RequireParam() bool {198 return true199}200func (cmd commandDele) RequireAuth() bool {201 return true202}203func (cmd commandDele) Execute(conn *Conn, param string) {204 path := conn.buildPath(param)205 err := conn.driver.DeleteFile(path)206 if err == nil {207 conn.writeMessage(250, "File deleted")208 } else {209 conn.writeMessage(550, "Action not taken "+err.Error())210 }211}212// commandEprt responds to the EPRT FTP command. It allows the client to213// request an active data socket with more options than the original PORT214// command. It mainly adds ipv6 support.215type commandEprt struct{}216func (cmd commandEprt) IsExtend() bool {217 return true218}219func (cmd commandEprt) RequireParam() bool {220 return true221}222func (cmd commandEprt) RequireAuth() bool {223 return true224}225func (cmd commandEprt) Execute(conn *Conn, param string) {226 delim := string(param[0:1])227 parts := strings.Split(param, delim)228 addressFamily, err := strconv.Atoi(parts[1])229 host := parts[2]230 port, err := strconv.Atoi(parts[3])231 if addressFamily != 1 && addressFamily != 2 {232 conn.writeMessage(522, "Network protocol not supported, use (1,2)")233 return234 }235 socket, err := newActiveSocket(host, port, conn.logger)236 if err != nil {237 conn.writeMessage(425, "Data connection failed")238 return239 }240 conn.dataConn = socket241 conn.writeMessage(200, "Connection established ("+strconv.Itoa(port)+")")242}243// commandEpsv responds to the EPSV FTP command. It allows the client to244// request a passive data socket with more options than the original PASV245// command. It mainly adds ipv6 support, although we don't support that yet.246type commandEpsv struct{}247func (cmd commandEpsv) IsExtend() bool {248 return true249}250func (cmd commandEpsv) RequireParam() bool {251 return false252}253func (cmd commandEpsv) RequireAuth() bool {254 return true255}256func (cmd commandEpsv) Execute(conn *Conn, param string) {257 addr := conn.passiveListenIP()258 lastIdx := strings.LastIndex(addr, ":")259 if lastIdx <= 0 {260 conn.writeMessage(425, "Data connection failed")261 return262 }263 socket, err := newPassiveSocket(addr[:lastIdx], conn.PassivePort(), conn.logger, conn.tlsConfig)264 if err != nil {265 log.Println(err)266 conn.writeMessage(425, "Data connection failed")267 return268 }269 conn.dataConn = socket270 msg := fmt.Sprintf("Entering Extended Passive Mode (|||%d|)", socket.Port())271 conn.writeMessage(229, msg)272}273// commandList responds to the LIST FTP command. It allows the client to retreive274// a detailed listing of the contents of a directory.275type commandList struct{}276func (cmd commandList) IsExtend() bool {277 return false278}279func (cmd commandList) RequireParam() bool {280 return false281}282func (cmd commandList) RequireAuth() bool {283 return true284}285func (cmd commandList) Execute(conn *Conn, param string) {286 conn.writeMessage(150, "Opening ASCII mode data connection for file list")287 var fpath string288 if len(param) == 0 {289 fpath = param290 } else {291 fields := strings.Fields(param)292 for _, field := range fields {293 if strings.HasPrefix(field, "-") {294 //TODO: currently ignore all the flag295 //fpath = conn.namePrefix296 } else {297 fpath = field298 }299 }300 }301 path := conn.buildPath(fpath)302 info, err := conn.driver.Stat(path)303 if err != nil {304 conn.writeMessage(500, err.Error())305 conn.Close()306 return307 }308 if !info.IsDir() {309 conn.logger.Printf("%s is not a dir.\n", path)310 return311 }312 var files []FileInfo313 err = conn.driver.ListDir(path, func(f FileInfo) error {314 files = append(files, f)315 return nil316 })317 if err != nil {318 conn.writeMessage(500, err.Error())319 conn.Close()320 return321 }322 conn.sendOutofbandData(listFormatter(files).Detailed())323}324// commandNlst responds to the NLST FTP command. It allows the client to325// retreive a list of filenames in the current directory.326type commandNlst struct{}327func (cmd commandNlst) IsExtend() bool {328 return false329}330func (cmd commandNlst) RequireParam() bool {331 return false332}333func (cmd commandNlst) RequireAuth() bool {334 return true335}336func (cmd commandNlst) Execute(conn *Conn, param string) {337 conn.writeMessage(150, "Opening ASCII mode data connection for file list")338 var fpath string339 if len(param) == 0 {340 fpath = param341 } else {342 fields := strings.Fields(param)343 for _, field := range fields {344 if strings.HasPrefix(field, "-") {345 //TODO: currently ignore all the flag346 //fpath = conn.namePrefix347 } else {348 fpath = field349 }350 }351 }352 path := conn.buildPath(fpath)353 info, err := conn.driver.Stat(path)354 if err != nil {355 conn.writeMessage(502, err.Error())356 return357 }358 if !info.IsDir() {359 // TODO: should we show the file description?360 return361 }362 var files []FileInfo363 err = conn.driver.ListDir(path, func(f FileInfo) error {364 files = append(files, f)365 return nil366 })367 if err != nil {368 conn.writeMessage(502, err.Error())369 return370 }371 conn.sendOutofbandData(listFormatter(files).Short())372}373// commandMdtm responds to the MDTM FTP command. It allows the client to374// retreive the last modified time of a file.375type commandMdtm struct{}376func (cmd commandMdtm) IsExtend() bool {377 return false378}379func (cmd commandMdtm) RequireParam() bool {380 return true381}382func (cmd commandMdtm) RequireAuth() bool {383 return true384}385func (cmd commandMdtm) Execute(conn *Conn, param string) {386 path := conn.buildPath(param)387 stat, err := conn.driver.Stat(path)388 if err == nil {389 conn.writeMessage(213, stat.ModTime().Format("20060102150405"))390 } else {391 conn.writeMessage(450, "File not available")392 }393}394// commandMkd responds to the MKD FTP command. It allows the client to create395// a new directory396type commandMkd struct{}397func (cmd commandMkd) IsExtend() bool {398 return false399}400func (cmd commandMkd) RequireParam() bool {401 return true402}403func (cmd commandMkd) RequireAuth() bool {404 return true405}406func (cmd commandMkd) Execute(conn *Conn, param string) {407 path := conn.buildPath(param)408 err := conn.driver.MakeDir(path)409 if err == nil {410 conn.writeMessage(257, "Directory created")411 } else {412 conn.writeMessage(550, "Action not taken "+err.Error())413 }414}415// cmdMode responds to the MODE FTP command.416//417// the original FTP spec had various options for hosts to negotiate how data418// would be sent over the data socket, In reality these days (S)tream mode419// is all that is used for the mode - data is just streamed down the data420// socket unchanged.421type commandMode struct{}422func (cmd commandMode) IsExtend() bool {423 return false424}425func (cmd commandMode) RequireParam() bool {426 return true427}428func (cmd commandMode) RequireAuth() bool {429 return true430}431func (cmd commandMode) Execute(conn *Conn, param string) {432 if strings.ToUpper(param) == "S" {433 conn.writeMessage(200, "OK")434 } else {435 conn.writeMessage(504, "MODE is an obsolete command")436 }437}438// cmdNoop responds to the NOOP FTP command.439//440// This is essentially a ping from the client so we just respond with an441// basic 200 message.442type commandNoop struct{}443func (cmd commandNoop) IsExtend() bool {444 return false445}446func (cmd commandNoop) RequireParam() bool {447 return false448}449func (cmd commandNoop) RequireAuth() bool {450 return false451}452func (cmd commandNoop) Execute(conn *Conn, param string) {453 conn.writeMessage(200, "OK")454}455// commandPass respond to the PASS FTP command by asking the driver if the456// supplied username and password are valid457type commandPass struct{}458func (cmd commandPass) IsExtend() bool {459 return false460}461func (cmd commandPass) RequireParam() bool {462 return true463}464func (cmd commandPass) RequireAuth() bool {465 return false466}467func (cmd commandPass) Execute(conn *Conn, param string) {468 ok, err := conn.server.Auth.CheckPasswd(conn.driver, conn.reqUser, param)469 if err != nil {470 conn.writeMessage(550, "Checking password error")471 return472 }473 if ok {474 conn.user = conn.reqUser475 conn.reqUser = ""476 conn.namePrefix = "/"477 conn.writeMessage(230, "Password ok, continue")478 } else {479 conn.writeMessage(530, "Incorrect password, not logged in")480 }481}482type commandPasv struct{}483func (cmd commandPasv) IsExtend() bool {484 return false485}486func (cmd commandPasv) RequireParam() bool {487 return false488}489func (cmd commandPasv) RequireAuth() bool {490 return true491}492func (cmd commandPasv) Execute(conn *Conn, param string) {493 listenIP := conn.passiveListenIP()494 socket, err := newPassiveSocket(listenIP, conn.PassivePort(), conn.logger, conn.tlsConfig)495 if err != nil {496 conn.writeMessage(425, "Data connection failed")497 return498 }499 conn.dataConn = socket500 p1 := socket.Port() / 256501 p2 := socket.Port() - (p1 * 256)502 quads := strings.Split(listenIP, ".")503 target := fmt.Sprintf("(%s,%s,%s,%s,%d,%d)", quads[0], quads[1], quads[2], quads[3], p1, p2)504 msg := "Entering Passive Mode " + target505 conn.writeMessage(227, msg)506}507// commandPort responds to the PORT FTP command.508//509// The client has opened a listening socket for sending out of band data and510// is requesting that we connect to it511type commandPort struct{}512func (cmd commandPort) IsExtend() bool {513 return false514}515func (cmd commandPort) RequireParam() bool {516 return true517}518func (cmd commandPort) RequireAuth() bool {519 return true520}521func (cmd commandPort) Execute(conn *Conn, param string) {522 nums := strings.Split(param, ",")523 portOne, _ := strconv.Atoi(nums[4])524 portTwo, _ := strconv.Atoi(nums[5])525 port := (portOne * 256) + portTwo526 host := nums[0] + "." + nums[1] + "." + nums[2] + "." + nums[3]527 socket, err := newActiveSocket(host, port, conn.logger)528 if err != nil {529 conn.writeMessage(425, "Data connection failed")530 return531 }532 conn.dataConn = socket533 conn.writeMessage(200, "Connection established ("+strconv.Itoa(port)+")")534}535// commandPwd responds to the PWD FTP command.536//537// Tells the client what the current working directory is.538type commandPwd struct{}539func (cmd commandPwd) IsExtend() bool {540 return false541}542func (cmd commandPwd) RequireParam() bool {543 return false544}545func (cmd commandPwd) RequireAuth() bool {546 return true547}548func (cmd commandPwd) Execute(conn *Conn, param string) {549 conn.writeMessage(257, "\""+conn.namePrefix+"\" is the current directory")550}551// CommandQuit responds to the QUIT FTP command. The client has requested the552// connection be closed.553type commandQuit struct{}554func (cmd commandQuit) IsExtend() bool {555 return false556}557func (cmd commandQuit) RequireParam() bool {558 return false559}560func (cmd commandQuit) RequireAuth() bool {561 return false562}563func (cmd commandQuit) Execute(conn *Conn, param string) {564 conn.writeMessage(221, "Goodbye")565 conn.Close()566}567// commandRetr responds to the RETR FTP command. It allows the client to568// download a file.569type commandRetr struct{}570func (cmd commandRetr) IsExtend() bool {571 return false572}573func (cmd commandRetr) RequireParam() bool {574 return true575}576func (cmd commandRetr) RequireAuth() bool {577 return true578}579func (cmd commandRetr) Execute(conn *Conn, param string) {580 path := conn.buildPath(param)581 defer func() {582 conn.lastFilePos = 0583 }()584 bytes, data, err := conn.driver.GetFile(path, conn.lastFilePos)585 if err == nil {586 defer data.Close()587 conn.writeMessage(150, fmt.Sprintf("Data transfer starting %v bytes", bytes))588 err = conn.sendOutofBandDataWriter(data)589 } else {590 conn.writeMessage(551, "File not available")591 }592}593type commandRest struct{}594func (cmd commandRest) IsExtend() bool {595 return false596}597func (cmd commandRest) RequireParam() bool {598 return true599}600func (cmd commandRest) RequireAuth() bool {601 return true602}603func (cmd commandRest) Execute(conn *Conn, param string) {604 var err error605 conn.lastFilePos, err = strconv.ParseInt(param, 10, 64)606 if err != nil {607 conn.writeMessage(551, "File not available")608 return609 }610 conn.appendData = true611 conn.writeMessage(350, fmt.Sprintln("Start transfer from", conn.lastFilePos))612}613// commandRnfr responds to the RNFR FTP command. It's the first of two commands614// required for a client to rename a file.615type commandRnfr struct{}616func (cmd commandRnfr) IsExtend() bool {617 return false618}619func (cmd commandRnfr) RequireParam() bool {620 return true621}622func (cmd commandRnfr) RequireAuth() bool {623 return true624}625func (cmd commandRnfr) Execute(conn *Conn, param string) {626 conn.renameFrom = conn.buildPath(param)627 conn.writeMessage(350, "Requested file action pending further information.")628}629// cmdRnto responds to the RNTO FTP command. It's the second of two commands630// required for a client to rename a file.631type commandRnto struct{}632func (cmd commandRnto) IsExtend() bool {633 return false634}635func (cmd commandRnto) RequireParam() bool {636 return true637}638func (cmd commandRnto) RequireAuth() bool {639 return true640}641func (cmd commandRnto) Execute(conn *Conn, param string) {642 toPath := conn.buildPath(param)643 err := conn.driver.Rename(conn.renameFrom, toPath)644 defer func() {645 conn.renameFrom = ""646 }()647 if err == nil {648 conn.writeMessage(250, "File renamed")649 } else {650 conn.writeMessage(502, "Action not taken "+err.Error())651 }652}653// cmdRmd responds to the RMD FTP command. It allows the client to delete a654// directory.655type commandRmd struct{}656func (cmd commandRmd) IsExtend() bool {657 return false658}659func (cmd commandRmd) RequireParam() bool {660 return true661}662func (cmd commandRmd) RequireAuth() bool {663 return true664}665func (cmd commandRmd) Execute(conn *Conn, param string) {666 path := conn.buildPath(param)667 err := conn.driver.DeleteDir(path)668 if err == nil {669 conn.writeMessage(250, "Directory deleted")670 } else {671 conn.writeMessage(502, "Action not taken "+err.Error())672 }673}674type commandAdat struct{}675func (cmd commandAdat) IsExtend() bool {676 return false677}678func (cmd commandAdat) RequireParam() bool {679 return true680}681func (cmd commandAdat) RequireAuth() bool {682 return true683}684func (cmd commandAdat) Execute(conn *Conn, param string) {685 conn.writeMessage(550, "Action not taken")686}687type commandAuth struct{}688func (cmd commandAuth) IsExtend() bool {689 return false690}691func (cmd commandAuth) RequireParam() bool {692 return true693}694func (cmd commandAuth) RequireAuth() bool {695 return false696}697func (cmd commandAuth) Execute(conn *Conn, param string) {698 log.Println(param, conn)699 if param == "TLS" && conn.tlsConfig != nil {700 conn.writeMessage(234, "AUTH command OK")701 err := conn.upgradeToTLS()702 if err != nil {703 conn.logger.Printf("Error upgrading conection to TLS %v", err)704 }705 } else {706 conn.writeMessage(550, "Action not taken")707 }708}709type commandCcc struct{}710func (cmd commandCcc) IsExtend() bool {711 return false712}713func (cmd commandCcc) RequireParam() bool {714 return true715}716func (cmd commandCcc) RequireAuth() bool {717 return true718}719func (cmd commandCcc) Execute(conn *Conn, param string) {720 conn.writeMessage(550, "Action not taken")721}722type commandEnc struct{}723func (cmd commandEnc) IsExtend() bool {724 return false725}726func (cmd commandEnc) RequireParam() bool {727 return true728}729func (cmd commandEnc) RequireAuth() bool {730 return true731}732func (cmd commandEnc) Execute(conn *Conn, param string) {733 conn.writeMessage(550, "Action not taken")734}735type commandMic struct{}736func (cmd commandMic) IsExtend() bool {737 return false738}739func (cmd commandMic) RequireParam() bool {740 return true741}742func (cmd commandMic) RequireAuth() bool {743 return true744}745func (cmd commandMic) Execute(conn *Conn, param string) {746 conn.writeMessage(550, "Action not taken")747}748type commandPbsz struct{}749func (cmd commandPbsz) IsExtend() bool {750 return false751}752func (cmd commandPbsz) RequireParam() bool {753 return true754}755func (cmd commandPbsz) RequireAuth() bool {756 return true757}758func (cmd commandPbsz) Execute(conn *Conn, param string) {759 if conn.tls && param == "0" {760 conn.writeMessage(200, "OK")761 } else {762 conn.writeMessage(550, "Action not taken")763 }764}765type commandProt struct{}766func (cmd commandProt) IsExtend() bool {767 return false768}769func (cmd commandProt) RequireParam() bool {770 return true771}772func (cmd commandProt) RequireAuth() bool {773 return true774}775func (cmd commandProt) Execute(conn *Conn, param string) {776 if conn.tls && param == "P" {777 conn.writeMessage(200, "OK")778 } else if conn.tls {779 conn.writeMessage(536, "Only P level is supported")780 } else {781 conn.writeMessage(550, "Action not taken")782 }783}784type commandConf struct{}785func (cmd commandConf) IsExtend() bool {786 return false787}788func (cmd commandConf) RequireParam() bool {789 return true790}791func (cmd commandConf) RequireAuth() bool {792 return true793}794func (cmd commandConf) Execute(conn *Conn, param string) {795 conn.writeMessage(550, "Action not taken")796}797// commandSize responds to the SIZE FTP command. It returns the size of the798// requested path in bytes.799type commandSize struct{}800func (cmd commandSize) IsExtend() bool {801 return false802}803func (cmd commandSize) RequireParam() bool {804 return true805}806func (cmd commandSize) RequireAuth() bool {807 return true808}809func (cmd commandSize) Execute(conn *Conn, param string) {810 path := conn.buildPath(param)811 stat, err := conn.driver.Stat(path)812 if err != nil {813 log.Printf("Size: error(%s)", err)814 conn.writeMessage(450, err.Error())815 } else {816 conn.writeMessage(213, strconv.Itoa(int(stat.Size())))817 }818}819// commandStor responds to the STOR FTP command. It allows the user to upload a820// new file.821type commandStor struct{}822func (cmd commandStor) IsExtend() bool {823 return false824}825func (cmd commandStor) RequireParam() bool {826 return true827}828func (cmd commandStor) RequireAuth() bool {829 return true830}831func (cmd commandStor) Execute(conn *Conn, param string) {832 targetPath := conn.buildPath(param)833 conn.writeMessage(150, "Data transfer starting")834 defer func() {835 conn.appendData = false836 }()837 bytes, err := conn.driver.PutFile(targetPath, conn.dataConn, conn.appendData)838 if err == nil {839 msg := "OK, received " + strconv.Itoa(int(bytes)) + " bytes"840 conn.writeMessage(226, msg)841 } else {842 conn.writeMessage(450, "Action not taken "+err.Error())843 }844}845// commandStru responds to the STRU FTP command.846//847// like the MODE and TYPE commands, stru[cture] dates back to a time when the848// FTP protocol was more aware of the content of the files it was transferring,849// and would sometimes be expected to translate things like EOL markers on the850// fly.851//852// These days files are sent unmodified, and F(ile) mode is the only one we853// really need to support.854type commandStru struct{}855func (cmd commandStru) IsExtend() bool {856 return false857}858func (cmd commandStru) RequireParam() bool {859 return true860}861func (cmd commandStru) RequireAuth() bool {862 return true863}864func (cmd commandStru) Execute(conn *Conn, param string) {865 if strings.ToUpper(param) == "F" {866 conn.writeMessage(200, "OK")867 } else {868 conn.writeMessage(504, "STRU is an obsolete command")869 }870}871// commandSyst responds to the SYST FTP command by providing a canned response.872type commandSyst struct{}873func (cmd commandSyst) IsExtend() bool {874 return false875}876func (cmd commandSyst) RequireParam() bool {877 return false878}879func (cmd commandSyst) RequireAuth() bool {880 return true881}882func (cmd commandSyst) Execute(conn *Conn, param string) {883 conn.writeMessage(215, "UNIX Type: L8")884}885// commandType responds to the TYPE FTP command.886//887// like the MODE and STRU commands, TYPE dates back to a time when the FTP888// protocol was more aware of the content of the files it was transferring, and889// would sometimes be expected to translate things like EOL markers on the fly.890//891// Valid options were A(SCII), I(mage), E(BCDIC) or LN (for local type). Since892// we plan to just accept bytes from the client unchanged, I think Image mode is893// adequate. The RFC requires we accept ASCII mode however, so accept it, but894// ignore it.895type commandType struct{}896func (cmd commandType) IsExtend() bool {897 return false898}899func (cmd commandType) RequireParam() bool {900 return false901}902func (cmd commandType) RequireAuth() bool {903 return true904}905func (cmd commandType) Execute(conn *Conn, param string) {906 if strings.ToUpper(param) == "A" {907 conn.writeMessage(200, "Type set to ASCII")908 } else if strings.ToUpper(param) == "I" {909 conn.writeMessage(200, "Type set to binary")910 } else {911 conn.writeMessage(500, "Invalid type")912 }913}914// commandUser responds to the USER FTP command by asking for the password915type commandUser struct{}916func (cmd commandUser) IsExtend() bool {917 return false918}919func (cmd commandUser) RequireParam() bool {920 return true921}922func (cmd commandUser) RequireAuth() bool {923 return false924}925func (cmd commandUser) Execute(conn *Conn, param string) {926 conn.reqUser = param927 conn.writeMessage(331, "User name ok, password required")928}...
execute_test.go
Source:execute_test.go
...31 if tempFile == nil {32 t.Fatal("Could not create temp pipeline file.")33 }34 defer os.Remove(tempFile.Name())35 args := []string{"pipeline", "execute", "--application", "app", "--name", "one", "--gate-endpoint", ts.URL}36 currentCmd := NewExecuteCmd(pipelineOptions{})37 rootCmd := getRootCmdForTest()38 pipelineCmd := NewPipelineCmd(os.Stdout)39 pipelineCmd.AddCommand(currentCmd)40 rootCmd.AddCommand(pipelineCmd)41 rootCmd.SetArgs(args)42 err := rootCmd.Execute()43 if err != nil {44 t.Fatalf("Command failed with: %s", err)45 }46}47func TestPipelineExecute_fail(t *testing.T) {48 ts := GateServerFail()49 defer ts.Close()50 tempFile := tempPipelineFile(testPipelineJsonStr)51 if tempFile == nil {52 t.Fatal("Could not create temp pipeline file.")53 }54 defer os.Remove(tempFile.Name())55 args := []string{"pipeline", "execute", "--application", "app", "--name", "one", "--gate-endpoint", ts.URL}56 currentCmd := NewExecuteCmd(pipelineOptions{})57 rootCmd := getRootCmdForTest()58 pipelineCmd := NewPipelineCmd(os.Stdout)59 pipelineCmd.AddCommand(currentCmd)60 rootCmd.AddCommand(pipelineCmd)61 rootCmd.SetArgs(args)62 err := rootCmd.Execute()63 if err == nil {64 t.Fatalf("Command failed with: %s", err)65 }66}67func TestPipelineExecute_flags(t *testing.T) {68 ts := GateServerSuccess()69 defer ts.Close()70 args := []string{"pipeline", "execute", "--gate-endpoint", ts.URL} // Missing pipeline app and name.71 currentCmd := NewExecuteCmd(pipelineOptions{})72 rootCmd := getRootCmdForTest()73 pipelineCmd := NewPipelineCmd(os.Stdout)74 pipelineCmd.AddCommand(currentCmd)75 rootCmd.AddCommand(pipelineCmd)76 rootCmd.SetArgs(args)77 err := rootCmd.Execute()78 if err == nil {79 t.Fatalf("Command failed with: %s", err)80 }81}82func TestPipelineExecute_missingname(t *testing.T) {83 ts := GateServerSuccess()84 defer ts.Close()85 tempFile := tempPipelineFile(missingNameJsonStr)86 if tempFile == nil {87 t.Fatal("Could not create temp pipeline file.")88 }89 defer os.Remove(tempFile.Name())90 args := []string{"pipeline", "execute", "--application", "app", "--gate-endpoint", ts.URL}91 currentCmd := NewExecuteCmd(pipelineOptions{})92 rootCmd := getRootCmdForTest()93 pipelineCmd := NewPipelineCmd(os.Stdout)94 pipelineCmd.AddCommand(currentCmd)95 rootCmd.AddCommand(pipelineCmd)96 rootCmd.SetArgs(args)97 err := rootCmd.Execute()98 if err == nil {99 t.Fatalf("Command failed with: %s", err)100 }101}102func TestPipelineExecute_missingapp(t *testing.T) {103 ts := GateServerSuccess()104 defer ts.Close()105 tempFile := tempPipelineFile(missingAppJsonStr)106 if tempFile == nil {107 t.Fatal("Could not create temp pipeline file.")108 }109 defer os.Remove(tempFile.Name())110 args := []string{"pipeline", "execute", "--name", "one", "--gate-endpoint", ts.URL}111 currentCmd := NewExecuteCmd(pipelineOptions{})112 rootCmd := getRootCmdForTest()113 pipelineCmd := NewPipelineCmd(os.Stdout)114 pipelineCmd.AddCommand(currentCmd)115 rootCmd.AddCommand(pipelineCmd)116 rootCmd.SetArgs(args)117 err := rootCmd.Execute()118 if err == nil {119 t.Fatalf("Command failed with: %s", err)120 }121}122// testGatePipelineExecuteSuccess spins up a local http server that we will configure the GateClient123// to direct requests to. Responds with successful responses to pipeline execute API calls.124func testGatePipelineExecuteSuccess() *httptest.Server {125 mux := http.NewServeMux()126 mux.Handle("/pipelines/app/one", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {127 resp := gate.ResponseEntity{StatusCode: "201 Accepted", StatusCodeValue: 201}128 b, _ := json.Marshal(&resp)129 w.WriteHeader(http.StatusAccepted)130 fmt.Fprintln(w, string(b)) // Write empty 201.131 }))132 mux.Handle("/applications/app/executions/search", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {133 w.WriteHeader(http.StatusOK)134 fmt.Fprintln(w, strings.TrimSpace(executions))135 }))136 return httptest.NewServer(mux)137}...
utils_files.go
Source:utils_files.go
...22func writeGoMod(projectName string){23 var content = `package go_commands24import (25 "fmt"26 execute "github.com/alexellis/go-execute/pkg/v1"27)28func GoModInit(username, projectName string){29 cmd := execute.ExecTask{30 Command: "go",31 Args: []string{"mod", "init", "github.com/" + username + "/" + projectName},32 StreamStdio: false,33 Cwd: projectName,34 }35 res, err := cmd.Execute()36 if err != nil {37 panic(err)38 }39 fmt.Printf("output: %s", res.Stderr)40}41func GoModTidy(projectName string){42 cmd := execute.ExecTask{43 Command: "go",44 Args: []string{"mod", "tidy"},45 StreamStdio: false,46 Cwd: projectName,47 }48 res, err := cmd.Execute()49 if err != nil {50 panic(err)51 }52 fmt.Printf("output: %s", res.Stderr)53}54func GoGet(packageName, projectName string){55 cmd := execute.ExecTask{56 Command: "go",57 Args: []string{"get", "-u", packageName},58 StreamStdio: false,59 Cwd: projectName,60 }61 res, err := cmd.Execute()62 if err != nil {63 panic(err)64 }65 fmt.Printf("output: %s", res.Stderr)66}67func GoModVendor(projectName string){68 cmd := execute.ExecTask{69 Command: "go",70 Args: []string{"mod", "vendor"},71 StreamStdio: false,72 Cwd: projectName,73 }74 res, err := cmd.Execute()75 if err != nil {76 panic(err)77 }78 fmt.Printf("output: %s", res.Stderr)79}80`81 file, err := os.Create(projectName + "/utils/go_commands/go_mod.go")82 if err != nil {83 log.Fatal(err)84 }85 _, err = file.WriteString(content)86 if err != nil {87 log.Fatal(err)88 }89}90func writeShellCommands(projectName string){91 var content = `package shell_commands92import (93 "fmt"94 execute "github.com/alexellis/go-execute/pkg/v1"95)96func ExecuteSh(file, projectName string){97 cmd := execute.ExecTask{98 Command: "sh",99 Args: []string{file},100 StreamStdio: false,101 Cwd: projectName,102 }103 res, err := cmd.Execute()104 if err != nil {105 panic(err)106 }107 fmt.Printf("output: %s", res.Stderr)108}109func ExecuteShellCommand(command string, projectName string, args ...string){110 cmd := execute.ExecTask{111 Command: command,112 Args: args,113 StreamStdio: false,114 Cwd: projectName,115 }116 res, err := cmd.Execute()117 if err != nil {118 panic(err)119 }120 fmt.Printf("output: %s", res.Stderr)121}122`123 file, err := os.Create(projectName + "/utils/shell_commands/shell_commands.go")124 if err != nil {...
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(out))9}
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(out))9}
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(out))9}
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-ltr")4 output, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(output))9}
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-la")4 result, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(result))9}
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls")4 cmd.Stdout = &bytes.Buffer{}5 err := cmd.Run()6 if err != nil {7 fmt.Println("Error executing command:", err)8 }9 fmt.Println(cmd.Stdout)10}11import (12func main() {13 out, err := exec.Command("ls").Output()14 if err != nil {15 fmt.Println("Error executing command:", err)16 }17 fmt.Println(out)18}19&{0xc42000a320 0xc42000a340 0xc42000a360 0xc42000a380 0xc42000a3a0 0xc42000a3c0 0xc42000a3e0 0xc42000a400 0xc42000a420 0xc42000a440 0xc42000a460 0xc42000a480 0xc42000a4a0 0xc42000a4c0 0xc42000a4e0 0xc42000a500 0xc42000a520 0xc42000a540 0xc42000a560 0xc42000a580 0xc42000a5a0 0xc42000a5c0 0xc42000a5e0 0xc42000a600 0xc42000a620 0xc42000a640 0xc42000a660 0xc42000a680 0xc42000a6a0 0xc42000a6c0 0xc42000a6e0 0xc42000a700 0xc42000a720 0xc42000a740 0xc42000a760 0xc42000a780 0xc42000a7a0 0xc42000a7c0 0xc42000a7e0 0xc42000a800 0xc42000a820 0xc42000a840 0xc42000a860 0xc42000a880 0xc42000a8a0 0xc
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("cmd", "/C", "echo", "hello")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err.Error())7 }8 fmt.Println("The date is ", string(out))9}10import (11func main() {12 cmd := exec.Command("cmd", "/C", "echo", "hello")13 err := cmd.Run()14 if err != nil {15 fmt.Println(err.Error())16 }17 fmt.Println("The date is ")18}19import (20func main() {21 cmd := exec.Command("cmd", "/C", "echo", "hello")22 err := cmd.Start()23 if err != nil {24 fmt.Println(err.Error())25 }26 fmt.Println("The date is ")27}28import (29func main() {30 cmd := exec.Command("cmd", "/C", "echo", "hello")31 out, err := cmd.CombinedOutput()32 if err != nil {33 fmt.Println(err.Error())34 }35 fmt.Println("The date is ", string(out))36}37import (38func main() {39 cmd := exec.Command("cmd", "/C", "echo", "hello")40 path, err := cmd.LookPath()
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(out))9}10import (11func main() {12 cmd := exec.Command("ls", "-l")13 err := cmd.Start()14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println("Command executed successfully...")18}19import (20func main() {21 cmd := exec.Command("ls", "-l")22 err := cmd.Run()23 if err != nil {24 fmt.Println(err)25 }26 fmt.Println("Command executed successfully...")27}28import (29func main() {30 cmd := exec.Command("ls", "-l")31 out, err := cmd.CombinedOutput()32 if err != nil {33 fmt.Println(err)34 }35 fmt.Println(string(out))36}37import (38func main() {39 cmd := exec.Command("ls", "-l")
execute
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-la")4 output, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println("Output is: ", string(output))9}10import (11func main() {12 cmd := exec.Command("ls", "-la")13 output, err := cmd.CombinedOutput()14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println("Output is: ", string(output))18}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!