2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-01-06 12:26:48 +00:00
|
|
|
"crypto/rand"
|
2019-01-08 08:57:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-01-07 20:24:23 +00:00
|
|
|
"log"
|
2019-01-05 22:44:33 +00:00
|
|
|
"os"
|
2019-01-08 08:57:24 +00:00
|
|
|
"path/filepath"
|
2019-01-05 22:44:33 +00:00
|
|
|
|
|
|
|
"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"
|
2019-01-08 08:57:24 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
2019-01-06 21:06:56 +00:00
|
|
|
log.Fatal(err)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:07:55 +00:00
|
|
|
func mustGetString(flags *pflag.FlagSet, flag string) string {
|
|
|
|
s, err := flags.GetString(flag)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:07:55 +00:00
|
|
|
func mustGetBool(flags *pflag.FlagSet, flag string) bool {
|
|
|
|
b, err := flags.GetBool(flag)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:07:55 +00:00
|
|
|
func mustGetUint(flags *pflag.FlagSet, flag string) uint {
|
|
|
|
b, err := flags.GetUint(flag)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-01-07 20:24:23 +00:00
|
|
|
|
|
|
|
type cobraFunc func(cmd *cobra.Command, args []string)
|
|
|
|
type pythonFunc func(cmd *cobra.Command, args []string, data pythonData)
|
|
|
|
|
|
|
|
type pythonConfig struct {
|
2019-01-07 20:34:44 +00:00
|
|
|
noDB bool
|
|
|
|
allowNoDB bool
|
2019-01-07 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
2019-01-08 20:10:27 +00:00
|
|
|
path := getParam(cmd.Flags(), "database")
|
2019-01-07 20:24:23 +00:00
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
data.hadDB = false
|
|
|
|
|
2019-01-08 10:29:09 +00:00
|
|
|
if !cfg.noDB && !cfg.allowNoDB {
|
|
|
|
log.Fatal(path + " does not exist. Please run 'filebrowser config init' first.")
|
2019-01-07 20:24:23 +00:00
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 08:57:24 +00:00
|
|
|
|
|
|
|
func marshal(filename string, data interface{}) error {
|
|
|
|
fd, err := os.Create(filename)
|
|
|
|
checkErr(err)
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
switch ext := filepath.Ext(filename); ext {
|
|
|
|
case ".json":
|
|
|
|
encoder := json.NewEncoder(fd)
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
return encoder.Encode(data)
|
|
|
|
case ".yml", ".yaml":
|
|
|
|
encoder := yaml.NewEncoder(fd)
|
|
|
|
return encoder.Encode(data)
|
|
|
|
default:
|
|
|
|
return errors.New("invalid format: " + ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshal(filename string, data interface{}) error {
|
|
|
|
fd, err := os.Open(filename)
|
|
|
|
checkErr(err)
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
switch ext := filepath.Ext(filename); ext {
|
|
|
|
case ".json":
|
|
|
|
return json.NewDecoder(fd).Decode(data)
|
|
|
|
case ".yml", ".yaml":
|
|
|
|
return yaml.NewDecoder(fd).Decode(data)
|
|
|
|
default:
|
|
|
|
return errors.New("invalid format: " + ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonYamlArg(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ext := filepath.Ext(args[0]); ext {
|
|
|
|
case ".json", ".yml", ".yaml":
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("invalid format: " + ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpInterfaceMap(in map[interface{}]interface{}) map[string]interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
for k, v := range in {
|
|
|
|
result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpInterfaceArray(in []interface{}) []interface{} {
|
|
|
|
result := make([]interface{}, len(in))
|
|
|
|
for i, v := range in {
|
|
|
|
result[i] = cleanUpMapValue(v)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpMapValue(v interface{}) interface{} {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
return cleanUpInterfaceArray(v)
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
return cleanUpInterfaceMap(v)
|
|
|
|
default:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|