filebrowser/cmd/config_init.go
Henrique Dias e5c150e83f merge with master
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

Former-commit-id: dfac33db9ca3454cd3af02a732a64a02a8268b01 [formerly 38be015f7b3376a3bc992725c1cb1a019c1843cb] [formerly 5a4e310b96ebc5895e64f8eeb2f592890d4b63b0 [formerly 2b8bd2815859af2eed2f6f2b6f95823af1dcff5d]]
Former-commit-id: a30bfcebe6bbc115990b8f8398171b32942a8767 [formerly 553fafa22b46b2689ece093f3b0cf85b3cff1c87]
Former-commit-id: 5ce047d4e3f5217a179685f8b2850d4eb157376e
2019-01-06 13:20:17 +00:00

71 lines
1.9 KiB
Go

package cmd
import (
"errors"
"fmt"
"os"
"strings"
"github.com/asdine/storm"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/spf13/cobra"
v "github.com/spf13/viper"
)
func init() {
configCmd.AddCommand(configInitCmd)
rootCmd.AddCommand(configInitCmd)
addConfigFlags(configInitCmd)
}
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: func(cmd *cobra.Command, args []string) {
databasePath := v.GetString("database")
if _, err := os.Stat(databasePath); err == nil {
panic(errors.New(databasePath + " already exists"))
}
defaults := settings.UserDefaults{}
getUserDefaults(cmd, &defaults, true)
authMethod, auther := getAuthentication(cmd)
db, err := storm.Open(databasePath)
checkErr(err)
defer db.Close()
st := getStorage(db)
s := &settings.Settings{
Key: generateRandomBytes(64), // 256 bit
BaseURL: mustGetString(cmd, "baseURL"),
Signup: mustGetBool(cmd, "signup"),
Shell: strings.Split(strings.TrimSpace(mustGetString(cmd, "shell")), " "),
AuthMethod: authMethod,
Defaults: defaults,
Branding: settings.Branding{
Name: mustGetString(cmd, "branding.name"),
DisableExternal: mustGetBool(cmd, "branding.disableExternal"),
Files: mustGetString(cmd, "branding.files"),
},
}
err = st.Settings.Save(s)
checkErr(err)
err = st.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(s, auther)
},
}