Merge pull request #618 from filebrowser/chores-version

Former-commit-id: e4a254c445bb6f9e6012800101812847f2f7a165 [formerly 39f8661c8cb8a37dd379bc87685fa266e2c98cc9] [formerly ad43c963e3adce98b64edb3324f9c4912b4a5623 [formerly 58edf878ab]]
Former-commit-id: 66301024e8d6c34070382fb0b9db8e25a1055da1 [formerly cb29ecd9eec4ae6ae0cb422457ee1d794a51df20]
Former-commit-id: d4eff084d7aad39e384ec9cc0cd3227250a195d3
This commit is contained in:
Henrique Dias 2019-01-08 15:55:06 +00:00 committed by GitHub
commit 2e1553542b
11 changed files with 75 additions and 71 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

@ -19,8 +19,7 @@ import (
func checkErr(err error) { func checkErr(err error) {
if err != nil { if err != nil {
fmt.Println(err) log.Fatal(err)
os.Exit(1)
} }
} }

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

View File

@ -12,32 +12,29 @@ type usersBackend struct {
db *storm.DB db *storm.DB
} }
func (st usersBackend) GetByID(id uint) (*users.User, error) { func (st usersBackend) GetBy(i interface{}) (user *users.User, err error) {
user := &users.User{} user = &users.User{}
err := st.db.One("ID", id, user)
var arg string
switch i.(type) {
case uint:
arg = "ID"
case string:
arg = "Username"
default:
return nil, errors.ErrInvalidDataType
}
err = st.db.One(arg, i, user)
if err != nil {
if err == storm.ErrNotFound { if err == storm.ErrNotFound {
return nil, errors.ErrNotExist return nil, errors.ErrNotExist
} }
if err != nil {
return nil, err return nil, err
} }
return user, nil return
}
func (st usersBackend) GetByUsername(username string) (*users.User, error) {
user := &users.User{}
err := st.db.One("Username", username, user)
if err == storm.ErrNotFound {
return nil, errors.ErrNotExist
}
if err != nil {
return nil, err
}
return user, nil
} }
func (st usersBackend) Gets() ([]*users.User, error) { func (st usersBackend) Gets() ([]*users.User, error) {
@ -82,7 +79,7 @@ func (st usersBackend) DeleteByID(id uint) error {
} }
func (st usersBackend) DeleteByUsername(username string) error { func (st usersBackend) DeleteByUsername(username string) error {
user, err := st.GetByUsername(username) user, err := st.GetBy(username)
if err != nil { if err != nil {
return err return err
} }

View File

@ -9,8 +9,7 @@ import (
// StorageBackend is the interface to implement for a users storage. // StorageBackend is the interface to implement for a users storage.
type StorageBackend interface { type StorageBackend interface {
GetByID(uint) (*User, error) GetBy(interface{}) (*User, error)
GetByUsername(string) (*User, error)
Gets() ([]*User, error) Gets() ([]*User, error)
Save(u *User) error Save(u *User) error
Update(u *User, fields ...string) error Update(u *User, fields ...string) error
@ -36,27 +35,13 @@ func NewStorage(back StorageBackend) *Storage {
// Get allows you to get a user by its name or username. The provided // Get allows you to get a user by its name or username. The provided
// id must be a string for username lookup or a uint for id lookup. If id // id must be a string for username lookup or a uint for id lookup. If id
// is neither, a ErrInvalidDataType will be returned. // is neither, a ErrInvalidDataType will be returned.
func (s *Storage) Get(baseScope string, id interface{}) (*User, error) { func (s *Storage) Get(baseScope string, id interface{}) (user *User, err error) {
var ( user, err = s.back.GetBy(id)
user *User
err error
)
switch id.(type) {
case string:
user, err = s.back.GetByUsername(id.(string))
case uint:
user, err = s.back.GetByID(id.(uint))
default:
return nil, errors.ErrInvalidDataType
}
if err != nil { if err != nil {
return nil, err return
} }
user.Clean(baseScope) user.Clean(baseScope)
return user, err return
} }
// Gets gets a list of all users. // Gets gets a list of all users.