From 7b606129b8c1edbf542d6a1f0a026cfacbbdf458 Mon Sep 17 00:00:00 2001 From: gogacoder Date: Sat, 8 Mar 2025 20:54:22 +0700 Subject: [PATCH] feat: path auto resolution --- generation/gen.go | 4 ++-- utils/find_frontend_path.go | 34 ++++++++++++---------------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/generation/gen.go b/generation/gen.go index 7938fe6..927aead 100644 --- a/generation/gen.go +++ b/generation/gen.go @@ -1,16 +1,16 @@ package generation import ( - "fmt" "log" "nto_cli/entities" "nto_cli/utils" "os" + "path/filepath" "strings" ) func Generate(structName string, fields []entities.Field) { - mkPath := fmt.Sprintf("%s/frontend/src/%s", utils.FindFrontendPath(), strings.ToLower(structName)) + mkPath := filepath.Join(utils.FindFrontendPath(), strings.ToLower(structName)) if err := os.Mkdir(mkPath, 0755); err != nil { log.Fatalf("Failed to mkdir for model: %s", err) } diff --git a/utils/find_frontend_path.go b/utils/find_frontend_path.go index c424137..96842f6 100644 --- a/utils/find_frontend_path.go +++ b/utils/find_frontend_path.go @@ -4,6 +4,7 @@ import ( "log" "os" "path/filepath" + "strings" ) func FindFrontendPath() string { @@ -12,33 +13,22 @@ func FindFrontendPath() string { log.Fatalf("Failed to find path for frontend: %s", err) } - var dirs []string - for currentPath != filepath.VolumeName(currentPath)+string(os.PathSeparator) { - dir, file := filepath.Split(currentPath) - if file != "" { - dirs = append([]string{file}, dirs...) - } - currentPath = filepath.Clean(dir) - } - - if len(dirs) < 2 || filepath.Join(dirs[len(dirs)-2], dirs[len(dirs)-1]) != filepath.Join("frontend", "src") { - log.Fatalf("Run this program in frontend/ directory") + frontendSuffix := filepath.Join("frontend") + frontendSourceSuffix := filepath.Join("frontend", "src") + if strings.HasSuffix(currentPath, frontendSourceSuffix) { + return currentPath } var path string - for i, dir := range dirs { - if dir == "frontend" { - break - } - if i > 0 { - path = filepath.Join(path, dir) - } else { - path = dir - } + + if strings.HasSuffix(currentPath, frontendSuffix) { + path = filepath.Join(currentPath, "src") + } else { + path = filepath.Join(currentPath, frontendSourceSuffix) } - if filepath.VolumeName(path) == "" { - path = filepath.Join(string(os.PathSeparator), path) + if _, err := os.Stat(path); os.IsNotExist(err) { + log.Fatalf("Frontend source directory not found at %s", path) } return path