Add --version flag and display version on startup
This commit is contained in:
@@ -16,11 +16,33 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// version returns the current version from git tag and last modification date.
|
||||||
|
func version() string {
|
||||||
|
tag := "dev"
|
||||||
|
if out, err := exec.Command("git", "describe", "--tags", "--abbrev=0").Output(); err == nil {
|
||||||
|
tag = strings.TrimSpace(string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
date := ""
|
||||||
|
if out, err := exec.Command("git", "log", "-1", "--format=%ci").Output(); err == nil {
|
||||||
|
date = strings.TrimSpace(string(out))
|
||||||
|
if len(date) >= 10 {
|
||||||
|
date = date[:10]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if date != "" {
|
||||||
|
return fmt.Sprintf("%s (%s)", tag, date)
|
||||||
|
}
|
||||||
|
return tag
|
||||||
|
}
|
||||||
|
|
||||||
// main is the entry point for the gormcol-gen CLI tool.
|
// main is the entry point for the gormcol-gen CLI tool.
|
||||||
// It parses flags, loads configuration from .env, connects to the database,
|
// It parses flags, loads configuration from .env, connects to the database,
|
||||||
// and generates GORM models with column descriptors.
|
// and generates GORM models with column descriptors.
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
versionFlag := flag.Bool("version", false, "display version information")
|
||||||
dsn := flag.String("dsn", "", "database DSN (e.g. user:pass@tcp(host:3306)/dbname)")
|
dsn := flag.String("dsn", "", "database DSN (e.g. user:pass@tcp(host:3306)/dbname)")
|
||||||
filter := flag.String("filter", "", "regex to match table names (triggers batch mode)")
|
filter := flag.String("filter", "", "regex to match table names (triggers batch mode)")
|
||||||
all := flag.Bool("all", false, "generate all tables matching filter (shows confirmation)")
|
all := flag.Bool("all", false, "generate all tables matching filter (shows confirmation)")
|
||||||
@@ -44,7 +66,15 @@ func main() {
|
|||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Get DSN from flag or environment variable.
|
// Display version on startup.
|
||||||
|
fmt.Printf("gormcol %s\n", version())
|
||||||
|
|
||||||
|
// Display version if requested.
|
||||||
|
if *versionFlag {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to the database.
|
||||||
dsnValue := *dsn
|
dsnValue := *dsn
|
||||||
if dsnValue == "" {
|
if dsnValue == "" {
|
||||||
dsnValue = os.Getenv("DSN")
|
dsnValue = os.Getenv("DSN")
|
||||||
|
|||||||
Reference in New Issue
Block a user