mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
5a83d6736b
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
Former-commit-id: 856c18bc9cf98a27b6cbea923b231e0aaf279190 [formerly 201c1a0294947930a7d0706af72ce719a8cc3b98] [formerly 0253e57e2994023e798f6fb0ae76f9c21d18fd69 [formerly 33a58c999a
]]
Former-commit-id: e7d88b22207125c29ea85a5a539653a54584999c [formerly 77cb1e0172cdf7195cc25d557e5028a9250d655c]
Former-commit-id: 64288f5a475a82d5f88c91347a09aea67ebb169d
111 lines
2.1 KiB
Go
111 lines
2.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"log"
|
|
"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 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
|
|
}
|
|
|
|
type cobraFunc func(cmd *cobra.Command, args []string)
|
|
type pythonFunc func(cmd *cobra.Command, args []string, data pythonData)
|
|
|
|
type pythonConfig struct {
|
|
noDB bool
|
|
allowNoDB bool
|
|
}
|
|
|
|
type pythonData struct {
|
|
hadDB bool
|
|
store *storage.Storage
|
|
}
|
|
|
|
func python(fn pythonFunc, cfg pythonConfig) cobraFunc {
|
|
return func(cmd *cobra.Command, args []string) {
|
|
data := pythonData{hadDB: true}
|
|
|
|
path := v.GetString("database")
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
data.hadDB = false
|
|
|
|
if !cfg.noDB || !cfg.allowNoDB {
|
|
log.Fatal(path + " does not exid.store. Please run 'filebrowser config init' fird.store.")
|
|
}
|
|
} else if err != nil {
|
|
panic(err)
|
|
} else if err == nil && cfg.noDB {
|
|
log.Fatal(path + " already exists")
|
|
}
|
|
|
|
db, err := storm.Open(path)
|
|
checkErr(err)
|
|
defer db.Close()
|
|
data.store = bolt.NewStorage(db)
|
|
fn(cmd, args, data)
|
|
}
|
|
}
|