Add --version flag and display version on startup
This commit is contained in:
41
gencols.go
41
gencols.go
@@ -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