diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..d163711 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +version: 2 +updates: + - package-ecosystem: gomod + directory: / + schedule: + interval: weekly + + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..399b5cb --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,52 @@ +name: CI + +on: + push: + branches: + - master + tags: + - "v[0-9]+.[0-9]+.[0-9]+*" + pull_request: + branches: + - master + +permissions: + contents: read + +jobs: + build: + name: Build + runs-on: ubuntu-24.04 + permissions: + contents: write + id-token: write + steps: + - name: Checkout Repository + uses: actions/checkout@v5.0.0 + with: + fetch-depth: 0 + + - name: Setup Golang Environment + uses: actions/setup-go@v5.5.0 + with: + go-version: stable + + - name: Download Syft + uses: anchore/sbom-action/download-syft@v0.20.0 + if: github.ref_type == 'tag' + + - name: Install Cosign + uses: sigstore/cosign-installer@v3.8.2 + if: github.ref_type == 'tag' + + - name: Build binary + uses: goreleaser/goreleaser-action@v6.3.0 + with: + version: latest + args: ${{ github.ref_type == 'tag' && 'release' || 'build --snapshot' }} --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Print version + run: ./dist/gen-crd-api-reference-docs_linux_amd64_v1/gen-crd-api-reference-docs -version + continue-on-error: true diff --git a/.gitignore b/.gitignore index a4d184e..dcea91a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ refdocs # goreleaser output dist + +gen-crd-api-reference-docs diff --git a/.goreleaser.yml b/.goreleaser.yml index f1cacca..ac18240 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,24 +1,40 @@ +version: 2 builds: - env: - CGO_ENABLED=0 - # travis ci currently sets GOPATH even with go1.11. - # force-setting GO111MODULE=on to use vgo - - GO111MODULE=on goos: - linux - darwin goarch: - amd64 -archive: - name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}" - files: - - LICENSE - - template/** - - example-config.json + - arm64 + flags: + - -trimpath + ldflags: + - -s -w -X main.version={{.Version}} + +archives: + - files: + - LICENSE + - template/** + - example-config.json + checksum: name_template: "checksums.txt" -changelog: - skip: true -release: - # releases are uploaded to github by .travis.yml - disable: true + +sboms: + - artifacts: archive + documents: + - "${artifact}.spdx.json" + +signs: + - cmd: cosign + artifacts: checksum + output: true + certificate: "${artifact}.pem" + args: + - sign-blob + - "--output-signature=${signature}" + - "--output-certificate=${certificate}" + - "${artifact}" + - "--yes" diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index d6845ba..00000000 --- a/.travis.yml +++ /dev/null @@ -1,32 +0,0 @@ -language: go -go: - - 1.11.x -install: - - echo noop -before_script: - # travis ci currently sets GOPATH even with go1.11. - # force-setting GO111MODULE=on to use vgo - - env GO111MODULE=on go mod download -script: - # travis ci currently sets GOPATH even with go1.11. - # force-setting GO111MODULE=on to use vgo - - env GO111MODULE=on go build -v -o /dev/null -deploy: - # use goreleaser to prepare dist/ - - provider: script - skip_cleanup: true - on: - tags: true - script: curl -sL https://git.io/goreleaser | bash - # use github release feature to upload dist/ - - provider: releases - skip_cleanup: true - on: - tags: true - file_glob: true - file: - - dist/*.tar.gz - - dist/*.zip - - dist/checksums.txt - api_key: - secure: r1GMgbVDnZTUcny/PbIATW9dXGOTpm2U9iEGaWvpprMO2AGo7ju7SWEJWtjcap3pc0YasyR2/eon9LC0scWY0Xlpeb+g0pRCQ39FABk1Vo3DpmIPRUCFFkaescWmrWDj3ImzjJgZjCewwK6Fo8s8ngnqIlZnE1Hq6ls2xDp6jNVf+Pn7LyqxkK4axFFSPQM9zFX3N1PVUH5RT03bIJfojJZguqnhNfyTvKvHJidoeWU/Ie+fXc4AdPHyP85xrmGHYl68O0HziU6JCLXira8r1FjUgVeYFYC5nnNuylszO6JWqWh1nXYDxs5FGPnZd9N8bEi/2ahiqms8eV7S+/DGzhSoEdHikcBxTgJpZP2VOmvRSITyv3RleJzCeMULTGFQodoxRgA/Q8qZySvInNjstiBjV2Pyucrnn990XQbN8rIV4RmNggJvbAwJNCGjCwS2eB42EKNCODTuzHPbIV0ap4EjvfBBo0cZ2J9M2Q6VzdpNErdntpM1hZl9yymv3MNN4hOiLQKkofoo/QI3cffB8Y0PBPAL8Cs9Mx1bbx+Dr8iitTHBUAt4a5DHFen4MS8znrZ+Cr4kLDD9QPJ8G0oh4tDKq8CJ73Gt+xqkLZEuka0W1awz9essqE7MH20kRJbKa5woTIs0v9njHMpbeqd7KrNV+1e5F5aPRQyiCzaom7c= diff --git a/README.md b/README.md index e1c6632..4d80b9d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,12 @@ API Reference Docs [like this][ar] this tool is for you. [ar]: https://knative.dev/docs/reference/api/serving-api/ + +> [!WARNING] +> **This project is not super actively maintained.** +> Consider using the [**crd-ref-docs** by Elastic](https://github.com/elastic/crd-ref-docs) project + instead. + ## Alternatives This project has inspired creation of the following projects: @@ -15,11 +21,11 @@ This project has inspired creation of the following projects: - [**crd-ref-docs** by Elastic](https://github.com/elastic/crd-ref-docs): A fresh implementation of this project. -Nowadays, I don't have a lot of time to maintain this tool. So consider -using one of the above in case this repo does not work for you. +- If you're an open source project, consider exposing your + CRD API Reference via https://doc.crds.dev/ without much effort. + + -If you're an open source project, consider exposing your -CRD API Reference via https://doc.crds.dev/ without much effort. ## Current Users @@ -38,6 +44,7 @@ CRD API Reference via https://doc.crds.dev/ without much effort. - [**Antrea** API reference docs](https://antrea.io/docs/v1.3.0/docs/api-reference/) - [**kube-green** API reference docs](https://kube-green.dev/docs/apireference_v1alpha1/) - [**Azure Service Operator** supported resources](https://azure.github.io/azure-service-operator/reference/) +- [**NGINX Gateway Fabric** API reference docs](https://docs.nginx.com/nginx-gateway-fabric/reference/api/) - _[[ADD YOUR PROJECT HERE]]_ Also some **forks**: diff --git a/go.mod b/go.mod index 65fac81..3afa56c 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,15 @@ module github.com/ahmetb/gen-crd-api-reference-docs -go 1.15 +go 1.21.0 require ( - github.com/go-logr/logr v1.2.4 // indirect github.com/russross/blackfriday/v2 v2.1.0 - k8s.io/gengo v0.0.0-20230306165830-ab3349d207d4 - k8s.io/klog/v2 v2.90.1 + k8s.io/gengo/v2 v2.0.0-20250106234829-0359904fc2a6 + k8s.io/klog/v2 v2.130.1 +) + +require ( + github.com/go-logr/logr v1.4.1 // indirect + golang.org/x/mod v0.14.0 // indirect + golang.org/x/tools v0.16.1 // indirect ) diff --git a/go.sum b/go.sum index 6d4a228..942f510 100644 --- a/go.sum +++ b/go.sum @@ -1,45 +1,16 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= -github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/kr/pretty v0.2.0/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/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/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/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 h1:BMFHd4OFnFtWX46Xj4DN6vvT1btiBxyq+s0orYBqcQY= -golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -k8s.io/gengo v0.0.0-20230306165830-ab3349d207d4 h1:aClvVG6GbX10ISHcc24J+tqbr0S7fEe1MWkFJ7cWWCI= -k8s.io/gengo v0.0.0-20230306165830-ab3349d207d4/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E= -k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y= -k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw= -k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= -sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= +golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= +k8s.io/gengo/v2 v2.0.0-20250106234829-0359904fc2a6 h1:SdzkGIk4b5LFkVO36PuO0Bx4tpBDJDpNN0F1/v8JM5c= +k8s.io/gengo/v2 v2.0.0-20250106234829-0359904fc2a6/go.mod h1:EJykeLsmFC60UQbYJezXkEsG2FLrt0GPNkU5iK5GWxU= +k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= +k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= diff --git a/main.go b/main.go index 8ab8f92..0eaba72 100644 --- a/main.go +++ b/main.go @@ -7,13 +7,14 @@ import ( "fmt" "html/template" "io" - "io/ioutil" "net/http" "os" "os/exec" "path/filepath" "reflect" "regexp" + "runtime" + "runtime/debug" "sort" "strconv" "strings" @@ -22,8 +23,9 @@ import ( "unicode" "github.com/russross/blackfriday/v2" - "k8s.io/gengo/parser" - "k8s.io/gengo/types" + "k8s.io/gengo/v2" + "k8s.io/gengo/v2/parser" + "k8s.io/gengo/v2/types" "k8s.io/klog/v2" ) @@ -31,9 +33,13 @@ var ( flConfig = flag.String("config", "", "path to config file") flAPIDir = flag.String("api-dir", "", "api directory (or import path), point this to pkg/apis") flTemplateDir = flag.String("template-dir", "template", "path to template/ dir") + flVersion = flag.Bool("version", false, "print version and exit") flHTTPAddr = flag.String("http-addr", "", "start an HTTP server on specified addr to view the result (e.g. :8080)") flOutFile = flag.String("out-file", "", "path to output file to save the result") + + // set by go build + version string ) const ( @@ -83,6 +89,14 @@ func init() { flag.Set("alsologtostderr", "true") // for klog flag.Parse() + commitHash, commitTime, dirtyBuild := getBuildInfo() + arch := fmt.Sprintf("%v/%v", runtime.GOOS, runtime.GOARCH) + + if *flVersion { + fmt.Printf("gen-crd-api-reference-docs version=%s commit=%s date=%s dirty=%v arch=%s go=%v\n", version, commitHash, commitTime, dirtyBuild, arch, runtime.Version()) + os.Exit(0) + } + if *flConfig == "" { panic("-config not specified") } @@ -98,7 +112,6 @@ func init() { if err := resolveTemplateDir(*flTemplateDir); err != nil { panic(err) } - } func resolveTemplateDir(dir string) error { @@ -156,14 +169,14 @@ func main() { if *flOutFile != "" { dir := filepath.Dir(*flOutFile) - if err := os.MkdirAll(dir, 0755); err != nil { + if err := os.MkdirAll(dir, 0o755); err != nil { klog.Fatalf("failed to create dir %s: %v", dir, err) } s, err := mkOutput() if err != nil { klog.Fatalf("failed: %+v", err) } - if err := ioutil.WriteFile(*flOutFile, []byte(s), 0644); err != nil { + if err := os.WriteFile(*flOutFile, []byte(s), 0o644); err != nil { klog.Fatalf("failed to write to out file: %v", err) } klog.Infof("written to %s", *flOutFile) @@ -191,7 +204,7 @@ func main() { // groupName extracts the "//+groupName" meta-comment from the specified // package's comments, or returns empty string if it cannot be found. func groupName(pkg *types.Package) string { - m := types.ExtractCommentTags("+", pkg.Comments) + m := gengo.ExtractCommentTags("+", pkg.Comments) v := m["groupName"] if len(v) == 1 { return v[0] @@ -200,39 +213,43 @@ func groupName(pkg *types.Package) string { } func parseAPIPackages(dir string) ([]*types.Package, error) { - b := parser.New() - // the following will silently fail (turn on -v=4 to see logs) - if err := b.AddDirRecursive(*flAPIDir); err != nil { - return nil, err + p := parser.New() + + pkgsFound, errFind := p.FindPackages(dir + "/...") + if errFind != nil { + return nil, fmt.Errorf("failed to find packages in %s: %w", dir, errFind) + } + klog.Infof("found %d packages", len(pkgsFound)) + + errLoad := p.LoadPackages(pkgsFound...) + if errLoad != nil { + return nil, fmt.Errorf("failed to load packages: %w", errLoad) } - scan, err := b.FindTypes() + + scan, err := p.NewUniverse() if err != nil { return nil, fmt.Errorf("failed to parse pkgs and types: %w", err) } - var pkgNames []string - for p := range scan { - pkg := scan[p] + + var pkgs []*types.Package + for _, pkg := range scan { + klog.V(3).Infof("trying package=%v groupName=%s", p, groupName(pkg)) // Do not pick up packages that are in vendor/ as API packages. (This // happened in knative/eventing-sources/vendor/..., where a package // matched the pattern, but it didn't have a compatible import path). if isVendorPackage(pkg) { - klog.V(3).Infof("package=%v coming from vendor/, ignoring.", p) + klog.V(3).Infof("package=%v coming from vendor/, ignoring.", pkg.Name) continue } if groupName(pkg) != "" && len(pkg.Types) > 0 || containsString(pkg.DocComments, docCommentForceIncludes) { - klog.V(3).Infof("package=%v has groupName and has types", p) - pkgNames = append(pkgNames, p) + klog.V(3).Infof("package=%v has groupName and has types", pkg.Name) + klog.Info("using package=", pkg.Name) + pkgs = append(pkgs, pkg) } } - sort.Strings(pkgNames) - var pkgs []*types.Package - for _, p := range pkgNames { - klog.Infof("using package=%s", p) - pkgs = append(pkgs, scan[p]) - } return pkgs, nil } @@ -302,7 +319,7 @@ func combineAPIPackages(pkgs []*types.Package) ([]*apiPackage, error) { // isVendorPackage determines if package is coming from vendor/ dir. func isVendorPackage(pkg *types.Package) bool { vendorPattern := string(os.PathSeparator) + "vendor" + string(os.PathSeparator) - return strings.Contains(pkg.SourcePath, vendorPattern) + return strings.Contains(pkg.Dir, vendorPattern) } func findTypeReferences(pkgs []*apiPackage) map[*types.Type][]*types.Type { @@ -404,7 +421,7 @@ func linkForType(t *types.Type, c generatorConfig, typePkgMap map[*types.Type]*a return "#" + anchorIDForLocalType(t, typePkgMap), nil } - var arrIndex = func(a []string, i int) string { + arrIndex := func(a []string, i int) string { return a[(len(a)+i)%len(a)] } @@ -473,21 +490,28 @@ func typeDisplayName(t *types.Type, c generatorConfig, typePkgMap map[*types.Typ s = tryDereference(t).Name.Name } - if t.Kind == types.Pointer { - s = strings.TrimLeft(s, "*") - } - switch t.Kind { case types.Struct, types.Interface, types.Alias, - types.Pointer, - types.Slice, types.Builtin: // noop + + case types.Pointer: + // Use the display name of the element of the pointer as the display name of the pointer. + return typeDisplayName(t.Elem, c, typePkgMap) + + case types.Slice: + // Use the display name of the element of the slice to build the display name of the slice. + elemName := typeDisplayName(t.Elem, c, typePkgMap) + return fmt.Sprintf("[]%s", elemName) + case types.Map: - // return original name - return t.Name.Name + // Use the display names of the key and element types of the map to build the display name of the map. + keyName := typeDisplayName(t.Key, c, typePkgMap) + elemName := typeDisplayName(t.Elem, c, typePkgMap) + return fmt.Sprintf("map[%s]%s", keyName, elemName) + case types.DeclarationOf: // For constants, we want to display the value // rather than the name of the constant, since the @@ -502,7 +526,9 @@ func typeDisplayName(t *types.Type, c generatorConfig, typePkgMap map[*types.Typ return *t.ConstValue } + klog.Fatalf("type %s is a non-const declaration, which is unhandled", t.Name) + default: klog.Fatalf("type %s has kind=%v which is unhandled", t.Name, t.Kind) } @@ -514,10 +540,6 @@ func typeDisplayName(t *types.Type, c generatorConfig, typePkgMap map[*types.Typ } } - if t.Kind == types.Slice { - s = "[]" + s - } - return s } @@ -591,7 +613,7 @@ func filterCommentTags(comments []string) []string { } func isOptionalMember(m types.Member) bool { - tags := types.ExtractCommentTags("+", m.CommentLines) + tags := gengo.ExtractCommentTags("+", m.CommentLines) _, ok := tags["optional"] return ok } @@ -663,7 +685,7 @@ func render(w io.Writer, pkgs []*apiPackage, config generatorConfig) error { "apiGroup": func(t *types.Type) string { return apiGroupForType(t, typePkgMap) }, "packageAnchorID": func(p *apiPackage) string { // TODO(ahmetb): currently this is the same as packageDisplayName - // func, and it's fine since it retuns valid DOM id strings like + // func, and it's fine since it returns valid DOM id strings like // 'serving.knative.dev/v1alpha1' which is valid per HTML5, except // spaces, so just trim those. return strings.Replace(p.identifier(), " ", "", -1) @@ -704,3 +726,23 @@ func render(w io.Writer, pkgs []*apiPackage, config generatorConfig) error { return nil } + +func getBuildInfo() (string, string, bool) { + var commitHash, commitTime string + var dirtyBuild bool + info, ok := debug.ReadBuildInfo() + if !ok { + return "", "", false + } + for _, kv := range info.Settings { + switch kv.Key { + case "vcs.revision": + commitHash = kv.Value + case "vcs.time": + commitTime = kv.Value + case "vcs.modified": + dirtyBuild = kv.Value == "true" + } + } + return commitHash, commitTime, dirtyBuild +}