2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2020-06-01 01:12:36 +02:00
|
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
configCmd.AddCommand(configInitCmd)
|
2019-01-08 10:50:37 +00:00
|
|
|
addConfigFlags(configInitCmd.Flags())
|
2019-01-05 22:44:33 +00: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
|
2019-01-08 16:37:02 +00:00
|
|
|
'filebrowser config set'. The user related flags apply
|
2019-01-05 22:44:33 +00:00
|
|
|
to the defaults when creating new users and you don't
|
|
|
|
|
override the options.`,
|
|
|
|
|
Args: cobra.NoArgs,
|
2025-11-18 11:29:28 +01:00
|
|
|
RunE: withStore(func(cmd *cobra.Command, _ []string, st *store) error {
|
2019-01-08 14:07:55 +00:00
|
|
|
flags := cmd.Flags()
|
2025-11-17 08:45:43 +01:00
|
|
|
|
2025-11-17 09:16:54 +01:00
|
|
|
// Initialize config
|
|
|
|
|
s := &settings.Settings{Key: generateKey()}
|
2025-11-17 08:45:43 +01:00
|
|
|
ser := &settings.Server{}
|
2025-07-22 11:55:21 +05:30
|
|
|
|
2025-11-17 09:16:54 +01:00
|
|
|
// Fill config with options
|
|
|
|
|
auther, err := getSettings(flags, s, ser, nil, true)
|
2025-11-17 00:57:02 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 09:16:54 +01:00
|
|
|
// Save updated config
|
2025-11-18 11:29:28 +01:00
|
|
|
err = st.Settings.Save(s)
|
2025-07-22 11:55:21 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-11-17 08:45:43 +01:00
|
|
|
|
2025-11-18 11:29:28 +01:00
|
|
|
err = st.Settings.SaveServer(ser)
|
2025-07-22 11:55:21 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-11-17 08:45:43 +01:00
|
|
|
|
2025-11-18 11:29:28 +01:00
|
|
|
err = st.Auth.Save(auther)
|
2025-07-22 11:55:21 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
|
|
|
|
|
fmt.Printf(`
|
|
|
|
|
Congratulations! You've set up your database to use with File Browser.
|
2020-11-05 00:47:36 +08:00
|
|
|
Now add your first user via 'filebrowser users add' and then you just
|
2019-01-05 22:44:33 +00:00
|
|
|
need to call the main command to boot up the server.
|
|
|
|
|
`)
|
2025-07-22 11:55:21 +05:30
|
|
|
return printSettings(ser, s, auther)
|
2025-11-18 11:29:28 +01:00
|
|
|
}, storeOptions{expectsNoDatabase: true}),
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|