2019-01-08 08:57:24 +00:00
package cmd
import (
"encoding/json"
"errors"
"reflect"
2020-06-01 01:12:36 +02:00
"github.com/spf13/cobra"
2019-01-08 08:57:24 +00:00
"github.com/filebrowser/filebrowser/v2/auth"
"github.com/filebrowser/filebrowser/v2/settings"
)
func init () {
configCmd . AddCommand ( configImportCmd )
}
type settingsFile struct {
Settings * settings . Settings `json:"settings"`
2019-01-08 14:07:55 +00:00
Server * settings . Server `json:"server"`
2019-01-08 08:57:24 +00:00
Auther interface {} `json:"auther"`
}
var configImportCmd = & cobra . Command {
2019-01-08 16:37:02 +00:00
Use : "import <path>" ,
2019-01-08 16:09:25 +00:00
Short : "Import a configuration file" ,
Long : `Import a configuration file. This will replace all the existing
2019-01-08 08:57:24 +00:00
configuration. Can be used with or without unexisting databases.
2019-01-08 16:37:02 +00:00
2019-01-08 08:57:24 +00:00
If used with a nonexisting database, a key will be generated
automatically. Otherwise the key will be kept the same as in the
2019-01-08 16:37:02 +00:00
database.
The path must be for a json or yaml file.` ,
2019-01-08 08:57:24 +00:00
Args : jsonYamlArg ,
2025-11-18 11:29:28 +01:00
RunE : withStore ( func ( _ * cobra . Command , args [] string , st * store ) error {
2019-01-08 08:57:24 +00:00
var key [] byte
2025-07-22 11:55:21 +05:30
var err error
2025-11-18 11:29:28 +01:00
if st . databaseExisted {
settings , settingErr := st . Settings . Get ()
2025-07-22 11:55:21 +05:30
if settingErr != nil {
return settingErr
}
2019-01-08 08:57:24 +00:00
key = settings . Key
} else {
2019-01-11 20:25:39 +00:00
key = generateKey ()
2019-01-08 08:57:24 +00:00
}
file := settingsFile {}
2025-07-22 11:55:21 +05:30
err = unmarshal ( args [ 0 ], & file )
if err != nil {
return err
}
2019-01-08 08:57:24 +00:00
2025-12-28 21:04:23 +00:00
if file . Settings == nil || file . Server == nil {
return errors . New ( "invalid configuration file: 'settings' or 'server' fields are missing. Please ensure you are importing a file generated by the 'config export' command" )
}
2019-01-08 08:57:24 +00:00
file . Settings . Key = key
2025-11-18 11:29:28 +01:00
err = st . Settings . Save ( file . Settings )
2025-07-22 11:55:21 +05:30
if err != nil {
return err
}
2019-01-08 08:57:24 +00:00
2025-11-18 11:29:28 +01:00
err = st . Settings . SaveServer ( file . Server )
2025-07-22 11:55:21 +05:30
if err != nil {
return err
}
2019-01-08 14:07:55 +00:00
2026-01-31 01:35:47 -05:00
var rawAuther = file . Auther
2019-01-08 08:57:24 +00:00
var auther auth . Auther
2025-07-22 11:55:21 +05:30
var autherErr error
2019-01-08 08:57:24 +00:00
switch file . Settings . AuthMethod {
case auth . MethodJSONAuth :
2025-07-22 11:55:21 +05:30
var a interface {}
a , autherErr = getAuther ( auth . JSONAuth {}, rawAuther )
auther = a .( * auth . JSONAuth )
2019-01-08 08:57:24 +00:00
case auth . MethodNoAuth :
2025-07-22 11:55:21 +05:30
var a interface {}
a , autherErr = getAuther ( auth . NoAuth {}, rawAuther )
auther = a .( * auth . NoAuth )
2019-01-08 08:57:24 +00:00
case auth . MethodProxyAuth :
2025-07-22 11:55:21 +05:30
var a interface {}
a , autherErr = getAuther ( auth . ProxyAuth {}, rawAuther )
auther = a .( * auth . ProxyAuth )
2021-09-13 13:47:06 +00:00
case auth . MethodHookAuth :
2025-07-22 11:55:21 +05:30
var a interface {}
a , autherErr = getAuther ( & auth . HookAuth {}, rawAuther )
auther = a .( * auth . HookAuth )
2019-01-08 08:57:24 +00:00
default :
2025-07-22 11:55:21 +05:30
return errors . New ( "invalid auth method" )
}
if autherErr != nil {
return autherErr
2019-01-08 08:57:24 +00:00
}
2025-11-18 11:29:28 +01:00
err = st . Auth . Save ( auther )
2025-07-22 11:55:21 +05:30
if err != nil {
return err
}
2019-01-08 14:07:55 +00:00
2025-07-22 11:55:21 +05:30
return printSettings ( file . Server , file . Settings , auther )
2025-11-18 11:29:28 +01:00
}, storeOptions { allowsNoDatabase : true }),
2019-01-08 08:57:24 +00:00
}
2025-07-22 11:55:21 +05:30
func getAuther ( sample auth . Auther , data interface {}) ( interface {}, error ) {
2019-01-08 08:57:24 +00:00
authType := reflect . TypeOf ( sample )
auther := reflect . New ( authType ). Interface ()
bytes , err := json . Marshal ( data )
2025-07-22 11:55:21 +05:30
if err != nil {
return nil , err
}
2019-01-08 08:57:24 +00:00
err = json . Unmarshal ( bytes , & auther )
2025-07-22 11:55:21 +05:30
if err != nil {
return nil , err
}
return auther , nil
2019-01-08 08:57:24 +00:00
}