filebrowser/cmd/config_init.go
Henrique Dias 42227d9edd feat: many updates (see PR)
feat: add main command

feat: add todos

feat: add signup api

feat: do not repeat code

fix: user return

feat: work out static box

fix: setup static handlers

feat: add share types

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

feat: start static

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

feat: bring back more features

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

add

feat: readd more files

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

feat: add dockerignore

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

feat: gitignore

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

feat: readd submodule

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
2018-12-28 23:40:11 +00:00

75 lines
1.8 KiB
Go

package cmd
import (
"errors"
"fmt"
"os"
"github.com/filebrowser/filebrowser/bolt"
"github.com/filebrowser/filebrowser/types"
"github.com/spf13/cobra"
)
func init() {
configCmd.AddCommand(configInitCmd)
addConfigFlags(configInitCmd)
configInitCmd.MarkFlagRequired("scope")
}
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: initDatabase,
}
func initDatabase(cmd *cobra.Command, args []string) {
if _, err := os.Stat(databasePath); err == nil {
panic(errors.New(databasePath + " already exists"))
}
defaults := types.UserDefaults{}
getUserDefaults(cmd, &defaults, true)
authMethod, auther := getAuthentication(cmd)
settings := &types.Settings{
Key: generateRandomBytes(64), // 256 bits
BaseURL: mustGetString(cmd, "baseURL"),
Signup: mustGetBool(cmd, "signup"),
Defaults: defaults,
AuthMethod: authMethod,
}
runner := &types.Runner{
Commands: map[string][]string{},
}
for _, event := range types.DefaultEvents {
runner.Commands[event] = []string{}
}
db, err := bolt.Open(databasePath)
checkErr(err)
defer db.Close()
st := bolt.ConfigStore{DB: db}
err = st.SaveSettings(settings)
checkErr(err)
err = st.SaveRunner(runner)
checkErr(err)
err = st.SaveAuther(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(settings, auther)
}