From 80871fc02a8a8ad4251ea17776096d3985772a9f Mon Sep 17 00:00:00 2001 From: rafaelDev0ps Date: Wed, 30 Oct 2024 15:13:51 -0300 Subject: [PATCH] Check project directory is correct and ensure there's a go.mod file --- cli/htmgo/tasks/astgen/entry.go | 36 +++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/cli/htmgo/tasks/astgen/entry.go b/cli/htmgo/tasks/astgen/entry.go index 6c45551..e45c835 100644 --- a/cli/htmgo/tasks/astgen/entry.go +++ b/cli/htmgo/tasks/astgen/entry.go @@ -2,17 +2,18 @@ package astgen import ( "fmt" - "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" - "github.com/maddalax/htmgo/cli/htmgo/tasks/process" - "github.com/maddalax/htmgo/framework/h" "go/ast" "go/parser" "go/token" - "golang.org/x/mod/modfile" "os" "path/filepath" "slices" "strings" + + "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" + "github.com/maddalax/htmgo/cli/htmgo/tasks/process" + "github.com/maddalax/htmgo/framework/h" + "golang.org/x/mod/modfile" ) type Page struct { @@ -390,9 +391,20 @@ func writePagesFile() { }) } +func HasModuleFile(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +} + func GetModuleName() string { wd := process.GetWorkingDir() modPath := filepath.Join(wd, "go.mod") + + if HasModuleFile(modPath) == false { + fmt.Fprintf(os.Stderr, "Module not found: go.mod file does not exists.") + return "" + } + goModBytes, err := os.ReadFile(modPath) if err != nil { fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err) @@ -402,6 +414,18 @@ func GetModuleName() string { return modName } +func matchProjectDirectory() bool { + wd := process.GetWorkingDir() + dirs := strings.Split(wd, "/") + + projectDir := dirs[len(dirs)-1] + + if GetModuleName() == projectDir { + return true + } + return false +} + func GenAst(flags ...process.RunFlag) error { if GetModuleName() == "" { if slices.Contains(flags, process.ExitOnError) { @@ -429,5 +453,9 @@ func GenAst(flags ...process.RunFlag) error { `, ChiModuleName) }) + if matchProjectDirectory() == false { + return fmt.Errorf("The project directory does not match with the project name.") + } + return nil }