From 1343528a43e1614ff64a8e5e5292a5ec71aa4ad1 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Thu, 1 Sep 2022 18:25:27 +0300 Subject: [PATCH] satellite/console/.../consoleapi/gen: ensure go generate works Currently the paths were set relative to the root of the module, however the code did not ensure that we are running relative to the module directory. Also, ensure typescript output corresponds to our styling. Change-Id: I2b3cbd4ea8f2615e35c7b58c6fb8851669c47885 --- private/apigen/tsgen.go | 3 +- .../console/consoleweb/consoleapi/gen/main.go | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/private/apigen/tsgen.go b/private/apigen/tsgen.go index b4752d4d0..9f5fe7a37 100644 --- a/private/apigen/tsgen.go +++ b/private/apigen/tsgen.go @@ -105,7 +105,8 @@ func (f *tsGenFile) pf(format string, a ...interface{}) { } func (f *tsGenFile) write() error { - return os.WriteFile(f.path, []byte(f.result), 0644) + content := strings.ReplaceAll(f.result, "\t", " ") + return os.WriteFile(f.path, []byte(content), 0644) } func (f *tsGenFile) getStructsFromType(t reflect.Type) { diff --git a/satellite/console/consoleweb/consoleapi/gen/main.go b/satellite/console/consoleweb/consoleapi/gen/main.go index b8e1065d4..72500bb45 100644 --- a/satellite/console/consoleweb/consoleapi/gen/main.go +++ b/satellite/console/consoleweb/consoleapi/gen/main.go @@ -6,7 +6,8 @@ package main //go:generate go run ./ import ( - "fmt" + "os" + "path/filepath" "time" "storj.io/common/uuid" @@ -146,6 +147,33 @@ func main() { }) } - a.MustWriteGo("satellite/console/consoleweb/consoleapi/api.gen.go") - a.MustWriteTS(fmt.Sprintf("web/satellite/src/api/%s.gen.ts", a.Version)) + modroot := findModuleRootDir() + a.MustWriteGo(filepath.Join(modroot, "satellite", "console", "consoleweb", "consoleapi", "api.gen.go")) + a.MustWriteTS(filepath.Join(modroot, "web", "satellite", "src", "api", a.Version+".gen.ts")) +} + +func findModuleRootDir() string { + dir, err := os.Getwd() + if err != nil { + panic("unable to find current working directory") + } + start := dir + + for i := 0; i < 100; i++ { + if fileExists(filepath.Join(dir, "go.mod")) { + return dir + } + next := filepath.Dir(dir) + if next == dir { + break + } + dir = next + } + + panic("unable to find go.mod starting from " + start) +} + +func fileExists(path string) bool { + _, err := os.Stat(path) + return err == nil }