From d1ad79fb9d06b158c1088cbad57de60c9f0bca73 Mon Sep 17 00:00:00 2001 From: 1138-4EB <1138-4EB@users.noreply.github.com> Date: Thu, 9 Aug 2018 08:14:16 +0100 Subject: [PATCH] add subcommand version --- cli/cmd/root.go | 3 ++- cli/cmd/version.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 cli/cmd/version.go diff --git a/cli/cmd/root.go b/cli/cmd/root.go index a8fddc5b..1c128175 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -14,7 +14,7 @@ var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "filebrowser", - Version: "someversion", + Version: "(untracked)", Short: "A stylish web-based file manager", Long: `File Browser is static binary composed of a golang backend and a Vue.js frontend to create, edit, copy, move, download your files @@ -37,6 +37,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) + rootCmd.SetVersionTemplate("File Browser {{printf \"version %s\" .Version}}\n") rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cli.yaml)") diff --git a/cli/cmd/version.go b/cli/cmd/version.go new file mode 100644 index 00000000..22eea14f --- /dev/null +++ b/cli/cmd/version.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "text/template" + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of File Browser", + Long: `All software has versions. This is File Browser's`, + Run: func(cmd *cobra.Command, args []string) { + // https://github.com/spf13/cobra/issues/724 + t := template.New("version") + template.Must(t.Parse(rootCmd.VersionTemplate())) + err := t.Execute(rootCmd.OutOrStdout(), rootCmd) + if err != nil { + rootCmd.Println(err) + } + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +}