gorm-goose/cmd/gorm-goose/main.go

123 lines
2.9 KiB
Go

/*
* Copyright (c) 2020 Alex aka mailoman <alex@webz.asia>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Alex aka mailoman <alex@webz.asia>
* @copyright Copyright (c) 2020 Alex aka mailoman <alex@webz.asia>
* @since 08.01.2020
*
*/
package main
import (
"flag"
"fmt"
"os"
"strings"
"text/template"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose"
"git.webz.asia/go-migration/gorm-goose/version"
)
// global options. available to any subcommands.
var (
flagPath = flag.String("path", "db", "folder containing db info")
flagEnv = flag.String("env", "development", "which DB environment to use")
flagPgSchema = flag.String("pgschema", "", "which postgres-schema to migrate (default = none)")
showVersion1 = flag.Bool("version", false, "show version")
showVersion2 = flag.Bool("v", false, "show version")
showInfo1 = flag.Bool("info", false, "show version")
showInfo2 = flag.Bool("i", false, "show version")
)
// helper to create a DBConf from the given flags
func dbConfFromFlags() (dbconf *goose.DBConf, err error) {
return goose.NewDBConf(*flagPath, *flagEnv, *flagPgSchema)
}
var commands = []*Command{
upCmd,
downCmd,
redoCmd,
statusCmd,
createCmd,
dbVersionCmd,
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) == 0 || args[0] == "-h" {
if len(args) == 0 {
if *showVersion1 || *showVersion2 {
fmt.Fprintf(os.Stderr, "version: %s\n", version.Version())
return
}
if *showInfo1 || *showInfo2 {
fmt.Fprintf(os.Stderr, "version: %s\nbuild: %s\ntime: %s\n", version.Version(), version.Build(), version.Time())
return
}
}
flag.Usage()
return
}
var cmd *Command
name := args[0]
for _, c := range commands {
if strings.HasPrefix(c.Name, name) {
cmd = c
break
}
}
if cmd == nil {
fmt.Printf("error: unknown command %q\n", name)
flag.Usage()
os.Exit(1)
}
cmd.Exec(args[1:])
}
func usage() {
fmt.Print(usagePrefix)
flag.PrintDefaults()
usageTmpl.Execute(os.Stdout, commands)
}
var usagePrefix = fmt.Sprintf(`
gorm-goose is a database migration management system for Go projects.
version: %s(%s, %s)
Usage:
gorm-goose [options] <subcommand> [subcommand options]
Options:
`, version.Version(), version.Build(), version.Time())
var usageTmpl = template.Must(template.New("usage").Parse(
`
Commands:{{range .}}
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
`))