Skip to content

Commit 9aa656e

Browse files
committed
refactor: isolate types and code related to generation
1 parent e287f28 commit 9aa656e

File tree

3 files changed

+72
-60
lines changed

3 files changed

+72
-60
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ docs/static/demo.gif: FORCE
9090

9191
assets/github-action-config.json: FORCE $(BINARY)
9292
# go run ./scripts/gen_github_action_config/main.go $@
93-
cd ./scripts/gen_github_action_config/; go run ./main.go ../../$@
93+
cd ./scripts/gen_github_action_config/; go run . ../../$@
9494

9595
go.mod: FORCE
9696
go mod tidy

scripts/gen_github_action_config/main.go

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,68 +16,13 @@ import (
1616

1717
const noPatch = -1
1818

19-
type logInfo struct {
20-
Warning string `json:",omitempty"`
21-
Info string `json:",omitempty"`
22-
}
23-
24-
type versionConfig struct {
25-
Error string `json:",omitempty"`
26-
27-
Log *logInfo `json:",omitempty"`
28-
29-
TargetVersion string `json:",omitempty"`
30-
AssetURL string `json:",omitempty"`
31-
}
32-
33-
type actionConfig struct {
34-
MinorVersionToConfig map[string]versionConfig
35-
}
36-
37-
type version struct {
38-
major, minor, patch int
39-
}
40-
41-
func (v version) String() string {
42-
ret := fmt.Sprintf("v%d.%d", v.major, v.minor)
43-
44-
if v.patch != noPatch {
45-
ret += fmt.Sprintf(".%d", v.patch)
46-
}
47-
48-
return ret
49-
}
50-
51-
func (v version) isAfterOrEq(vv *version) bool {
52-
if v.major != vv.major {
53-
return v.major >= vv.major
54-
}
55-
56-
if v.minor != vv.minor {
57-
return v.minor >= vv.minor
58-
}
59-
60-
return v.patch >= vv.patch
61-
}
62-
63-
type release struct {
64-
TagName string
65-
ReleaseAssets struct {
66-
Nodes []releaseAsset
67-
} `graphql:"releaseAssets(first: 50)"`
68-
}
69-
70-
type releaseAsset struct {
71-
DownloadURL string
72-
}
73-
7419
func main() {
75-
if err := generate(context.Background()); err != nil {
20+
if err := run(context.Background()); err != nil {
7621
log.Fatal(err)
7722
}
7823
}
7924

80-
func generate(ctx context.Context) error {
25+
func run(ctx context.Context) error {
8126
if len(os.Args) != 2 {
8227
return fmt.Errorf("usage: go run .../main.go out-path.json")
8328
}
@@ -87,15 +32,24 @@ func generate(ctx context.Context) error {
8732
return fmt.Errorf("failed to fetch all releases: %w", err)
8833
}
8934

35+
dest := os.Args[1]
36+
9037
// https://github.com/golangci/golangci-lint-action/blob/5421a116d2bf2a1d53595d0dca7da6e18bd1cfd7/src/version.ts#L43-L47
91-
minAllowedVersion := version{major: 1, minor: 28, patch: 3}
38+
err = generate(allReleases, version{major: 1, minor: 28, patch: 3}, dest)
39+
if err != nil {
40+
return fmt.Errorf("failed to generate v1: %w", err)
41+
}
42+
43+
return nil
44+
}
9245

46+
func generate(allReleases []release, minAllowedVersion version, dest string) error {
9347
cfg, err := buildConfig(allReleases, minAllowedVersion)
9448
if err != nil {
9549
return fmt.Errorf("failed to build config: %w", err)
9650
}
9751

98-
outFile, err := os.Create(os.Args[1])
52+
outFile, err := os.Create(dest)
9953
if err != nil {
10054
return fmt.Errorf("failed to create output config file: %w", err)
10155
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type logInfo struct {
6+
Warning string `json:",omitempty"`
7+
Info string `json:",omitempty"`
8+
}
9+
10+
type versionConfig struct {
11+
Error string `json:",omitempty"`
12+
13+
Log *logInfo `json:",omitempty"`
14+
15+
TargetVersion string `json:",omitempty"`
16+
AssetURL string `json:",omitempty"`
17+
}
18+
19+
type actionConfig struct {
20+
MinorVersionToConfig map[string]versionConfig
21+
}
22+
23+
type version struct {
24+
major, minor, patch int
25+
}
26+
27+
func (v version) String() string {
28+
ret := fmt.Sprintf("v%d.%d", v.major, v.minor)
29+
30+
if v.patch != noPatch {
31+
ret += fmt.Sprintf(".%d", v.patch)
32+
}
33+
34+
return ret
35+
}
36+
37+
func (v version) isAfterOrEq(vv *version) bool {
38+
if v.major != vv.major {
39+
return v.major >= vv.major
40+
}
41+
42+
if v.minor != vv.minor {
43+
return v.minor >= vv.minor
44+
}
45+
46+
return v.patch >= vv.patch
47+
}
48+
49+
type release struct {
50+
TagName string
51+
ReleaseAssets struct {
52+
Nodes []releaseAsset
53+
} `graphql:"releaseAssets(first: 50)"`
54+
}
55+
56+
type releaseAsset struct {
57+
DownloadURL string
58+
}

0 commit comments

Comments
 (0)