Replaced go generate commands with native go code

This commit is contained in:
Marc Ole Bulling
2023-03-31 13:56:25 +02:00
parent 63c3828805
commit a97979a899
8 changed files with 156 additions and 74 deletions

View File

@@ -0,0 +1,33 @@
//go:build ignore
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
err := buildWasmModule("github.com/forceu/gokapi/cmd/wasmdownloader", "../../internal/webserver/web/main.wasm")
if err != nil {
fmt.Println("ERROR: Could not compile wasmdownloader")
fmt.Println(err)
os.Exit(2)
}
fmt.Println("Compiled Downloader WASM module")
err = buildWasmModule("github.com/forceu/gokapi/cmd/wasme2e", "../../internal/webserver/web/e2e.wasm")
if err != nil {
fmt.Println("ERROR: Could not compile wasme2e")
fmt.Println(err)
os.Exit(3)
}
fmt.Println("Compiled E2E WASM module")
}
func buildWasmModule(src string, dst string) error {
cmd := exec.Command("go", "build", "-o", dst, src)
cmd.Env = append(os.Environ(),
"GOOS=js", "GOARCH=wasm")
return cmd.Run()
}

View File

@@ -0,0 +1,32 @@
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
)
func main() {
copyFile(build.Default.GOROOT+"/misc/wasm/wasm_exec.js", "../../internal/webserver/web/static/js/wasm_exec.js")
copyFile("../../go.mod", "../../build/go.mod")
copyFile("../../openapi.json", "../../internal/webserver/web/static/apidocumentation/openapi.json")
}
// copyFile should only be used for small files
func copyFile(src string, dst string) {
data, err := os.ReadFile(src)
if err != nil {
fmt.Println("ERROR: Cannot read " + src)
fmt.Println(err)
os.Exit(1)
}
err = os.WriteFile(dst, data, 0644)
if err != nil {
fmt.Println("ERROR: Cannot write " + dst)
fmt.Println(err)
os.Exit(2)
}
filename := filepath.Base(src)
fmt.Println("Copied " + filename)
}

View File

@@ -0,0 +1,84 @@
//go:build ignore
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
const versionJsAdmin = "16"
const versionJsDropzone = "3"
const versionJsE2EAdmin = "1"
const fileMain = "../../cmd/gokapi/Main.go"
const fileVersionConstants = "../../internal/webserver/web/templates/string_constants.tmpl"
func main() {
checkFileExists(fileMain)
checkFileExists(fileVersionConstants)
template := getTemplate()
err := os.WriteFile(fileVersionConstants, []byte(template), 0664)
if err != nil {
fmt.Println("FAIL: Updating version numbers")
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Updated version numbers")
}
func checkFileExists(filename string) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
fmt.Println("ERROR: File does not exist: " + filename)
os.Exit(2)
}
if info.IsDir() {
fmt.Println("ERROR: File is actually directory: " + filename)
os.Exit(3)
}
}
func getTemplate() string {
versionGokapi := parseGokapiVersion()
result := strings.ReplaceAll(templateVersions, "%gokapiversion%", versionGokapi)
result = strings.ReplaceAll(result, "%jsadmin%", versionJsAdmin)
result = strings.ReplaceAll(result, "%jsdropzone%", versionJsDropzone)
result = strings.ReplaceAll(result, "%jse2e%", versionJsE2EAdmin)
return result
}
func parseGokapiVersion() string {
file, err := os.Open(fileMain)
if err != nil {
fmt.Println("ERROR: Cannot open file: ")
fmt.Println(err)
os.Exit(4)
}
defer file.Close()
scanner := bufio.NewScanner(file)
constRegex := regexp.MustCompile(`const\s+versionGokapi\s+=\s+"(\S+)"`)
for scanner.Scan() {
line := scanner.Text()
matches := constRegex.FindStringSubmatch(line)
if len(matches) == 2 {
return matches[1]
}
}
fmt.Println("ERROR: Gokapi version not found")
os.Exit(5)
return ""
}
const templateVersions = `// Change these for rebranding
{{define "app_name"}}Gokapi{{end}}
{{define "version"}}%gokapiversion%{{end}}
// Specifies the version of JS files, so that the browser doesn't
// use a cached version, if the file has been updated
{{define "js_admin_version"}}%jsadmin%{{end}}
{{define "js_dropzone_version"}}%jsdropzone%{{end}}
{{define "js_e2eversion"}}%jse2e%{{end}}`

View File

