11package commands
22
33import (
4+ "context"
45 "fmt"
56 "strings"
67
@@ -13,15 +14,28 @@ import (
1314 "github.com/spf13/pflag"
1415)
1516
17+ type CommandContextKey string
18+
19+ const commandKey CommandContextKey = "command"
20+
1621// New returns a BaseCommand that implements Command. It is used as a base to create custom commands from.
1722func New (name , usage string , examples ... string ) * BaseCommand {
18- return & BaseCommand {
19- cobra : & cobra.Command {
20- Use : name ,
21- Short : usage ,
22- Example : strings .Join (examples , "\n " ),
23- },
23+ cmd := & cobra.Command {
24+ Use : name ,
25+ Short : usage ,
26+ Example : strings .Join (examples , "\n " ),
2427 }
28+
29+ // Initialize BaseCommand
30+ baseCmd := & BaseCommand {
31+ cobra : cmd ,
32+ }
33+
34+ // Store reference to itself in the context - We need this to access the command in the CobraCommand interface
35+ // Specifically to generate the reference documentation
36+ cmd .SetContext (context .WithValue (context .Background (), commandKey , baseCmd ))
37+
38+ return baseCmd
2539}
2640
2741// Command is the base command type for all commands.
@@ -125,7 +139,42 @@ func BuildCommand(child Command, parent *cobra.Command, config *config.Config) C
125139
126140// BaseCommand is the base type for all commands, implementing Command
127141type BaseCommand struct {
128- cobra * cobra.Command
142+ cobra * cobra.Command
143+ deprecatedAliases []string
144+ }
145+
146+ // Aliases return non deprecated aliases
147+ func (s * BaseCommand ) Aliases () []string {
148+ // Get all aliases from Cobra
149+ allAliases := s .cobra .Aliases
150+
151+ // Filter out deprecated aliases
152+ var filteredAliases []string
153+ for _ , alias := range allAliases {
154+ if ! s .isDeprecatedAlias (alias ) {
155+ filteredAliases = append (filteredAliases , alias )
156+ }
157+ }
158+
159+ return filteredAliases
160+ }
161+
162+ // isDeprecatedAlias checks if an alias is deprecated
163+ func (s * BaseCommand ) isDeprecatedAlias (alias string ) bool {
164+ for _ , deprecated := range s .deprecatedAliases {
165+ if alias == deprecated {
166+ return true
167+ }
168+ }
169+ return false
170+ }
171+
172+ func (s * BaseCommand ) DeprecatedAliases () []string {
173+ return s .deprecatedAliases
174+ }
175+
176+ func (s * BaseCommand ) SetDeprecatedAliases (aliases []string ) {
177+ s .deprecatedAliases = aliases
129178}
130179
131180// MaximumExecutions return the max executed workers
0 commit comments