diff --git a/cmd/scw-qa/main.go b/cmd/scw-qa/main.go index 182ef4c803..fbb7e70827 100644 --- a/cmd/scw-qa/main.go +++ b/cmd/scw-qa/main.go @@ -2,26 +2,34 @@ package main import ( "fmt" + "os" + "strings" + "github.com/fatih/color" "github.com/scaleway/scaleway-cli/internal/namespaces" "github.com/scaleway/scaleway-cli/internal/qa" + "github.com/scaleway/scaleway-cli/internal/tabwriter" + "github.com/scaleway/scaleway-cli/internal/terminal" ) func main() { commands := namespaces.GetCommands() errors := qa.LintCommands(commands) - errorCounts := map[string]int{} + fmt.Println(terminal.Style("Errors:", color.Bold)) for _, err := range errors { - errorCounts[fmt.Sprintf("%T", err)]++ + fmt.Printf("%v\n", err) } - fmt.Printf("Errors:\n") + errorCounts := map[string]int{} for _, err := range errors { - fmt.Printf("%v\n", err) + errorCounts[fmt.Sprintf("%T", err)]++ } - fmt.Printf("\nSummary:\n") + + fmt.Println(terminal.Style("\nSummary:", color.Bold)) + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) for key, count := range errorCounts { - fmt.Printf("%s: %d\n", key, count) + _, _ = fmt.Fprintf(w, "%s\t%d\n", strings.TrimPrefix(key, "*qa."), count) } + _ = w.Flush() } diff --git a/internal/qa/qa.go b/internal/qa/qa.go index f4cc9430eb..137eb02693 100644 --- a/internal/qa/qa.go +++ b/internal/qa/qa.go @@ -16,6 +16,7 @@ func LintCommands(commands *core.Commands) []interface{} { errors = append(errors, testPositionalArgMustBeRequiredError(commands)...) errors = append(errors, testExampleCanHaveOnlyOneTypeOfExampleError(commands)...) errors = append(errors, testDifferentLocalizationForNamespaceError(commands)...) + errors = append(errors, testDuplicatedCommandError(commands)...) return errors } @@ -140,7 +141,7 @@ type DifferentLocalizationForNamespaceError struct { } func (err DifferentLocalizationForNamespaceError) Error() string { - return fmt.Sprintf("different localization for '%v', '%v': %v, %v", + return fmt.Sprintf("different localization for commands '%v', '%v': %v, %v", err.Command1.GetCommandLine(), err.Command2.GetCommandLine(), err.ArgNames1, err.ArgNames2) } @@ -208,3 +209,30 @@ func testDifferentLocalizationForNamespaceError(commands *core.Commands) []inter } return errors } + +type DuplicatedCommandError struct { + Command *core.Command +} + +func (err DuplicatedCommandError) Error() string { + return fmt.Sprintf("duplicated command '%s'", err.Command.GetCommandLine()) +} + +// testDuplicatedCommandError testes that there is no duplicate command. +func testDuplicatedCommandError(commands *core.Commands) []interface{} { + errors := []interface{}(nil) + uniqueness := make(map[string]bool) + + for _, command := range commands.GetAll() { + key := command.GetCommandLine() + + if uniqueness[key] { + errors = append(errors, &DuplicatedCommandError{Command: command}) + continue + } + + uniqueness[key] = true + } + + return errors +}