From 8dc59794a797753487fccac6883d0a9dcd0fd062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Wed, 15 Jan 2025 12:58:13 -0500 Subject: [PATCH] incusd/main_cluster: Tweak to have help refer to correct command name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "incusd cluster" is typically called by a re-exec of "incus admin cluster". This was leading to help messages referring to "incusd cluster XYZ" rather than "incus admin cluster XYZ". This commit now adds a bit more logic to detect this situation and correctly alter the command name. Signed-off-by: Stéphane Graber --- cmd/incus/admin_cluster.go | 2 +- cmd/incusd/main.go | 10 +++++++++- cmd/incusd/main_cluster.go | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cmd/incus/admin_cluster.go b/cmd/incus/admin_cluster.go index b03946cd2b4..5b2dca25f1a 100644 --- a/cmd/incus/admin_cluster.go +++ b/cmd/incus/admin_cluster.go @@ -51,5 +51,5 @@ You can invoke it through "incusd cluster".`)) os.Exit(1) } - _ = doExec(path, append([]string{"incusd", "cluster"}, args...), env) + _ = doExec(path, append([]string{"incusd", "admin", "cluster"}, args...), env) } diff --git a/cmd/incusd/main.go b/cmd/incusd/main.go index 4bac62bf386..eb670c1129b 100644 --- a/cmd/incusd/main.go +++ b/cmd/incusd/main.go @@ -85,6 +85,11 @@ func main() { // Workaround for main command app.Args = cobra.ArbitraryArgs + // Workaround for being called through "incus admin cluster". + if len(os.Args) >= 3 && os.Args[0] == "incusd" && os.Args[1] == "admin" && os.Args[2] == "cluster" { + app.Use = "incus" + } + // Global flags globalCmd := cmdGlobal{cmd: app} daemonCmd.global = &globalCmd @@ -185,7 +190,10 @@ func main() { waitreadyCmd := cmdWaitready{global: &globalCmd} app.AddCommand(waitreadyCmd.Command()) - // cluster sub-command + // cluster sub-command (also admin cluster) + adminCmd := cmdAdmin{global: &globalCmd} + app.AddCommand(adminCmd.Command()) + clusterCmd := cmdCluster{global: &globalCmd} app.AddCommand(clusterCmd.Command()) diff --git a/cmd/incusd/main_cluster.go b/cmd/incusd/main_cluster.go index ee3d558cd9b..a45d2903ffd 100644 --- a/cmd/incusd/main_cluster.go +++ b/cmd/incusd/main_cluster.go @@ -28,10 +28,31 @@ import ( "github.com/lxc/incus/v6/shared/termios" ) +type cmdAdmin struct { + global *cmdGlobal +} + +// Command returns a cobra command for inclusion. +func (c *cmdAdmin) Command() *cobra.Command { + cmd := &cobra.Command{} + cmd.Hidden = true + cmd.Use = "admin" + + // Cluster + clusterCmd := cmdCluster{global: c.global} + cmd.AddCommand(clusterCmd.Command()) + + // Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706 + cmd.Args = cobra.NoArgs + cmd.Run = func(cmd *cobra.Command, args []string) { _ = cmd.Usage() } + return cmd +} + type cmdCluster struct { global *cmdGlobal } +// Command returns a cobra command for inclusion. func (c *cmdCluster) Command() *cobra.Command { cmd := &cobra.Command{} cmd.Use = "cluster"