mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
fix(sql-migrate): use goreleaser ldflags for version info
Replace hardcoded version const with var block populated by goreleaser ldflags (version, commit, date). Add printVersion() matching the pattern used by sibling commands (tcpfwd, smsapid, auth-proxy). Fix date var shadowing in main() by renaming local to today.
This commit is contained in:
parent
075cc7b286
commit
404079f154
@ -17,6 +17,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -28,8 +29,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// replaced by goreleaser / ldflags
|
||||||
version = "2.0.3"
|
var (
|
||||||
|
version = "0.0.0-dev"
|
||||||
|
commit = "0000000"
|
||||||
|
date = "0001-01-01"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -74,8 +78,13 @@ fi
|
|||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
const helpText = `
|
// printVersion displays the version, commit, and build date.
|
||||||
sql-migrate v` + version + ` - a feature-branch-friendly SQL migrator
|
func printVersion(w io.Writer) {
|
||||||
|
_, _ = fmt.Fprintf(w, "sql-migrate v%s %s (%s)\n", version, commit[:7], date)
|
||||||
|
}
|
||||||
|
|
||||||
|
var helpText = `
|
||||||
|
sql-migrate - a feature-branch-friendly SQL migrator
|
||||||
|
|
||||||
USAGE
|
USAGE
|
||||||
sql-migrate [-d sqldir] <command> [args]
|
sql-migrate [-d sqldir] <command> [args]
|
||||||
@ -146,17 +155,22 @@ type MainConfig struct {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var cfg MainConfig
|
var cfg MainConfig
|
||||||
var date = time.Now()
|
var today = time.Now()
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
//nolint
|
printVersion(os.Stdout)
|
||||||
|
fmt.Println("")
|
||||||
fmt.Printf("%s\n", helpText)
|
fmt.Printf("%s\n", helpText)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
case "help", "--help",
|
case "-V", "-version", "--version", "version":
|
||||||
"version", "--version", "-V":
|
printVersion(os.Stdout)
|
||||||
|
os.Exit(0)
|
||||||
|
case "help", "-help", "--help":
|
||||||
|
printVersion(os.Stdout)
|
||||||
|
fmt.Println("")
|
||||||
fmt.Printf("%s\n", helpText)
|
fmt.Printf("%s\n", helpText)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
default:
|
default:
|
||||||
@ -190,7 +204,8 @@ func main() {
|
|||||||
fsSub = flag.NewFlagSet(subcmd, flag.ExitOnError)
|
fsSub = flag.NewFlagSet(subcmd, flag.ExitOnError)
|
||||||
default:
|
default:
|
||||||
log.Printf("unknown command %s", subcmd)
|
log.Printf("unknown command %s", subcmd)
|
||||||
fmt.Printf("%s\n", helpText)
|
printVersion(os.Stderr)
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", helpText)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err := fsSub.Parse(subArgs); err != nil {
|
if err := fsSub.Parse(subArgs); err != nil {
|
||||||
@ -247,7 +262,7 @@ func main() {
|
|||||||
// mMigratorDownPath := filepath.Join(cfg.migrationsDir, M_MIGRATOR_DOWN_NAME)
|
// mMigratorDownPath := filepath.Join(cfg.migrationsDir, M_MIGRATOR_DOWN_NAME)
|
||||||
|
|
||||||
state := State{
|
state := State{
|
||||||
Date: date,
|
Date: today,
|
||||||
MigrationsDir: cfg.migrationsDir,
|
MigrationsDir: cfg.migrationsDir,
|
||||||
}
|
}
|
||||||
state.SQLCommand, state.LogPath, err = extractVars(mMigratorUpPath)
|
state.SQLCommand, state.LogPath, err = extractVars(mMigratorUpPath)
|
||||||
@ -367,7 +382,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Printf("unknown command %s", subcmd)
|
log.Printf("unknown command %s", subcmd)
|
||||||
fmt.Printf("%s\n", helpText)
|
printVersion(os.Stderr)
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", helpText)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user