2022-02-09 14:01:19 +00:00
|
|
|
package initialise
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
2022-02-11 10:02:47 +00:00
|
|
|
"fmt"
|
2022-02-09 14:01:19 +00:00
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/spf13/cobra"
|
2022-02-11 10:02:47 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
//sql import
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
conn string
|
2022-02-09 14:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New() *cobra.Command {
|
2022-02-11 10:02:47 +00:00
|
|
|
cmd := &cobra.Command{
|
2022-02-09 14:01:19 +00:00
|
|
|
Use: "init",
|
|
|
|
Short: "initialize ZITADEL instance",
|
|
|
|
Long: `init sets up the minimum requirements to start ZITADEL.
|
|
|
|
Prereqesits:
|
|
|
|
- cockroachdb`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-02-11 10:02:47 +00:00
|
|
|
config := new(Config)
|
|
|
|
if err := viper.Unmarshal(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return initialise(config)
|
2022-02-09 14:01:19 +00:00
|
|
|
},
|
|
|
|
}
|
2022-02-11 10:02:47 +00:00
|
|
|
|
|
|
|
// cmd.PersistentFlags().StringArrayVar(&configFiles, "config", nil, "path to config file to overwrite system defaults")
|
|
|
|
//TODO(hust): simplify to multiple flags
|
|
|
|
cmd.PersistentFlags().StringVar(&conn, "connection", "", "connection string to connect with a user which is allowed to create the database and user")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func initialise(config *Config) error {
|
|
|
|
logging.Info("initialization started")
|
|
|
|
|
|
|
|
if conn == "" {
|
|
|
|
return fmt.Errorf("connection not defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := prepareDB(config.Database); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return prepareZitadel(config.Database)
|
2022-02-09 14:01:19 +00:00
|
|
|
}
|