enable version subcmd and --version flag for root cmd and all subcmds

Former-commit-id: a9e681d47d47f9f09a22d8479705f62672f509b7 [formerly b7d17db57156675b06897bc289fe616628a47abb] [formerly e06d3b13b547356fb928288b09aaaaf1a60b8950 [formerly b4708348c6]]
Former-commit-id: 43b18221e14269bada106e867b666f67a692c992 [formerly bee15f87fc2ecf5e8b4a0d74f41bde7617aef12a]
Former-commit-id: 733aaa6f3d5e3fc80dbb65ccc028d9f6ff6f73b7
pull/726/head
1138-4EB 2019-01-06 22:34:56 +01:00
parent b660404bfe
commit 6712fa580b
8 changed files with 51 additions and 28 deletions

View File

@ -12,6 +12,7 @@ func init() {
var cmdsCmd = &cobra.Command{ var cmdsCmd = &cobra.Command{
Use: "cmds", Use: "cmds",
Version: rootCmd.Version,
Short: "Command runner management utility", Short: "Command runner management utility",
Long: `Command runner management utility.`, Long: `Command runner management utility.`,
Args: cobra.NoArgs, Args: cobra.NoArgs,

View File

@ -21,6 +21,7 @@ func init() {
var configCmd = &cobra.Command{ var configCmd = &cobra.Command{
Use: "config", Use: "config",
Version: rootCmd.Version,
Short: "Configuration management utility", Short: "Configuration management utility",
Long: `Configuration management utility.`, Long: `Configuration management utility.`,
Args: cobra.NoArgs, Args: cobra.NoArgs,

View File

@ -13,6 +13,7 @@ func init() {
var hashCmd = &cobra.Command{ var hashCmd = &cobra.Command{
Use: "hash <password>", Use: "hash <password>",
Version: rootCmd.Version,
Short: "Hashes a password", Short: "Hashes a password",
Long: `Hashes a password using bcrypt algorithm.`, Long: `Hashes a password using bcrypt algorithm.`,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),

View File

@ -16,7 +16,8 @@ import (
"github.com/filebrowser/filebrowser/v2/settings" "github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage" "github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users" "github.com/filebrowser/filebrowser/v2/users"
"github.com/mitchellh/go-homedir" "github.com/filebrowser/filebrowser/v2/version"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
v "github.com/spf13/viper" v "github.com/spf13/viper"
@ -29,6 +30,9 @@ var (
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
rootCmd.SetVersionTemplate("File Browser version {{printf \"%s\" .Version}}\n")
flags := rootCmd.Flags() flags := rootCmd.Flags()
persistent := rootCmd.PersistentFlags() persistent := rootCmd.PersistentFlags()
@ -89,6 +93,7 @@ func mustGetStringViperFlag(flags *pflag.FlagSet, key string) string {
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "filebrowser", Use: "filebrowser",
Short: "A stylish web-based file browser", Short: "A stylish web-based file browser",
Version: version.Version,
Long: `File Browser CLI lets you create the database to use with File Browser, Long: `File Browser CLI lets you create the database to use with File Browser,
manage your users and all the configurations without acessing the manage your users and all the configurations without acessing the
web interface. web interface.

View File

@ -19,6 +19,7 @@ func init() {
var rulesCmd = &cobra.Command{ var rulesCmd = &cobra.Command{
Use: "rules", Use: "rules",
Version: rootCmd.Version,
Short: "Rules management utility", Short: "Rules management utility",
Long: `On each subcommand you'll have available at least two flags: Long: `On each subcommand you'll have available at least two flags:
"username" and "id". You must either set only one of them "username" and "id". You must either set only one of them

View File

@ -15,6 +15,7 @@ func init() {
var upgradeCmd = &cobra.Command{ var upgradeCmd = &cobra.Command{
Use: "upgrade", Use: "upgrade",
Version: rootCmd.Version,
Short: "Upgrades an old configuration", Short: "Upgrades an old configuration",
Long: `Upgrades an old configuration. This command DOES NOT Long: `Upgrades an old configuration. This command DOES NOT
import share links because they are incompatible with import share links because they are incompatible with

View File

@ -19,6 +19,7 @@ func init() {
var usersCmd = &cobra.Command{ var usersCmd = &cobra.Command{
Use: "users", Use: "users",
Version: rootCmd.Version,
Short: "Users management utility", Short: "Users management utility",
Long: `Users management utility.`, Long: `Users management utility.`,
Args: cobra.NoArgs, Args: cobra.NoArgs,

View File

@ -1,20 +1,32 @@
package cmd package cmd
import ( import (
"fmt" "text/template"
"github.com/filebrowser/filebrowser/v2/version"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
cmdsCmd.AddCommand(versionCmd)
configCmd.AddCommand(versionCmd)
hashCmd.AddCommand(versionCmd)
upgradeCmd.AddCommand(versionCmd)
rulesCmd.AddCommand(versionCmd)
usersCmd.AddCommand(versionCmd)
} }
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
Short: "Print the version number", 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) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("File Browser Version " + version.Version) // 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)
}
}, },
} }