2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-01-06 12:26:48 +00:00
|
|
|
"crypto/rand"
|
2019-01-05 22:44:33 +00:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/asdine/storm"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/storage/bolt"
|
|
|
|
"github.com/spf13/cobra"
|
2019-01-06 05:11:15 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
v "github.com/spf13/viper"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
2019-01-06 05:11:15 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
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 mustGetInt(cmd *cobra.Command, flag string) int {
|
|
|
|
b, err := cmd.Flags().GetInt(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 {
|
2019-01-06 05:11:15 +00:00
|
|
|
databasePath := v.GetString("database")
|
2019-01-05 22:44:33 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-01-06 12:26:48 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|