filebrowser/cmd/config_init.go
Henrique Dias 981b42fb5c fix: remove init from root
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

Former-commit-id: 0a29d1eb40c1df0ad039959d3dc3ab4e5ebe328f [formerly eda99299d47c005f30bc04123403d9a739fd4933] [formerly f0703faa441e10b681854c71e6fb9fdbbad6ac44 [formerly 148d233d7a]]
Former-commit-id: e2162f9ac885704fc60e0b427b8331207b960712 [formerly c39d1dbdb49c5f534c69d6359f3afc793bcf5632]
Former-commit-id: de358cd75d688ba93fd4f36b8c5f3aadbe58ca3f
2019-01-07 19:21:29 +00:00

69 lines
1.8 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)
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
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)
},
}