[v11.0.x] Server: Fix 'server' subcommand double-registration (#86126)

Server: Fix 'server' subcommand double-registration (#86083)

fix 'server' subcommand double-registration

Signed-off-by: Dave Henderson <dave.henderson@grafana.com>
(cherry picked from commit 44adfea049)

Co-authored-by: Dave Henderson <dave.henderson@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-04-15 09:35:33 -04:00
committed by GitHub
co-authored by Dave Henderson
parent 9caf8f6fff
commit aaf233faac
2 changed files with 28 additions and 9 deletions
+12 -9
View File
@@ -20,7 +20,18 @@ var buildBranch = "main"
var buildstamp string
func main() {
app := &cli.App{
app := MainApp()
if err := app.Run(os.Args); err != nil {
fmt.Printf("%s: %s %s\n", color.RedString("Error"), color.RedString("✗"), err)
os.Exit(1)
}
os.Exit(0)
}
func MainApp() *cli.App {
return &cli.App{
Name: "grafana",
Usage: "Grafana server and command line interface",
Authors: []*cli.Author{
@@ -52,18 +63,10 @@ func main() {
return nil
},
},
gsrv.ServerCommand(version, commit, enterpriseCommit, buildBranch, buildstamp),
},
CommandNotFound: cmdNotFound,
EnableBashCompletion: true,
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("%s: %s %s\n", color.RedString("Error"), color.RedString("✗"), err)
os.Exit(1)
}
os.Exit(0)
}
func cmdNotFound(c *cli.Context, command string) {
+16
View File
@@ -0,0 +1,16 @@
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestMainApp_NoDuplicateSubcommands(t *testing.T) {
app := MainApp()
found := map[string]bool{}
for _, cmd := range app.Commands {
require.False(t, found[cmd.Name], "command %q registered twice", cmd.Name)
found[cmd.Name] = true
}
}