feat: also suppport yaml
License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
parent
fff79d3d30
commit
e0276e84f0
@ -22,7 +22,7 @@ type jsonCred struct {
|
|||||||
|
|
||||||
// JSONAuth is a json implementaion of an Auther.
|
// JSONAuth is a json implementaion of an Auther.
|
||||||
type JSONAuth struct {
|
type JSONAuth struct {
|
||||||
ReCaptcha *ReCaptcha
|
ReCaptcha *ReCaptcha `json:"recaptcha" yaml:"recaptcha"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth authenticates the user via a json in content body.
|
// Auth authenticates the user via a json in content body.
|
||||||
|
|||||||
@ -14,7 +14,7 @@ const MethodProxyAuth settings.AuthMethod = "proxy"
|
|||||||
|
|
||||||
// ProxyAuth is a proxy implementation of an auther.
|
// ProxyAuth is a proxy implementation of an auther.
|
||||||
type ProxyAuth struct {
|
type ProxyAuth struct {
|
||||||
Header string
|
Header string `json:"header"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth authenticates the user via an HTTP header.
|
// Auth authenticates the user via an HTTP header.
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,7 +11,7 @@ func init() {
|
|||||||
var configExportCmd = &cobra.Command{
|
var configExportCmd = &cobra.Command{
|
||||||
Use: "export <filename>",
|
Use: "export <filename>",
|
||||||
Short: "Export the configuration to a file.",
|
Short: "Export the configuration to a file.",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: jsonYamlArg,
|
||||||
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
||||||
settings, err := d.store.Settings.Get()
|
settings, err := d.store.Settings.Get()
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
@ -22,15 +19,12 @@ var configExportCmd = &cobra.Command{
|
|||||||
auther, err := d.store.Auth.Get(settings.AuthMethod)
|
auther, err := d.store.Auth.Get(settings.AuthMethod)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
fd, err := os.Create(args[0])
|
data := &settingsFile{
|
||||||
checkErr(err)
|
|
||||||
defer fd.Close()
|
|
||||||
|
|
||||||
encoder := json.NewEncoder(fd)
|
|
||||||
encoder.SetIndent("", " ")
|
|
||||||
encoder.Encode(&settingsFile{
|
|
||||||
Settings: settings,
|
Settings: settings,
|
||||||
Auther: auther,
|
Auther: auther,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
err = marshal(args[0], data)
|
||||||
|
checkErr(err)
|
||||||
}, pythonConfig{}),
|
}, pythonConfig{}),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/filebrowser/filebrowser/v2/auth"
|
"github.com/filebrowser/filebrowser/v2/auth"
|
||||||
@ -27,7 +26,7 @@ configuration. Can be used with or without unexisting databases.
|
|||||||
If used with a nonexisting database, a key will be generated
|
If used with a nonexisting database, a key will be generated
|
||||||
automatically. Otherwise the key will be kept the same as in the
|
automatically. Otherwise the key will be kept the same as in the
|
||||||
database.`,
|
database.`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: jsonYamlArg,
|
||||||
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
||||||
var key []byte
|
var key []byte
|
||||||
if d.hadDB {
|
if d.hadDB {
|
||||||
@ -38,27 +37,24 @@ database.`,
|
|||||||
key = generateRandomBytes(64)
|
key = generateRandomBytes(64)
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := os.Open(args[0])
|
|
||||||
checkErr(err)
|
|
||||||
defer fd.Close()
|
|
||||||
|
|
||||||
file := settingsFile{}
|
file := settingsFile{}
|
||||||
err = json.NewDecoder(fd).Decode(&file)
|
err := unmarshal(args[0], &file)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
file.Settings.Key = key
|
file.Settings.Key = key
|
||||||
|
|
||||||
err = d.store.Settings.Save(file.Settings)
|
err = d.store.Settings.Save(file.Settings)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
|
autherInterf := cleanUpInterfaceMap(file.Auther.(map[interface{}]interface{}))
|
||||||
|
|
||||||
var auther auth.Auther
|
var auther auth.Auther
|
||||||
switch file.Settings.AuthMethod {
|
switch file.Settings.AuthMethod {
|
||||||
case auth.MethodJSONAuth:
|
case auth.MethodJSONAuth:
|
||||||
auther = getAuther(auth.JSONAuth{}, file.Auther).(*auth.JSONAuth)
|
auther = getAuther(auth.JSONAuth{}, autherInterf).(*auth.JSONAuth)
|
||||||
case auth.MethodNoAuth:
|
case auth.MethodNoAuth:
|
||||||
auther = getAuther(auth.NoAuth{}, file.Auther).(*auth.NoAuth)
|
auther = getAuther(auth.NoAuth{}, autherInterf).(*auth.NoAuth)
|
||||||
case auth.MethodProxyAuth:
|
case auth.MethodProxyAuth:
|
||||||
auther = getAuther(auth.ProxyAuth{}, file.Auther).(*auth.ProxyAuth)
|
auther = getAuther(auth.ProxyAuth{}, autherInterf).(*auth.ProxyAuth)
|
||||||
default:
|
default:
|
||||||
checkErr(errors.New("invalid auth method"))
|
checkErr(errors.New("invalid auth method"))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,17 +11,12 @@ func init() {
|
|||||||
var usersExportCmd = &cobra.Command{
|
var usersExportCmd = &cobra.Command{
|
||||||
Use: "export <filename>",
|
Use: "export <filename>",
|
||||||
Short: "Export all users.",
|
Short: "Export all users.",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: jsonYamlArg,
|
||||||
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
||||||
list, err := d.store.Users.Gets("")
|
list, err := d.store.Users.Gets("")
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
fd, err := os.Create(args[0])
|
err = marshal(args[0], list)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
defer fd.Close()
|
|
||||||
|
|
||||||
encoder := json.NewEncoder(fd)
|
|
||||||
encoder.SetIndent("", " ")
|
|
||||||
encoder.Encode(list)
|
|
||||||
}, pythonConfig{}),
|
}, pythonConfig{}),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -18,14 +17,14 @@ func init() {
|
|||||||
var usersImportCmd = &cobra.Command{
|
var usersImportCmd = &cobra.Command{
|
||||||
Use: "import <filename>",
|
Use: "import <filename>",
|
||||||
Short: "Import users from a file.",
|
Short: "Import users from a file.",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: jsonYamlArg,
|
||||||
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
||||||
fd, err := os.Open(args[0])
|
fd, err := os.Open(args[0])
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
|
|
||||||
list := []*users.User{}
|
list := []*users.User{}
|
||||||
err = json.NewDecoder(fd).Decode(&list)
|
err = unmarshal(args[0], &list)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
for _, user := range list {
|
for _, user := range list {
|
||||||
|
|||||||
77
cmd/utils.go
77
cmd/utils.go
@ -2,9 +2,12 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/asdine/storm"
|
"github.com/asdine/storm"
|
||||||
"github.com/filebrowser/filebrowser/v2/storage"
|
"github.com/filebrowser/filebrowser/v2/storage"
|
||||||
@ -12,6 +15,7 @@ import (
|
|||||||
"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"
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func vaddP(f *pflag.FlagSet, k, p string, i interface{}, u string) {
|
func vaddP(f *pflag.FlagSet, k, p string, i interface{}, u string) {
|
||||||
@ -110,3 +114,76 @@ func python(fn pythonFunc, cfg pythonConfig) cobraFunc {
|
|||||||
fn(cmd, args, data)
|
fn(cmd, args, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -31,7 +31,7 @@ type User struct {
|
|||||||
Perm Permissions `json:"perm"`
|
Perm Permissions `json:"perm"`
|
||||||
Commands []string `json:"commands"`
|
Commands []string `json:"commands"`
|
||||||
Sorting files.Sorting `json:"sorting"`
|
Sorting files.Sorting `json:"sorting"`
|
||||||
Fs afero.Fs `json:"-"`
|
Fs afero.Fs `json:"-" yaml:"-"`
|
||||||
Rules []rules.Rule `json:"rules"`
|
Rules []rules.Rule `json:"rules"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user