cloudflare/cfssl_trust
Publicmirrored from https://github.com/cloudflare/cfssl_trustAvailable
cli/setup.go
57lines · modecode
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | |
| 8 | "github.com/cloudflare/cfssl_trust/config" |
| 9 | "github.com/spf13/cobra" |
| 10 | "github.com/spf13/viper" |
| 11 | |
| 12 | _ "github.com/mattes/migrate/driver/sqlite3" |
| 13 | "github.com/mattes/migrate/migrate" |
| 14 | ) |
| 15 | |
| 16 | var setupCmd = &cobra.Command{ |
| 17 | Use: "setup", |
| 18 | Short: "Set up the trust database.", |
| 19 | Long: "`Set up the trust database.", |
| 20 | Run: setup, |
| 21 | } |
| 22 | |
| 23 | func setup(cmd *cobra.Command, args []string) { |
| 24 | var sourceDir string |
| 25 | |
| 26 | // First argument: the path to the migration files. |
| 27 | if len(args) == 0 { |
| 28 | sourceDir = filepath.Join(config.GoPath(), "src", "github.com", "cloudflare", "cfssl_trust", "model") |
| 29 | } else { |
| 30 | sourceDir = args[0] |
| 31 | } |
| 32 | |
| 33 | var err error |
| 34 | dbPath := viper.GetString("database.path") |
| 35 | if dbPath == "" { |
| 36 | dbPath, err = os.Getwd() |
| 37 | if err != nil { |
| 38 | fmt.Fprintf(os.Stderr, "[!] %s\n", err) |
| 39 | os.Exit(1) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fmt.Println("Migration directory:", sourceDir) |
| 44 | fmt.Println("Database path:", dbPath) |
| 45 | errs, ok := migrate.UpSync("sqlite3://"+dbPath, sourceDir) |
| 46 | if !ok { |
| 47 | fmt.Fprintf(os.Stderr, "[!] Failed to set up database:\n") |
| 48 | for _, err := range errs { |
| 49 | fmt.Fprintf(os.Stderr, "\t%s\n", err) |
| 50 | } |
| 51 | os.Exit(1) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func init() { |
| 56 | RootCmd.AddCommand(setupCmd) |
| 57 | } |
| 58 | |