From 2c184edb05c1d1f567977730d680ab8249340031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Fri, 30 Mar 2018 11:13:18 -0400 Subject: lxc/main: Add version subcommand MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #4381 Closes #4382 Signed-off-by: Stéphane Graber --- lxc/main.go | 4 ++++ lxc/version.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 lxc/version.go diff --git a/lxc/main.go b/lxc/main.go index fa76d276..70abc681 100644 --- a/lxc/main.go +++ b/lxc/main.go @@ -189,6 +189,10 @@ For help with any of those, simply call them with --help.`)) stopCmd := cmdStop{global: &globalCmd} app.AddCommand(stopCmd.Command()) + // version sub-command + versionCmd := cmdVersion{global: &globalCmd} + app.AddCommand(versionCmd.Command()) + // Deal with --all flag err := app.ParseFlags(os.Args[1:]) if err == nil { diff --git a/lxc/version.go b/lxc/version.go new file mode 100644 index 00000000..a4a01790 --- /dev/null +++ b/lxc/version.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" + + cli "github.com/lxc/lxd/shared/cmd" + "github.com/lxc/lxd/shared/i18n" + "github.com/lxc/lxd/shared/version" +) + +type cmdVersion struct { + global *cmdGlobal +} + +func (c *cmdVersion) Command() *cobra.Command { + cmd := &cobra.Command{} + cmd.Use = i18n.G("version [:]") + cmd.Short = i18n.G("Show local and remote versions") + cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G( + `Show local and remote versions`)) + + cmd.RunE = c.Run + + return cmd +} + +func (c *cmdVersion) Run(cmd *cobra.Command, args []string) error { + // Sanity checks + exit, err := c.global.CheckArgs(cmd, args, 0, 1) + if exit { + return err + } + + fmt.Printf(i18n.G("Client version: %s\n"), version.Version) + + // Remote version + remote := "" + if len(args) == 1 { + remote = args[0] + if !strings.HasSuffix(remote, ":") { + remote = remote + ":" + } + } + + version := i18n.G("unreachable") + resources, err := c.global.ParseServers(remote) + if err == nil { + resource := resources[0] + info, _, err := resource.server.GetServer() + if err == nil { + version = info.Environment.ServerVersion + } + } + + fmt.Printf(i18n.G("Server version: %s\n"), version) + + return nil +}