Add --version flag and display version on startup
This commit is contained in:
+31
-1
@@ -16,11 +16,33 @@ import (
|
||||
"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.
|
||||
// It parses flags, loads configuration from .env, connects to the database,
|
||||
// and generates GORM models with column descriptors.
|
||||
func main() {
|
||||
|
||||
versionFlag := flag.Bool("version", false, "display version information")
|
||||
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)")
|
||||
all := flag.Bool("all", false, "generate all tables matching filter (shows confirmation)")
|
||||
@@ -44,7 +66,15 @@ func main() {
|
||||
|
||||
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
|
||||
if dsnValue == "" {
|
||||
dsnValue = os.Getenv("DSN")
|
||||
|
||||
+1
-40
@@ -181,37 +181,6 @@ func generateColsVarBlock(si *structInfo) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// findGoMod searches upward from startDir for a go.mod file.
|
||||
func findGoMod(startDir string) (string, error) {
|
||||
dir := startDir
|
||||
for {
|
||||
path := filepath.Join(dir, "go.mod")
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path, nil
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
return "", fmt.Errorf("go.mod not found from %s", startDir)
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
|
||||
// readModulePath extracts the module path from a go.mod file.
|
||||
func readModulePath(goModPath string) (string, error) {
|
||||
content, err := os.ReadFile(goModPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, line := range strings.Split(string(content), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "module ") {
|
||||
return strings.TrimSpace(strings.TrimPrefix(line, "module ")), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("module directive not found in %s", goModPath)
|
||||
}
|
||||
|
||||
// generateCols appends <Model>Cols variables to generated model files.
|
||||
// It parses each .go file in the output directory, extracts struct fields
|
||||
// with gorm column tags, and generates type-safe Field descriptors.
|
||||
@@ -231,15 +200,7 @@ func (m *GormGen) generateCols() error {
|
||||
return err
|
||||
}
|
||||
|
||||
goModPath, err := findGoMod(absDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
modulePath, err := readModulePath(goModPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gormcolImport := fmt.Sprintf("%q", modulePath+"")
|
||||
gormcolImport := `"git.ma-al.com/goc_marek/gormcol"`
|
||||
|
||||
var fileFilter *regexp.Regexp
|
||||
if len(m.cfg.SelectedTables) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user