Improvements to field generation (#137)

* Remove `no-embedded-types` flag

We don't use this.

* Use `fw-download.ubnt.com`

* Return firmware download URL from `latestUnifiVersion`

* Don't use version metadata
This commit is contained in:
Joshua Spence
2023-05-26 15:29:07 +10:00
committed by GitHub
parent 93d9584462
commit 97b562a6d2
6 changed files with 180 additions and 197 deletions

View File

@@ -9,6 +9,7 @@ import (
"fmt"
"go/format"
"io"
"net/url"
"os"
"path"
"path/filepath"
@@ -89,8 +90,6 @@ var fileReps = []replacement{
{"ApGroups", "APGroup"},
}
var embedTypes bool
type Resource struct {
StructName string
ResourcePath string
@@ -194,7 +193,6 @@ func usage() {
func main() {
flag.Usage = usage
noEmbeddedTypesFlag := flag.Bool("no-embedded-types", true, "Whether to generate top-level type definitions for embedded type definitions")
versionBaseDirFlag := flag.String("version-base-dir", ".", "The base directory for version JSON files")
outputDirFlag := flag.String("output-dir", ".", "The output directory of the generated Go code")
downloadOnly := flag.Bool("download-only", false, "Only download and build the fields JSON directory, do not generate")
@@ -202,8 +200,6 @@ func main() {
flag.Parse()
embedTypes = !*noEmbeddedTypesFlag
specifiedVersion := flag.Arg(0)
if specifiedVersion != "" && *useLatestVersion {
fmt.Print("error: cannot specify version with latest\n\n")
@@ -216,10 +212,11 @@ func main() {
}
var unifiVersion *version.Version
var unifiDownloadUrl *url.URL
var err error
if *useLatestVersion {
unifiVersion, err = latestUnifiVersion()
unifiVersion, unifiDownloadUrl, err = latestUnifiVersion()
if err != nil {
panic(err)
}
@@ -229,6 +226,11 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
unifiDownloadUrl, err = url.Parse(fmt.Sprintf("https://dl.ui.com/unifi/%s/unifi_sysvinit_all.deb", unifiVersion))
if err != nil {
panic(err)
}
}
wd, err := os.Getwd()
@@ -251,7 +253,7 @@ func main() {
}
// download fields, create
jarFile, err := downloadJar(unifiVersion, fieldsDir)
jarFile, err := downloadJar(unifiDownloadUrl, fieldsDir)
if err != nil {
panic(err)
}
@@ -586,9 +588,7 @@ func (r *Resource) generateCode() (string, error) {
var buf bytes.Buffer
writer := io.Writer(&buf)
tpl := template.Must(template.New("api.go.tmpl").Funcs(template.FuncMap{
"embedTypes": func() bool { return embedTypes },
}).Parse(apiGoTemplate))
tpl := template.Must(template.New("api.go.tmpl").Parse(apiGoTemplate))
err = tpl.Execute(writer, r)
if err != nil {