add subcommand version

This commit is contained in:
1138-4EB 2018-08-09 08:14:16 +01:00
parent dd1354eed1
commit d1ad79fb9d
2 changed files with 27 additions and 1 deletions

View File

@ -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)")

25
cli/cmd/version.go Normal file
View File

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