-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement autocli customization (#13251)
* feat: implement autocli customization * WIP * integrate simple options * skip positional fields * WIP * WIP * WIP * trying to simplify diff, fixing errors * WIP * WIP * tests passing again * positional params working * add TODO * WIP on tests * WIP on tests * working tests * doc strings * docs * tests * tests * simplify API * proper validation * fix import * address review comments * address review comments Co-authored-by: Marko <[email protected]>
- Loading branch information
1 parent
6fdcbfd
commit fa2cb40
Showing
28 changed files
with
1,494 additions
and
363 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NOTE: this was copied from client/cmd.go to avoid introducing a dependency | ||
// on the v1 client package. | ||
|
||
// validateCmd returns unknown command error or Help display if help flag set | ||
func validateCmd(cmd *cobra.Command, args []string) error { | ||
var unknownCmd string | ||
var skipNext bool | ||
|
||
for _, arg := range args { | ||
// search for help flag | ||
if arg == "--help" || arg == "-h" { | ||
return cmd.Help() | ||
} | ||
|
||
// check if the current arg is a flag | ||
switch { | ||
case len(arg) > 0 && (arg[0] == '-'): | ||
// the next arg should be skipped if the current arg is a | ||
// flag and does not use "=" to assign the flag's value | ||
if !strings.Contains(arg, "=") { | ||
skipNext = true | ||
} else { | ||
skipNext = false | ||
} | ||
case skipNext: | ||
// skip current arg | ||
skipNext = false | ||
case unknownCmd == "": | ||
// unknown command found | ||
// continue searching for help flag | ||
unknownCmd = arg | ||
} | ||
} | ||
|
||
// return the help screen if no unknown command is found | ||
if unknownCmd != "" { | ||
err := fmt.Sprintf("unknown command \"%s\" for \"%s\"", unknownCmd, cmd.CalledAs()) | ||
|
||
// build suggestions for unknown argument | ||
if suggestions := cmd.SuggestionsFor(unknownCmd); len(suggestions) > 0 { | ||
err += "\n\nDid you mean this?\n" | ||
for _, s := range suggestions { | ||
err += fmt.Sprintf("\t%v\n", s) | ||
} | ||
} | ||
return errors.New(err) | ||
} | ||
|
||
return cmd.Help() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Package flag defines functionality for automatically managing command | ||
// line flags as well positional arguments that are based on protobuf message | ||
// fields. | ||
package flag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.