Skip to content

Commit 0bee2a0

Browse files
authored
Merge branch 'master' into fixLintDocker
2 parents 263f2ae + 3944b33 commit 0bee2a0

File tree

11 files changed

+433
-424
lines changed

11 files changed

+433
-424
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ linters:
2929
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
3030
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false]
3131
- exptostd # Detects functions from golang.org/x/exp/ that can be replaced by std functions. [auto-fix]
32+
- funcorder # Checks the order of functions, methods, and constructors. [fast]
3233
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false]
3334
- gochecksumtype # Run exhaustiveness checks on Go "sum types" [fast: false, auto-fix: false]
3435
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
@@ -122,6 +123,7 @@ linters:
122123
linters:
123124
- unused
124125
- errcheck
126+
- funcorder
125127

126128
- path: internal/tabwriter/.*_test\.go
127129
linters:

core/autocomplete.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ type FlagSpec struct {
5757
EnumValues []string
5858
}
5959

60-
func (node *AutoCompleteNode) addFlags(flags []FlagSpec) {
61-
for i := range flags {
62-
flag := &flags[i]
63-
node.Children[flag.Name] = NewAutoCompleteFlagNode(node, flag)
64-
}
65-
}
66-
6760
// newAutoCompleteResponse builds a new AutocompleteResponse
6861
func newAutoCompleteResponse(suggestions []string) *AutocompleteResponse {
6962
sort.Strings(suggestions)
@@ -176,6 +169,13 @@ func (node *AutoCompleteNode) GetChildMatch(name string) (*AutoCompleteNode, boo
176169
return nil, false
177170
}
178171

172+
func (node *AutoCompleteNode) addFlags(flags []FlagSpec) {
173+
for i := range flags {
174+
flag := &flags[i]
175+
node.Children[flag.Name] = NewAutoCompleteFlagNode(node, flag)
176+
}
177+
}
178+
179179
// isLeafCommand returns true only if n is a node with no child command (namespace, verb, resource) or a positional arg.
180180
// A leaf command can have 2 types of children: arguments or flags
181181
func (node *AutoCompleteNode) isLeafCommand() bool {

core/command.go

Lines changed: 39 additions & 255 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package core
22

33
import (
44
"context"
5-
"fmt"
65
"reflect"
7-
"sort"
86
"strings"
97

108
"github.com/scaleway/scaleway-cli/v2/core/human"
@@ -117,26 +115,6 @@ func (c *Command) Override(builder func(command *Command) *Command) {
117115
*c = *builder(c)
118116
}
119117

120-
func (c *Command) getPath() string {
121-
if c.path != "" {
122-
return c.path
123-
}
124-
path := []string(nil)
125-
if c.Namespace != "" {
126-
path = append(path, c.Namespace)
127-
}
128-
if c.Resource != "" {
129-
path = append(path, c.Resource)
130-
}
131-
if c.Verb != "" {
132-
path = append(path, c.Verb)
133-
}
134-
135-
c.path = strings.Join(path, indexCommandSeparator)
136-
137-
return c.path
138-
}
139-
140118
func (c *Command) GetCommandLine(binaryName string) string {
141119
return strings.Trim(
142120
binaryName+" "+strings.ReplaceAll(c.getPath(), indexCommandSeparator, " "),
@@ -162,24 +140,6 @@ func (c *Command) GetUsage(binaryName string, commands *Commands) string {
162140
return strings.Join(parts, " ")
163141
}
164142

165-
// seeAlsosAsStr returns all See Alsos as a single string
166-
func (c *Command) seeAlsosAsStr() string {
167-
seeAlsos := make([]string, 0, len(c.SeeAlsos))
168-
169-
for _, cmdSeeAlso := range c.SeeAlsos {
170-
short := " # " + cmdSeeAlso.Short
171-
commandStr := " " + cmdSeeAlso.Command
172-
173-
seeAlsoLines := []string{
174-
short,
175-
commandStr,
176-
}
177-
seeAlsos = append(seeAlsos, strings.Join(seeAlsoLines, "\n"))
178-
}
179-
180-
return strings.Join(seeAlsos, "\n\n")
181-
}
182-
183143
// AddInterceptors add one or multiple interceptors to a command.
184144
// These new interceptors will be added after the already present interceptors (if any).
185145
func (c *Command) AddInterceptors(interceptors ...CommandInterceptor) {
@@ -204,145 +164,27 @@ func (c *Command) MatchAlias(alias alias.Alias) bool {
204164
return true
205165
}
206166

207-
// Commands represent a list of CLI commands, with a index to allow searching.
208-
type Commands struct {
209-
commands []*Command
210-
commandIndex map[string]*Command
211-
}
212-
213-
func NewCommands(cmds ...*Command) *Commands {
214-
c := &Commands{
215-
commands: make([]*Command, 0, len(cmds)),
216-
commandIndex: make(map[string]*Command, len(cmds)),
217-
}
218-
219-
for _, cmd := range cmds {
220-
c.Add(cmd)
221-
}
222-
223-
return c
224-
}
225-
226-
func NewCommandsMerge(cmdsList ...*Commands) *Commands {
227-
cmdCount := 0
228-
for _, cmds := range cmdsList {
229-
cmdCount += len(cmds.commands)
230-
}
231-
c := &Commands{
232-
commands: make([]*Command, 0, cmdCount),
233-
commandIndex: make(map[string]*Command, cmdCount),
234-
}
235-
for _, cmds := range cmdsList {
236-
for _, cmd := range cmds.commands {
237-
c.Add(cmd)
238-
}
239-
}
240-
241-
return c
242-
}
243-
244-
func (c *Commands) MustFind(path ...string) *Command {
245-
cmd, exist := c.find(path...)
246-
if exist {
247-
return cmd
248-
}
249-
250-
panic(fmt.Errorf("command %v not found", strings.Join(path, " ")))
251-
}
252-
253-
func (c *Commands) Find(path ...string) *Command {
254-
cmd, exist := c.find(path...)
255-
if exist {
256-
return cmd
257-
}
258-
259-
return nil
260-
}
261-
262-
func (c *Commands) Remove(namespace, verb string) {
263-
for i := range c.commands {
264-
if c.commands[i].Namespace == namespace && c.commands[i].Verb == verb {
265-
c.commands = append(c.commands[:i], c.commands[i+1:]...)
266-
267-
return
268-
}
269-
}
270-
}
271-
272-
func (c *Commands) RemoveResource(namespace, resource string) {
273-
for i := range c.commands {
274-
if c.commands[i].Namespace == namespace && c.commands[i].Resource == resource &&
275-
c.commands[i].Verb == "" {
276-
c.commands = append(c.commands[:i], c.commands[i+1:]...)
277-
278-
return
279-
}
280-
}
281-
}
282-
283-
func (c *Commands) Add(cmd *Command) {
284-
c.commands = append(c.commands, cmd)
285-
c.commandIndex[cmd.getPath()] = cmd
286-
}
287-
288-
func (c *Commands) Merge(cmds *Commands) {
289-
for _, cmd := range cmds.commands {
290-
c.Add(cmd)
291-
}
292-
}
293-
294-
func (c *Commands) MergeAll(cmds ...*Commands) {
295-
for _, command := range cmds {
296-
c.Merge(command)
167+
// Copy returns a copy of a command
168+
func (c *Command) Copy() *Command {
169+
newCommand := *c
170+
newCommand.Aliases = append([]string(nil), c.Aliases...)
171+
newCommand.Examples = make([]*Example, len(c.Examples))
172+
for i := range c.Examples {
173+
e := *c.Examples[i]
174+
newCommand.Examples[i] = &e
297175
}
298-
}
299-
300-
func (c *Commands) GetAll() []*Command {
301-
return c.commands
302-
}
303-
304-
// find must take the command path, eg. find("instance","get","server")
305-
func (c *Commands) find(path ...string) (*Command, bool) {
306-
cmd, exist := c.commandIndex[strings.Join(path, indexCommandSeparator)]
307-
if exist {
308-
return cmd, true
176+
newCommand.SeeAlsos = make([]*SeeAlso, len(c.SeeAlsos))
177+
for i := range c.SeeAlsos {
178+
sa := *c.SeeAlsos[i]
179+
newCommand.SeeAlsos[i] = &sa
309180
}
310181

311-
return nil, false
312-
}
313-
314-
// GetSortedCommand returns a slice of commands sorted alphabetically
315-
func (c *Commands) GetSortedCommand() []*Command {
316-
commands := make([]*Command, len(c.commands))
317-
copy(commands, c.commands)
318-
sort.Slice(commands, func(i, j int) bool {
319-
return commands[i].signature() < commands[j].signature()
320-
})
321-
322-
return commands
182+
return &newCommand
323183
}
324184

325-
func (c *Commands) HasSubCommands(cmd *Command) bool {
326-
if cmd.Namespace != "" && cmd.Resource != "" && cmd.Verb != "" {
327-
return false
328-
}
329-
if cmd.Namespace == "" && cmd.Resource == "" && cmd.Verb == "" {
330-
return true
331-
}
332-
for _, command := range c.commands {
333-
if command == cmd {
334-
continue
335-
}
336-
if cmd.Resource == "" && cmd.Namespace == command.Namespace {
337-
return true
338-
}
339-
if cmd.Verb == "" && cmd.Namespace == command.Namespace &&
340-
cmd.Resource == command.Resource {
341-
return true
342-
}
343-
}
344-
345-
return false
185+
// get a signature to sort commands
186+
func (c *Command) signature() string {
187+
return c.Namespace + " " + c.Resource + " " + c.Verb + " " + c.Short
346188
}
347189

348190
func (c *Command) getHumanMarshalerOpt() *human.MarshalOpt {
@@ -353,98 +195,40 @@ func (c *Command) getHumanMarshalerOpt() *human.MarshalOpt {
353195
return nil
354196
}
355197

356-
// get a signature to sort commands
357-
func (c *Command) signature() string {
358-
return c.Namespace + " " + c.Resource + " " + c.Verb + " " + c.Short
359-
}
360-
361-
// AliasIsValidCommandChild returns true is alias is a valid child command of given command
362-
// Useful for this case:
363-
// isl => instance server list
364-
// valid child of "instance"
365-
// invalid child of "rdb instance"
366-
func (c *Commands) AliasIsValidCommandChild(command *Command, alias alias.Alias) bool {
367-
// if alias is of size one, it means it cannot be a child
368-
if len(alias.Command) == 1 {
369-
return true
370-
}
371-
372-
// if command is verb, it cannot have children
373-
if command.Verb != "" {
374-
return true
375-
}
198+
// seeAlsosAsStr returns all See Alsos as a single string
199+
func (c *Command) seeAlsosAsStr() string {
200+
seeAlsos := make([]string, 0, len(c.SeeAlsos))
376201

377-
// if command is a resource, check command with alias' verb
378-
if command.Resource != "" {
379-
return c.Find(command.Namespace, command.Resource, alias.Command[1]) != nil
380-
}
202+
for _, cmdSeeAlso := range c.SeeAlsos {
203+
short := " # " + cmdSeeAlso.Short
204+
commandStr := " " + cmdSeeAlso.Command
381205

382-
// if command is a namespace, check for alias' verb or resource
383-
if command.Namespace != "" {
384-
if len(alias.Command) > 2 {
385-
return c.Find(command.Namespace, alias.Command[1], alias.Command[2]) != nil
206+
seeAlsoLines := []string{
207+
short,
208+
commandStr,
386209
}
387-
388-
return c.Find(command.Namespace, alias.Command[1]) != nil
210+
seeAlsos = append(seeAlsos, strings.Join(seeAlsoLines, "\n"))
389211
}
390212

391-
return false
213+
return strings.Join(seeAlsos, "\n\n")
392214
}
393215

394-
// addAliases add valid aliases to a command
395-
func (c *Commands) addAliases(command *Command, aliases []alias.Alias) {
396-
names := make([]string, 0, len(aliases))
397-
for i := range aliases {
398-
if c.AliasIsValidCommandChild(command, aliases[i]) && command.MatchAlias(aliases[i]) {
399-
names = append(names, aliases[i].Name)
400-
}
216+
func (c *Command) getPath() string {
217+
if c.path != "" {
218+
return c.path
401219
}
402-
command.Aliases = append(command.Aliases, names...)
403-
}
404-
405-
// applyAliases add resource aliases to each commands
406-
func (c *Commands) applyAliases(config *alias.Config) {
407-
for _, command := range c.commands {
408-
aliases := []alias.Alias(nil)
409-
exists := false
410-
switch {
411-
case command.Verb != "":
412-
aliases, exists = config.ResolveAliasesByFirstWord(command.Verb)
413-
case command.Resource != "":
414-
aliases, exists = config.ResolveAliasesByFirstWord(command.Resource)
415-
case command.Namespace != "":
416-
aliases, exists = config.ResolveAliasesByFirstWord(command.Namespace)
417-
}
418-
if exists {
419-
c.addAliases(command, aliases)
420-
}
220+
path := []string(nil)
221+
if c.Namespace != "" {
222+
path = append(path, c.Namespace)
421223
}
422-
}
423-
424-
// Copy returns a copy of a command
425-
func (c *Command) Copy() *Command {
426-
newCommand := *c
427-
newCommand.Aliases = append([]string(nil), c.Aliases...)
428-
newCommand.Examples = make([]*Example, len(c.Examples))
429-
for i := range c.Examples {
430-
e := *c.Examples[i]
431-
newCommand.Examples[i] = &e
224+
if c.Resource != "" {
225+
path = append(path, c.Resource)
432226
}
433-
newCommand.SeeAlsos = make([]*SeeAlso, len(c.SeeAlsos))
434-
for i := range c.SeeAlsos {
435-
sa := *c.SeeAlsos[i]
436-
newCommand.SeeAlsos[i] = &sa
227+
if c.Verb != "" {
228+
path = append(path, c.Verb)
437229
}
438230

439-
return &newCommand
440-
}
441-
442-
// Copy return a copy of all commands
443-
func (c *Commands) Copy() *Commands {
444-
newCommands := make([]*Command, len(c.commands))
445-
for i := range c.commands {
446-
newCommands[i] = c.commands[i].Copy()
447-
}
231+
c.path = strings.Join(path, indexCommandSeparator)
448232

449-
return NewCommands(newCommands...)
233+
return c.path
450234
}

0 commit comments

Comments
 (0)