2019-01-06 06:44:33 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
configCmd.AddCommand(configInitCmd)
|
2019-01-08 18:50:37 +08:00
|
|
|
addConfigFlags(configInitCmd.Flags())
|
2019-01-06 06:44:33 +08: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
|
|
|
|
"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,
|
2019-01-08 04:24:23 +08:00
|
|
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
2019-01-06 06:44:33 +08:00
|
|
|
defaults := settings.UserDefaults{}
|
2019-01-08 22:07:55 +08:00
|
|
|
flags := cmd.Flags()
|
|
|
|
getUserDefaults(flags, &defaults, true)
|
|
|
|
authMethod, auther := getAuthentication(flags)
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
s := &settings.Settings{
|
|
|
|
Key: generateRandomBytes(64), // 256 bit
|
2019-01-08 22:07:55 +08:00
|
|
|
Signup: mustGetBool(flags, "signup"),
|
|
|
|
Shell: strings.Split(strings.TrimSpace(mustGetString(flags, "shell")), " "),
|
2019-01-06 06:44:33 +08:00
|
|
|
AuthMethod: authMethod,
|
|
|
|
Defaults: defaults,
|
|
|
|
Branding: settings.Branding{
|
2019-01-08 22:07:55 +08:00
|
|
|
Name: mustGetString(flags, "branding.name"),
|
|
|
|
DisableExternal: mustGetBool(flags, "branding.disableExternal"),
|
|
|
|
Files: mustGetString(flags, "branding.files"),
|
2019-01-06 06:44:33 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:07:55 +08:00
|
|
|
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"),
|
|
|
|
}
|
|
|
|
|
2019-01-08 04:24:23 +08:00
|
|
|
err := d.store.Settings.Save(s)
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
2019-01-08 22:07:55 +08:00
|
|
|
err = d.store.Settings.SaveServer(ser)
|
|
|
|
checkErr(err)
|
2019-01-08 04:24:23 +08:00
|
|
|
err = d.store.Auth.Save(auther)
|
2019-01-06 06:44:33 +08:00
|
|
|
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.
|
|
|
|
`)
|
2019-01-08 22:07:55 +08:00
|
|
|
printSettings(ser, s, auther)
|
2019-01-08 04:24:23 +08:00
|
|
|
}, pythonConfig{noDB: true}),
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|