2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2020-05-31 23:12:36 +00:00
|
|
|
|
2023-09-03 17:28:00 +00:00
|
|
|
"github.com/gtsteffaniak/filebrowser/auth"
|
|
|
|
"github.com/gtsteffaniak/filebrowser/errors"
|
2023-06-15 01:08:09 +00:00
|
|
|
"github.com/gtsteffaniak/filebrowser/settings"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var configInitCmd = &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize a new database",
|
|
|
|
Long: `Initialize a new database to use with File Browser. All of
|
|
|
|
this options can be changed in the future with the command
|
2019-01-08 16:37:02 +00:00
|
|
|
'filebrowser config set'. The user related flags apply
|
2019-01-05 22:44:33 +00:00
|
|
|
to the defaults when creating new users and you don't
|
|
|
|
override the options.`,
|
|
|
|
Args: cobra.NoArgs,
|
2019-01-07 20:24:23 +00:00
|
|
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
2023-09-02 18:02:50 +00:00
|
|
|
auther := getAuthentication()
|
|
|
|
s := settings.GlobalConfiguration
|
|
|
|
s.Key = generateKey()
|
|
|
|
err := d.store.Settings.Save(&s)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
2023-09-02 18:02:50 +00:00
|
|
|
err = d.store.Settings.SaveServer(&s.Server)
|
2019-01-08 14:07:55 +00:00
|
|
|
checkErr(err)
|
2019-01-07 20:24:23 +00:00
|
|
|
err = d.store.Auth.Save(auther)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
fmt.Printf(`
|
|
|
|
Congratulations! You've set up your database to use with File Browser.
|
2020-11-04 16:47:36 +00:00
|
|
|
Now add your first user via 'filebrowser users add' and then you just
|
2019-01-05 22:44:33 +00:00
|
|
|
need to call the main command to boot up the server.
|
|
|
|
`)
|
2019-01-07 20:24:23 +00:00
|
|
|
}, pythonConfig{noDB: true}),
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
2023-09-03 17:28:00 +00:00
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func getAuthentication() auth.Auther {
|
|
|
|
method := settings.GlobalConfiguration.Auth.Method
|
|
|
|
var auther auth.Auther
|
|
|
|
if method == "proxy" {
|
|
|
|
header := settings.GlobalConfiguration.Auth.Header
|
|
|
|
auther = &auth.ProxyAuth{Header: header}
|
|
|
|
}
|
|
|
|
|
|
|
|
if method == "noauth" {
|
|
|
|
auther = &auth.NoAuth{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if method == "password" {
|
|
|
|
jsonAuth := &auth.JSONAuth{}
|
|
|
|
host := settings.GlobalConfiguration.Auth.Recaptcha.Host
|
|
|
|
key := settings.GlobalConfiguration.Auth.Recaptcha.Key
|
|
|
|
secret := settings.GlobalConfiguration.Auth.Recaptcha.Secret
|
|
|
|
if key != "" && secret != "" {
|
|
|
|
jsonAuth.ReCaptcha = &auth.ReCaptcha{
|
|
|
|
Host: host,
|
|
|
|
Key: key,
|
|
|
|
Secret: secret,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auther = jsonAuth
|
|
|
|
}
|
|
|
|
|
|
|
|
if method == "hook" {
|
|
|
|
command := settings.GlobalConfiguration.Auth.Command
|
|
|
|
auther = &auth.HookAuth{Command: command}
|
|
|
|
}
|
|
|
|
|
|
|
|
if auther == nil {
|
|
|
|
panic(errors.ErrInvalidAuthMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
return auther
|
|
|
|
}
|