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
This commit is contained in:
Egon Elbre 2022-09-01 18:25:27 +03:00
parent 64e5fb7772
commit 1343528a43
2 changed files with 33 additions and 4 deletions

View File

@ -105,7 +105,8 @@ func (f *tsGenFile) pf(format string, a ...interface{}) {
} }
func (f *tsGenFile) write() error { 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) { func (f *tsGenFile) getStructsFromType(t reflect.Type) {

View File

@ -6,7 +6,8 @@ package main
//go:generate go run ./ //go:generate go run ./
import ( import (
"fmt" "os"
"path/filepath"
"time" "time"
"storj.io/common/uuid" "storj.io/common/uuid"
@ -146,6 +147,33 @@ func main() {
}) })
} }
a.MustWriteGo("satellite/console/consoleweb/consoleapi/api.gen.go") modroot := findModuleRootDir()
a.MustWriteTS(fmt.Sprintf("web/satellite/src/api/%s.gen.ts", a.Version)) 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
} }