filebrowser/cmd/config_init.go
Henrique Dias 58ff28f84d feat: export generate key
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

Former-commit-id: 3088476c44d4608e2b8c90d3c274b6db17ef30c0 [formerly e40d3ba193e0b54bccce8c13459032f51af84c7b] [formerly 33cabf26abee5ef4f1dd63e423fe1b0fa7ae4f7c [formerly a3daac84a2]]
Former-commit-id: fe022f36fc4d7d8442df8c0fa4a86e2ff3ec328e [formerly 1d949d0ae88b3b5a9b8e4da5c3b6ca814a319589]
Former-commit-id: 36207ddeb7eba335f5fe73ea9dcdf6bffc6265bf
2019-01-11 20:25:39 +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: generateKey(),
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}),
}