-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (69 loc) · 1.67 KB
/
main.go
File metadata and controls
85 lines (69 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"log"
"os"
"path/filepath"
"runtime/debug"
"github.com/vmkteam/pgmigrator/pkg/app"
"github.com/vmkteam/pgmigrator/pkg/migrator"
"github.com/BurntSushi/toml"
"github.com/go-pg/pg/v10"
"github.com/spf13/cobra"
)
var (
cfgFile string
migrationsDir string
)
func main() {
log.SetFlags(0)
rootCmd := newRootCmd()
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", app.DefaultConfigFile, "configuration file")
rootCmd.PersistentFlags().StringVarP(&migrationsDir, "dir", "d", "", "path to migrations directory")
rootCmd.InitDefaultVersionFlag()
rootCmd.InitDefaultHelpFlag()
exitOnErr(rootCmd.ParseFlags(os.Args))
// read config
cfg := app.Config{
App: migrator.NewDefaultConfig(),
ConfigFile: cfgFile,
}
var mg *migrator.Migrator
// check for configuration file
_, err := os.Stat(cfgFile)
if err == nil {
_, err = toml.DecodeFile(cfgFile, &cfg)
exitOnErr(err)
cfg.ConfigFile, err = filepath.Abs(cfgFile)
exitOnErr(err)
rootDir := filepath.Dir(cfg.ConfigFile)
if migrationsDir != "" {
rootDir, err = filepath.Abs(migrationsDir)
exitOnErr(err)
}
mg = migrator.NewMigrator(pg.Connect(cfg.Database), cfg.App, rootDir)
}
// create app and run
a := app.New(rootCmd, mg, cfg)
exitOnErr(a.Run(context.Background()))
}
func exitOnErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func newRootCmd() *cobra.Command {
return &cobra.Command{
Use: "pgmigrator",
Short: "Command-line tool for PostgreSQL migrations",
Long: ``,
Version: appVersion(),
}
}
func appVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return info.Main.Version
}