@@ -1,48 +0,0 @@
github.com/aws/aws-sdk-go v1.38.36/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/renameio v0.1.0 h1:GOZbcHa3HfsPKPlmyPyN2KEohoMXOhdMbHrvbpl2QaA=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7 h1:+/+DxvQaYifJ+grD4klzrS5y+KJXldn/2YTl5JG+vZ8=
github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7/go.mod h1:zO8QMzTeZd5cpnIkz/Gn6iK0jDfGicM1nynOkkPIl28=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201029080932-201ba4db2418/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20191110171634-ad39bd3f0407/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
mvdan.cc/editorconfig v0.1.1-0.20200121172147-e40951bde157 h1:VBYz8greWWP8BDpRX0v7SDv/8rNlZVmdHdO4ZSsHj/E=
mvdan.cc/editorconfig v0.1.1-0.20200121172147-e40951bde157/go.mod h1:Ge4atmRUYqueGppvJ7JNrtqpqokoJEFxYbP0Z+WeKS8=
mvdan.cc/sh/v3 v3.2.4 h1:+fZaWcXWRjYAvqzEKoDhDM3DkxdDUykU2iw0VMKFe9s=
mvdan.cc/sh/v3 v3.2.4/go.mod h1:fPQmabBpREM/XQ9YXSU5ZFZ/Sm+PmKP9/vkFHgYKJEI=

View File

@@ -1,9 +0,0 @@
#!/bin/sh
#Called by go generate
#Sets the version numbers in the template automatically
sed -i 's/{{define "version"}}.*{{end}}/{{define "version"}}'$1'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
echo "Updated version in web template"
sed -i 's/{{define "js_admin_version"}}.*{{end}}/{{define "js_admin_version"}}'$2'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
sed -i 's/{{define "js_dropzone_version"}}.*{{end}}/{{define "js_dropzone_version"}}'$3'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
sed -i 's/{{define "js_e2eversion"}}.*{{end}}/{{define "js_e2eversion"}}'$4'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
echo "Updated JS version numbers"

View File

@@ -29,16 +29,13 @@ import (
)
// versionGokapi is the current version in readable form.
// The go generate call below needs to be modified as well
// Other version numbers can be modified in /build/go-generate/updateVersionNumbers.go
const versionGokapi = "1.6.3-dev"
// The following call updates the version numbers
// Parameters:
// GokapiVersion, JsAdmin, JsDropzone, JsE2EAdmin
//go:generate sh "../../build/setVersionTemplate.sh" "1.6.3-dev" "16" "3" "1"
//go:generate sh -c "cp \"$(go env GOROOT)/misc/wasm/wasm_exec.js\" ../../internal/webserver/web/static/js/ && echo Copied wasm_exec.js"
//go:generate sh -c "GOOS=js GOARCH=wasm go build -o ../../internal/webserver/web/main.wasm github.com/forceu/gokapi/cmd/wasmdownloader && echo Compiled Downloader WASM module"
//go:generate sh -c "GOOS=js GOARCH=wasm go build -o ../../internal/webserver/web/e2e.wasm github.com/forceu/gokapi/cmd/wasme2e && echo Compiled E2E WASM module"
// The following calls updates the version numbers and build the WASM modules
//go:generate go run "../../build/go-generate/updateVersionNumbers.go"
//go:generate go run "../../build/go-generate/buildWasm.go"
//go:generate go run "../../build/go-generate/copyStaticFiles.go"
// Main routine that is called on startup
func main() {
@@ -164,7 +161,3 @@ const logo = `
██  ██ ██  ██ ██  ██  ██   ██ ██      ██ 
 ██████   ██████  ██  ██ ██  ██ ██  ██ 
`
// Copy go mod file to docker image builder
//go:generate cp "../../go.mod" "../../build/go.mod"
//go:generate echo "Copied go.mod to Docker build directory"

View File

@@ -14,9 +14,6 @@ import (
"time"
)
//go:generate cp ../../../openapi.json ../web/static/apidocumentation/
//go:generate echo "Copied openapi.json"
// Process parses the request and executes the API call or returns an error message to the sender
func Process(w http.ResponseWriter, r *http.Request, maxMemory int) {
w.Header().Set("cache-control", "no-store")

View File

@@ -4,6 +4,6 @@
// Specifies the version of JS files, so that the browser doesn't
// use a cached version, if the file has been updated
{{define "js_admin_version"}}15{{end}}
{{define "js_admin_version"}}16{{end}}
{{define "js_dropzone_version"}}3{{end}}
{{define "js_e2eversion"}}1{{end}}
{{define "js_e2eversion"}}1{{end}}