bottin/cmd/server.go
Victor Lacasse-Beaudoin 991a116d86 Migrate flags to cobra (breaks retrocompatibility)
See `cmd/server.go` for new flag definitions, the old flags will not work anymore.

Most flags can now be defined using viper, see `cmd/root.go` for full list.

Using the `help` subcommand (or the `-h, --help` flags) will now show the equivalence between flags and config fields.

Split bottin.go functionalities into `cmd/server.go`, `data/data.go` and `bottin/bottin.go`

New packages:
`bottin`: base for the implementation of the webserver before version 3. Now has some residual code that should slowly be moved towards the serverCmd in `cmd` package.

`cmd`: Cobra commands package. Webserver is now called using the `server` subcommand.

`data`: Package for database manipulations and the definition of the `Membre{}` struct.
2022-06-02 17:11:53 -04:00

111 lines
2.8 KiB
Go

/*
Copyright © 2022 Victor Lacasse-Beaudoin <victor.lacassebeaudoin@gmail.com>
*/
package cmd
import (
"git.agecem.com/agecem/bottin/bottin"
"git.agecem.com/agecem/bottin/data"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
json_insert_path string
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "Run the bottin server",
Run: func(cmd *cobra.Command, args []string) {
// Open and migrate db
data.OpenDatabase()
data.MigrateDatabase()
bottin.UpdateFlags()
// Import from flag
if json_insert_path != "" {
bottin.InsertJson(json_insert_path)
}
// Run web app
bottin.RunServer()
},
}
func init() {
declareFlags()
rootCmd.AddCommand(serverCmd)
}
func declareFlags() {
// db.type
serverCmd.PersistentFlags().String(
"db-type", "",
"Database type (config: 'db.type')")
viper.BindPFlag(
"db.type",
serverCmd.PersistentFlags().Lookup("db-type"))
serverCmd.MarkPersistentFlagRequired("db.type")
// db.sqlite.path
serverCmd.PersistentFlags().String(
"db-sqlite-path", "",
"Path to sqlite database (config: 'db.sqlite.path')")
viper.BindPFlag(
"db.sqlite.path",
serverCmd.PersistentFlags().Lookup("db-sqlite-path"))
// server.port
serverCmd.PersistentFlags().Int(
"server-port", 1312,
"The port on which the web application will server content (config: 'server.port')")
viper.BindPFlag(
"server.port",
serverCmd.PersistentFlags().Lookup("server-port"))
// login.username
serverCmd.PersistentFlags().String(
"login-username", "bottin",
"The username to login to the web ui. (config: 'login.username')")
viper.BindPFlag(
"login.username",
serverCmd.PersistentFlags().Lookup("login-username"))
// login.password
serverCmd.PersistentFlags().String(
"login-password", "bottin",
"The password to login to the web ui. (config: 'login.password')")
viper.BindPFlag(
"login.password",
serverCmd.PersistentFlags().Lookup("login-password"))
// import.insert-batch-size
serverCmd.PersistentFlags().Int(
"insert-batch-size", 500,
"The amount of inserts to do per batch (config: 'import.insert_batch_size')")
viper.BindPFlag(
"import.insert_batch_size",
serverCmd.PersistentFlags().Lookup("insert-batch-size"))
// json-insert-path
serverCmd.PersistentFlags().StringVar(
&json_insert_path, "json-insert-path", "",
"The location of a json file containing Membres to insert.")
/*
// Not using viper for json-insert-path since it would make it too easy
// to forget to remove from the config every time, which heavily risks
// doubling values.
//
// It would at least need to check for doubles before importing the file,
// for it to be a kind of automatic differential update of the database.
viper.BindPFlag(
"import.json-insert-path",
serverCmd.PersistentFlags().Lookup("json-insert-path"))
*/
}