filebrowser/cmd/config_init.go
Henrique Dias 870b5b4079 fix: use server options from DB too (#616)
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

Former-commit-id: bb60ec81f56f5d761ef76b2c1e26bbe83210f434 [formerly b28f83c65e0934e138a1710549f52e0f66a158e0] [formerly 7138a3215ceb144afa0ea45be2315f30b052109f [formerly 72069207c6]]
Former-commit-id: 0fb7ae6353f786edd93a9166141bfa446dd3dbf9 [formerly 5cb07e8c96a7cfca1aec5843123e1abc4ca8578a]
Former-commit-id: 914c0348516f072a9ccce4e105327feaaddb4cc0
2019-01-08 14:07:55 +00:00

69 lines
2.0 KiB
Go

package cmd
import (
"fmt"
"strings"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/spf13/cobra"
)
func init() {
configCmd.AddCommand(configInitCmd)
addConfigFlags(configInitCmd.Flags())
}
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
"filebrowser config set". The user related flags apply
to the defaults when creating new users and you don't
override the options.`,
Args: cobra.NoArgs,
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
defaults := settings.UserDefaults{}
flags := cmd.Flags()
getUserDefaults(flags, &defaults, true)
authMethod, auther := getAuthentication(flags)
s := &settings.Settings{
Key: generateRandomBytes(64), // 256 bit
Signup: mustGetBool(flags, "signup"),
Shell: strings.Split(strings.TrimSpace(mustGetString(flags, "shell")), " "),
AuthMethod: authMethod,
Defaults: defaults,
Branding: settings.Branding{
Name: mustGetString(flags, "branding.name"),
DisableExternal: mustGetBool(flags, "branding.disableExternal"),
Files: mustGetString(flags, "branding.files"),
},
}
ser := &settings.Server{
Address: mustGetString(flags, "address"),
Root: mustGetString(flags, "root"),
BaseURL: mustGetString(flags, "baseurl"),
TLSKey: mustGetString(flags, "key"),
TLSCert: mustGetString(flags, "cert"),
Port: mustGetString(flags, "port"),
Log: mustGetString(flags, "log"),
}
err := d.store.Settings.Save(s)
checkErr(err)
err = d.store.Settings.SaveServer(ser)
checkErr(err)
err = d.store.Auth.Save(auther)
checkErr(err)
fmt.Printf(`
Congratulations! You've set up your database to use with File Browser.
Now add your first user via 'filebrowser users new' and then you just
need to call the main command to boot up the server.
`)
printSettings(ser, s, auther)
}, pythonConfig{noDB: true}),
}