Skip to content

Commit 74e0d8b

Browse files
authored
feat: version snippets (#8)
Generate a snippet with the latest release for each major version of the SDKs. These variables can be imported in any MDX file.
1 parent 0ee6fd7 commit 74e0d8b

7 files changed

Lines changed: 123 additions & 10 deletions

File tree

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ linters:
66
- mnd
77
- prealloc
88
- testpackage
9+
exclusions:
10+
warn-unused: true
11+
rules:
12+
- path: _test\.go
13+
linters:
14+
- funlen
915
formatters:
1016
enable:
1117
- gofmt

devbox.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"lockfile_version": "1",
33
"packages": {
44
"github:NixOS/nixpkgs/nixpkgs-unstable": {
5-
"last_modified": "2025-06-24T02:18:21Z",
6-
"resolved": "github:NixOS/nixpkgs/69dfebb3d175bde602f612915c5576a41b18486b?lastModified=1750731501&narHash=sha256-Ah4qq%2BSbwMaGkuXCibyg%2BFwn00el4KmI3XFX6htfDuk%3D"
5+
"last_modified": "2025-06-30T12:09:31Z",
6+
"resolved": "github:NixOS/nixpkgs/b9c03fbbaf84d85bb28eee530c7e9edc4021ca1b?lastModified=1751285371&narHash=sha256-%2FhDU%2B2AUeFFu5qGHO%2FUyFMc4UG%2Fx5Cw5uXO36KGTk6c%3D"
77
},
88
"go-task@3.43.3": {
99
"last_modified": "2025-06-20T02:24:11Z",

pkg/cmd/generate/sla/sla.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ import (
1616
)
1717

1818
type Options struct {
19-
DataFile string
20-
Output string
19+
DataFile string
20+
Output string
21+
VersionsFile string
2122
}
2223

2324
// VersionInfo represents the version information for a single version of an API client.
2425
type VersionInfo struct {
2526
ReleaseDate string `json:"releaseDate"`
2627
SlaStatus string `json:"slaStatus"`
2728
SlaEndDate string `json:"slaEndDate,omitempty"`
28-
// SupportStatus string `json:"supportStatus"`
29-
// SupportEndDate string `json:"supportEndDate,omitempty"`
3029
}
3130

3231
// Version maps a version string to its version information.
@@ -66,6 +65,8 @@ func NewSlaCommand() *cobra.Command {
6665
}
6766

6867
cmd.Flags().StringVarP(&opts.Output, "output", "o", "", "Output file")
68+
cmd.Flags().
69+
StringVarP(&opts.VersionsFile, "versions", "v", "", "Generate file with latest versions")
6970

7071
return cmd
7172
}
@@ -102,6 +103,18 @@ func runCommand(opts *Options) {
102103
if err = renderPage(output, pageTemplate, sorted, funcMap); err != nil {
103104
log.Fatalf("error: %v", err)
104105
}
106+
107+
var versionsOutput io.Writer
108+
if opts.VersionsFile == "" {
109+
versionsOutput = os.Stdout
110+
} else {
111+
versionsOutput, err = os.Create(opts.VersionsFile)
112+
if err != nil {
113+
log.Fatalf("error: %v", err)
114+
}
115+
}
116+
117+
renderVersionsFile(versionsOutput, sorted)
105118
}
106119

107120
// parseVersions reads a JSON file and parses it into a Clients struct.
@@ -177,3 +190,26 @@ func renderPage(
177190

178191
return nil
179192
}
193+
194+
func renderVersionsFile(w io.Writer, data []ClientEntry) {
195+
fmt.Fprintln(w, "export const sdkVersions = {")
196+
197+
for _, client := range data {
198+
fmt.Fprintf(w, " %s: {\n", client.Language)
199+
200+
seenMajors := make(map[string]bool)
201+
202+
for _, ver := range client.Versions {
203+
major := semver.Major("v" + ver.Version)
204+
if !seenMajors[major] {
205+
seenMajors[major] = true
206+
207+
fmt.Fprintf(w, " %s: \"%s\",\n", major, ver.Version)
208+
}
209+
}
210+
211+
fmt.Fprintln(w, " },")
212+
}
213+
214+
fmt.Fprintln(w, "};")
215+
}

pkg/cmd/generate/sla/sla_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sla
22

33
import (
4+
"bytes"
45
"reflect"
56
"testing"
67
)
@@ -53,3 +54,73 @@ func TestParseVersions(t *testing.T) {
5354
t.Errorf("parsed mismatch.\nGot: %#v\nExpected: %#v", got, expected)
5455
}
5556
}
57+
58+
func TestRenderVersionsFile(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
input []ClientEntry
62+
expected string
63+
}{
64+
{
65+
name: "multiple majors and patches",
66+
input: []ClientEntry{
67+
{
68+
Language: "csharp",
69+
Versions: []VersionEntry{
70+
{Version: "7.2.3"},
71+
{Version: "7.1.0"},
72+
{Version: "6.5.1"},
73+
{Version: "6.4.0"},
74+
},
75+
},
76+
{
77+
Language: "dart",
78+
Versions: []VersionEntry{
79+
{Version: "1.3.0"},
80+
{Version: "1.2.0"},
81+
},
82+
},
83+
},
84+
expected: `export const sdkVersions = {
85+
csharp: {
86+
v7: "7.2.3",
87+
v6: "6.5.1",
88+
},
89+
dart: {
90+
v1: "1.3.0",
91+
},
92+
};
93+
`,
94+
},
95+
{
96+
name: "single major only",
97+
input: []ClientEntry{
98+
{
99+
Language: "go",
100+
Versions: []VersionEntry{
101+
{Version: "2.0.0"},
102+
{Version: "2.0.0-beta"},
103+
},
104+
},
105+
},
106+
expected: `export const sdkVersions = {
107+
go: {
108+
v2: "2.0.0",
109+
},
110+
};
111+
`,
112+
},
113+
}
114+
115+
for _, tt := range tests {
116+
t.Run(tt.name, func(t *testing.T) {
117+
var buf bytes.Buffer
118+
119+
renderVersionsFile(&buf, tt.input)
120+
121+
if got := buf.String(); got != tt.expected {
122+
t.Errorf("renderVersionsFile() =\n%q\nwant\n%q", got, tt.expected)
123+
}
124+
})
125+
}
126+
}

pkg/cmd/generate/snippets/snippets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestSortLanguages(t *testing.T) {
114114
}
115115
}
116116

117-
func TestGenerateMarkdownSnippet(t *testing.T) { //nolint:funlen
117+
func TestGenerateMarkdownSnippet(t *testing.T) {
118118
tests := []struct {
119119
name string
120120
snippet map[string]string

pkg/cmd/generate/utils/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"gopkg.in/yaml.v3"
1010
)
1111

12-
func TestGetLanguageName(t *testing.T) { //nolint:funlen
12+
func TestGetLanguageName(t *testing.T) {
1313
tests := []struct {
1414
name string
1515
label string
@@ -305,7 +305,7 @@ func mockOp(extensions *yaml.Node) v3.Operation {
305305
return op
306306
}
307307

308-
func TestGetAcl(t *testing.T) { //nolint:funlen
308+
func TestGetAcl(t *testing.T) {
309309
tests := []struct {
310310
name string
311311
extensions *yaml.Node

pkg/cmd/update/cdn/cdn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
)
1212

13-
func TestReadData(t *testing.T) { //nolint:funlen
13+
func TestReadData(t *testing.T) {
1414
type want struct {
1515
packages []Package
1616
err bool

0 commit comments

Comments
 (0)