Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crdsdev/doc
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: cahillsf/doc
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Aug 30, 2023

  1. Copy the full SHA
    a734733 View commit details
  2. Copy the full SHA
    0ea63c5 View commit details
Showing with 52 additions and 32 deletions.
  1. +50 −32 cmd/doc/main.go
  2. +1 −0 go.mod
  3. +1 −0 go.sum
82 changes: 50 additions & 32 deletions cmd/doc/main.go
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ import (
"net/rpc"
"net/url"
"os"
"sort"
"strings"

crdutil "github.com/crdsdev/doc/pkg/crd"
@@ -36,6 +37,7 @@ import (
"github.com/jackc/pgx/v4/pgxpool"
flag "github.com/spf13/pflag"
"github.com/unrolled/render"
"golang.org/x/mod/semver"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/yaml"
@@ -291,40 +293,10 @@ func org(w http.ResponseWriter, r *http.Request) {
pageData := getPageData(r, fmt.Sprintf("%s/%s", org, repo), false)
fullRepo := fmt.Sprintf("%s/%s/%s", "github.com", org, repo)
b := &pgx.Batch{}
if tag == "" {
b.Queue("SELECT t.name, c.group, c.version, c.kind FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.id = (SELECT id FROM tags WHERE LOWER(repo) = LOWER($1) ORDER BY time DESC LIMIT 1);", fullRepo)
} else {
pageData.Title += fmt.Sprintf("@%s", tag)
b.Queue("SELECT t.name, c.group, c.version, c.kind FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.name=$2;", fullRepo, tag)
}
b.Queue("SELECT name FROM tags WHERE LOWER(repo)=LOWER($1) ORDER BY time DESC;", fullRepo)
br := db.SendBatch(context.Background(), b)
defer br.Close()
c, err := br.Query()
if err != nil {
log.Printf("failed to get CRDs for %s : %v", repo, err)
if err := page.HTML(w, http.StatusOK, "new", baseData{Page: pageData}); err != nil {
log.Printf("newTemplate.Execute(): %v", err)
fmt.Fprint(w, "Unable to render new template.")
}
return
}
repoCRDs := map[string]models.RepoCRD{}
foundTag := tag
for c.Next() {
var t, g, v, k string
if err := c.Scan(&t, &g, &v, &k); err != nil {
log.Printf("newTemplate.Execute(): %v", err)
fmt.Fprint(w, "Unable to render new template.")
}
foundTag = t
repoCRDs[g+"/"+v+"/"+k] = models.RepoCRD{
Group: g,
Version: v,
Kind: k,
}
}
c, err = br.Query()
if err != nil {
log.Printf("failed to get tags for %s : %v", repo, err)
if err := page.HTML(w, http.StatusOK, "new", baseData{Page: pageData}); err != nil {
@@ -335,6 +307,7 @@ func org(w http.ResponseWriter, r *http.Request) {
}
tags := []string{}
tagExists := false
followsSemver := false
for c.Next() {
var t string
if err := c.Scan(&t); err != nil {
@@ -344,6 +317,10 @@ func org(w http.ResponseWriter, r *http.Request) {
if !tagExists && t == tag {
tagExists = true
}
if semver.IsValid(t) {
log.Println("tag follows semver -- will select latest semver version if tag has not been specified in url params")
followsSemver = true
}
tags = append(tags, t)
}
if len(tags) == 0 || (!tagExists && tag != "") {
@@ -358,8 +335,49 @@ func org(w http.ResponseWriter, r *http.Request) {
}
return
}
if foundTag == "" {
foundTag = tags[0]
if tag == "" {
if len(tags) == 1 || !followsSemver {
tag = tags[0]
} else {
sort.SliceStable(tags, func(i, j int) bool {
switch res := semver.Compare(tags[i], tags[j]); res {
case -1:
return false
default:
return true
}
})
tag = tags[0]
}
} else {
pageData.Title += fmt.Sprintf("@%s", tag)
}
b = &pgx.Batch{}
b.Queue("SELECT t.name, c.group, c.version, c.kind FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.name=$2;", fullRepo, tag)
br = db.SendBatch(context.Background(), b)
defer br.Close()
c, err = br.Query()
if err != nil {
log.Printf("failed to get CRDs for %s : %v", repo, err)
if err := page.HTML(w, http.StatusOK, "new", baseData{Page: pageData}); err != nil {
log.Printf("newTemplate.Execute(): %v", err)
fmt.Fprint(w, "Unable to render new template.")
}
return
}
repoCRDs := map[string]models.RepoCRD{}
foundTag := tag
for c.Next() {
var t, g, v, k string
if err := c.Scan(&t, &g, &v, &k); err != nil {
log.Printf("error scanning in results: %v", err)
}
foundTag = t
repoCRDs[g+"/"+v+"/"+k] = models.RepoCRD{
Group: g,
Version: v,
Kind: k,
}
}
if err := page.HTML(w, http.StatusOK, "org", orgData{
Page: pageData,
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ require (
github.com/prometheus/client_golang v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/unrolled/render v1.0.3
golang.org/x/mod v0.3.0
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58 // indirect
golang.org/x/text v0.3.4 // indirect
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -556,6 +556,7 @@ golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=