filebrowser/cmd/utils.go
Henrique Dias 42134a4849 feat: cleanup cli
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

Former-commit-id: 2923c251335301f361098890bf67d47cd58c0f86 [formerly 277653e21c4b9077ea3b83c149f0c5b2982b694f] [formerly 7481557b6f47c8de6499f3ee7339ea8d29216700 [formerly 999c69de5c]]
Former-commit-id: 65eaacb4aee11f84c9f97ca2834def045921202a [formerly 7c9260a1fe142258dd0ac02c4710b03badd66d76]
Former-commit-id: ff3f3d7189b2e8cc2f00bef581cba108780e4552
2019-01-06 15:26:46 +00:00

86 lines
1.6 KiB
Go

package cmd
import (
"crypto/rand"
"errors"
"os"
"github.com/asdine/storm"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/storage/bolt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
v "github.com/spf13/viper"
)
func vaddP(f *pflag.FlagSet, k, p string, i interface{}, u string) {
switch y := i.(type) {
case bool:
f.BoolP(k, p, y, u)
case int:
f.IntP(k, p, y, u)
case string:
f.StringP(k, p, y, u)
}
v.SetDefault(k, i)
}
func vadd(f *pflag.FlagSet, k string, i interface{}, u string) {
switch y := i.(type) {
case bool:
f.Bool(k, y, u)
case int:
f.Int(k, y, u)
case string:
f.String(k, y, u)
}
v.SetDefault(k, i)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func mustGetString(cmd *cobra.Command, flag string) string {
s, err := cmd.Flags().GetString(flag)
checkErr(err)
return s
}
func mustGetBool(cmd *cobra.Command, flag string) bool {
b, err := cmd.Flags().GetBool(flag)
checkErr(err)
return b
}
func mustGetUint(cmd *cobra.Command, flag string) uint {
b, err := cmd.Flags().GetUint(flag)
checkErr(err)
return b
}
func getDB() *storm.DB {
databasePath := v.GetString("database")
if _, err := os.Stat(databasePath); err != nil {
panic(errors.New(databasePath + " does not exist. Please run 'filebrowser init' first."))
}
db, err := storm.Open(databasePath)
checkErr(err)
return db
}
func getStorage(db *storm.DB) *storage.Storage {
return bolt.NewStorage(db)
}
func generateRandomBytes(n int) []byte {
b := make([]byte, n)
_, err := rand.Read(b)
checkErr(err)
// Note that err == nil only if we read len(b) bytes.
return b
}