-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the capability to alias resource names and aliases line_items ->…
… invoice_line_items (#1270) * Adds basic functions for managing a list of aliases for commands to rename them while leaving the original resource hidden (but functional for backward compatibility) * Refactors resource command generation to create duplicate resources with aliased names when an alias is defined. Also removes some unnecessary pointer returns * Hides principle resources when they have been aliased and teaches help text to omit hidden resources * Generates resource commands with line_items aliased to invoice_line_items * Adds tests to ensure aliases are hidden in the resource list and that aliased and principle resources hit the same underlying APIs
- Loading branch information
1 parent
25dfc23
commit 7ffd100
Showing
7 changed files
with
161 additions
and
24 deletions.
There are no files selected for viewing
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,39 @@ | ||
package resource | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Aliases are mapped from resource name in the OpenAPI spec -> friendly name in the CLI | ||
// Each alias causes a second resource command (the target / friendly name) to be generated, and uses post-processing | ||
// to hide the principle resource name (OpenAPI spec) | ||
var aliasedCmds = map[string]string{ | ||
"line_item": "invoice_line_item", | ||
} | ||
|
||
// GetCmdAlias retrieves the alias for a given resource, if one is present; otherwise returns "" | ||
func GetCmdAlias(principle string) string { | ||
alias, ok := aliasedCmds[principle] | ||
if !ok { | ||
return "" | ||
} | ||
return alias | ||
} | ||
|
||
// GetAliases retrieves the entire alias map, useful for testing | ||
func GetAliases() map[string]string { | ||
return aliasedCmds | ||
} | ||
|
||
// HideAliasedCommands performs the post-processing on the command tree to hide | ||
// resources that have an alias | ||
func HideAliasedCommands(rootCmd *cobra.Command) { | ||
for _, cmd := range rootCmd.Commands() { | ||
for principle := range aliasedCmds { | ||
formattedPrinciple := GetResourceCmdName(principle) | ||
if cmd.Use == formattedPrinciple { | ||
cmd.Hidden = true | ||
} | ||
} | ||
} | ||
} |
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
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