Skip to content

Commit fe63a2b

Browse files
jremy42Codelax
andauthored
feat(core): add support for handling deprecated method (#3786)
Co-authored-by: Jules Castéran <[email protected]>
1 parent d942896 commit fe63a2b

File tree

7 files changed

+118
-6
lines changed

7 files changed

+118
-6
lines changed

internal/core/autocomplete.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ func BuildAutoCompleteTree(ctx context.Context, commands *Commands) *AutoComplet
186186
globalFlags := getGlobalFlags(ctx)
187187
root := NewAutoCompleteCommandNode(globalFlags)
188188
for _, cmd := range commands.commands {
189+
if cmd.Deprecated {
190+
continue
191+
}
189192
node := root
190193

191194
// Creates nodes for namespaces, resources, verbs

internal/core/autocomplete_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ func testAutocompleteGetCommands() *core.Commands {
6767
},
6868
},
6969
},
70+
&core.Command{
71+
Namespace: "test",
72+
Resource: "flower",
73+
Verb: "deprecated",
74+
ArgsType: reflect.TypeOf(struct{}{}),
75+
Deprecated: true,
76+
Short: "this command is deprected",
77+
Long: "This command is deprecated and should not show up in autocomplete.",
78+
},
7079
)
7180
}
7281

@@ -291,3 +300,17 @@ func TestAutocompleteProfiles(t *testing.T) {
291300
t.Run("scw test -p ", run(&testCase{Suggestions: core.AutocompleteSuggestions{"p1", "p2"}}))
292301
t.Run("scw test flower --profile ", run(&testCase{Suggestions: core.AutocompleteSuggestions{"p1", "p2"}}))
293302
}
303+
304+
func TestAutocompleteDeprecatedCommand(t *testing.T) {
305+
ctx := core.InjectMeta(context.Background(), &core.Meta{
306+
Commands: testAutocompleteGetCommands(),
307+
})
308+
309+
type testCase = autoCompleteTestCase
310+
311+
run := func(tc *testCase) func(*testing.T) {
312+
return runAutocompleteTest(ctx, tc)
313+
}
314+
315+
t.Run("scw test flower deprecated", run(&testCase{Suggestions: nil}))
316+
}

internal/core/cobra_builder.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command, group
172172
cobraCmd.PersistentFlags().BoolP("wait", "w", false, waitUsage)
173173
}
174174

175+
if cmd.Deprecated {
176+
cobraCmd.IsAvailableCommand()
177+
cobraCmd.Deprecated = "Deprecated:"
178+
}
179+
175180
if commandHasWeb(cmd) {
176181
cobraCmd.PersistentFlags().Bool("web", false, "open console page for the current ressource")
177182
}
@@ -204,15 +209,19 @@ DEPRECATED ARGS:
204209
{{- range $_, $group := orderGroups (getCommandsGroups .Commands) }}
205210
206211
{{ $group.Title }} COMMANDS:
207-
{{- range $_, $command := orderCommands $.Commands }}
208-
{{- if $command.IsAvailableCommand }}
212+
{{- range $_, $command := orderCommands $.Commands }}
213+
{{- if or $command.IsAvailableCommand $command.Deprecated }}
209214
{{- if or ($command.ContainsGroup $group.ID) (and (eq $group.ID "utility") (eq $command.Name "help")) }}
210-
{{ rpad $command.Name .NamePadding }} {{ if $command.Short }}{{ $command.Short }}{{end}}
211-
{{- end }}
215+
{{ rpad $command.Name .NamePadding }}
216+
{{- if $command.Deprecated }} {{ if $command.Short }}{{ $command.Short }} (Deprecated){{ end }}
217+
{{- else }} {{ if $command.Short }}{{ $command.Short }}{{ end }}
218+
{{- end }}
212219
{{- end }}
213220
{{- end }}
214221
{{- end }}
215222
{{- end }}
223+
{{- end }}
224+
216225
{{- if .HasAvailableLocalFlags }}
217226
218227
FLAGS:

internal/core/cobra_builder_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,53 @@ func Test_UnknownCommand(t *testing.T) {
3939
),
4040
}))
4141
}
42+
43+
func Test_DeprecatedCommand(t *testing.T) {
44+
dummyNamespaceCommand := &core.Command{
45+
Namespace: "instance",
46+
}
47+
48+
dummyResourceCommand1 := &core.Command{
49+
Namespace: "instance",
50+
Resource: "server",
51+
Short: "short server",
52+
Deprecated: false,
53+
}
54+
55+
dummyResourceCommand2 := &core.Command{
56+
Namespace: "instance",
57+
Resource: "engine",
58+
Short: "short engine",
59+
Deprecated: true,
60+
}
61+
62+
dummyResourceCommand3 := &core.Command{
63+
Namespace: "instance",
64+
Resource: "a",
65+
Short: "short server",
66+
Deprecated: false,
67+
}
68+
69+
dummyResourceCommand4 := &core.Command{
70+
Namespace: "instance",
71+
Resource: "b",
72+
Short: "short server",
73+
Deprecated: true,
74+
}
75+
76+
cmds := core.NewCommands(
77+
dummyNamespaceCommand,
78+
dummyResourceCommand1,
79+
dummyResourceCommand2,
80+
dummyResourceCommand3,
81+
dummyResourceCommand4,
82+
)
83+
t.Run("", core.Test(&core.TestConfig{
84+
Commands: cmds,
85+
Cmd: "scw instance -h",
86+
Check: core.TestCheckCombine(
87+
core.TestCheckGolden(),
88+
core.TestCheckExitCode(0),
89+
),
90+
}))
91+
}

internal/core/cobra_usage_builder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ func orderCobraCommands(cobraCommands []*cobra.Command) []*cobra.Command {
161161
copy(commands, cobraCommands)
162162

163163
sort.Slice(commands, func(i, j int) bool {
164-
return commands[i].Use < commands[j].Use
164+
deprecatedI := commands[i].Deprecated != ""
165+
deprecatedJ := commands[j].Deprecated != ""
166+
if deprecatedI == deprecatedJ {
167+
return commands[i].Use < commands[j].Use
168+
}
169+
return !deprecatedI && deprecatedJ
165170
})
166171
return commands
167172
}

internal/core/command.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ type Command struct {
9393

9494
// Groups contains a list of groups IDs
9595
Groups []string
96+
//
97+
Deprecated bool
9698
}
9799

98100
// CommandPreValidateFunc allows to manipulate args before validation.
@@ -289,7 +291,6 @@ func (c *Commands) find(path ...string) (*Command, bool) {
289291
func (c *Commands) GetSortedCommand() []*Command {
290292
commands := make([]*Command, len(c.commands))
291293
copy(commands, c.commands)
292-
293294
sort.Slice(commands, func(i, j int) bool {
294295
return commands[i].signature() < commands[j].signature()
295296
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
USAGE:
4+
scw instance <command>
5+
6+
AVAILABLE COMMANDS:
7+
a short server
8+
server short server
9+
b short server (Deprecated)
10+
engine short engine (Deprecated)
11+
12+
FLAGS:
13+
-h, --help help for instance
14+
15+
GLOBAL FLAGS:
16+
-c, --config string The path to the config file
17+
-D, --debug Enable debug mode
18+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
19+
-p, --profile string The config profile to use
20+
21+
Use "scw instance [command] --help" for more information about a command.

0 commit comments

Comments
 (0)