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

fix 'server' subcommand double-registration

Signed-off-by: Dave Henderson <dave.henderson@grafana.com>
This commit is contained in:
Dave Henderson
2024-04-15 08:21:03 -04:00
committed by GitHub
parent ad8d505d81
commit 44adfea049
2 changed files with 28 additions and 9 deletions

View File

@@ -20,7 +20,18 @@ var buildBranch = "main"
var buildstamp string var buildstamp string
func main() { 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", Name: "grafana",
Usage: "Grafana server and command line interface", Usage: "Grafana server and command line interface",
Authors: []*cli.Author{ Authors: []*cli.Author{
@@ -52,18 +63,10 @@ func main() {
return nil return nil
}, },
}, },
gsrv.ServerCommand(version, commit, enterpriseCommit, buildBranch, buildstamp),
}, },
CommandNotFound: cmdNotFound, CommandNotFound: cmdNotFound,
EnableBashCompletion: true, 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) { func cmdNotFound(c *cli.Context, command string) {

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
}
}