Skip to content

Commit 0a77c9c

Browse files
committed
chore: add the tools folder
1 parent f14ffd8 commit 0a77c9c

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/MicahParks/keyfunc/v3 v3.7.0
88
github.com/golang-jwt/jwt/v5 v5.3.0
99
github.com/google/go-cmp v0.7.0
10+
github.com/google/go-github/v83 v83.0.0
1011
github.com/google/uuid v1.6.0
1112
github.com/hashicorp/go-hclog v1.6.3
1213
github.com/hashicorp/go-plugin v1.7.0
@@ -37,6 +38,7 @@ require (
3738
github.com/go-ole/go-ole v1.2.6 // indirect
3839
github.com/gobwas/glob v0.2.3 // indirect
3940
github.com/golang/protobuf v1.5.4 // indirect
41+
github.com/google/go-querystring v1.2.0 // indirect
4042
github.com/hashicorp/hcl/v2 v2.24.0 // indirect
4143
github.com/hashicorp/yamux v0.1.2 // indirect
4244
github.com/inconshreveable/mousetrap v1.1.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9v
4545
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
4646
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
4747
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
48+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4849
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4950
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
51+
github.com/google/go-github/v83 v83.0.0 h1:Ydy4gAfqxrnFUwXAuKl/OMhhGa0KtMtnJ3EozIIuHT0=
52+
github.com/google/go-github/v83 v83.0.0/go.mod h1:gbqarhK37mpSu8Xy7sz21ITtznvzouyHSAajSaYCHe8=
53+
github.com/google/go-querystring v1.2.0 h1:yhqkPbu2/OH+V9BfpCVPZkNmUXhb2gBxJArfhIxNtP0=
54+
github.com/google/go-querystring v1.2.0/go.mod h1:8IFJqpSRITyJ8QhQ13bmbeMBDfmeEJZD5A0egEOmkqU=
5055
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
5156
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5257
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

tools/update-manifest/main.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
"regexp"
10+
"strings"
11+
12+
"github.com/google/go-github/v83/github"
13+
"github.com/infracost/cli/internal/config"
14+
"github.com/infracost/cli/pkg/plugins"
15+
)
16+
17+
type Config struct {
18+
Target string `env:"UPDATE_MANIFEST_PATH" default:"docs/manifest.json"`
19+
20+
Plugin string `env:"UPDATE_MANIFEST_PLUGIN"`
21+
Version string `env:"UPDATE_MANIFEST_VERSION"`
22+
23+
GithubToken string `env:"GITHUB_TOKEN"`
24+
25+
Latest bool `env:"UPDATE_MANIFEST_LATEST" default:"true"`
26+
}
27+
28+
func main() {
29+
var cfg Config
30+
if diags := config.Process(&cfg, nil); diags.Critical().Len() > 0 {
31+
_, _ = fmt.Fprintln(os.Stderr, diags)
32+
os.Exit(1)
33+
}
34+
35+
switch {
36+
case cfg.Plugin == "":
37+
_, _ = fmt.Fprintln(os.Stderr, "Plugin must be specified")
38+
os.Exit(1)
39+
case cfg.Version == "":
40+
_, _ = fmt.Fprintln(os.Stderr, "Version must be specified")
41+
os.Exit(1)
42+
case cfg.GithubToken == "":
43+
_, _ = fmt.Fprintln(os.Stderr, "Github token must be specified")
44+
os.Exit(1)
45+
}
46+
47+
_, _ = fmt.Fprintf(os.Stderr, "Updating %s to %s at %s.\n", cfg.Plugin, cfg.Version, cfg.Target)
48+
49+
var manifest plugins.Manifest
50+
if data, err := os.ReadFile(cfg.Target); err != nil {
51+
if !os.IsNotExist(err) {
52+
_, _ = fmt.Fprintf(os.Stderr, "Error reading manifest: %v\n", err)
53+
os.Exit(1)
54+
}
55+
} else {
56+
if err := json.Unmarshal(data, &manifest); err != nil {
57+
_, _ = fmt.Fprintf(os.Stderr, "Error parsing manifest: %v\n", err)
58+
os.Exit(1)
59+
}
60+
}
61+
62+
version := strings.TrimPrefix(cfg.Version, "v") // 0.1.0
63+
tag := fmt.Sprintf("%s/v%s", cfg.Plugin, version) // infracost-parser-plugin/v0.1.0
64+
65+
client := github.NewClient(nil).WithAuthToken(cfg.GithubToken)
66+
release, _, err := client.Repositories.GetReleaseByTag(context.Background(), "infracost", "cli", tag)
67+
if err != nil {
68+
_, _ = fmt.Fprintf(os.Stderr, "Error fetching release: %v\n", err)
69+
os.Exit(1)
70+
}
71+
72+
if manifest.Plugins == nil {
73+
manifest.Plugins = make(map[string]plugins.Plugin)
74+
}
75+
76+
plugin, ok := manifest.Plugins[cfg.Plugin]
77+
if !ok {
78+
plugin = plugins.Plugin{
79+
Latest: version, // if we're making a completely fresh one, then it's definitely latest.
80+
Versions: make(map[string]plugins.Version),
81+
}
82+
}
83+
84+
re := regexp.MustCompile(fmt.Sprintf(`^%s_%s_([^_]+_[^.]+)\.`, regexp.QuoteMeta(cfg.Plugin), regexp.QuoteMeta(version)))
85+
86+
v := plugins.Version{
87+
Artifacts: make(map[string]plugins.Artifact),
88+
}
89+
for _, asset := range release.Assets {
90+
matches := re.FindStringSubmatch(asset.GetName())
91+
if matches == nil {
92+
continue
93+
}
94+
osArch := matches[1]
95+
96+
a := plugins.Artifact{
97+
URL: fmt.Sprintf("https://api.github.com/repos/infracost/cli/releases/assets/%d", asset.GetID()),
98+
SHA: strings.TrimPrefix(asset.GetDigest(), "sha256:"),
99+
Name: asset.GetName(),
100+
}
101+
v.Artifacts[osArch] = a
102+
_, _ = fmt.Fprintf(os.Stderr, " %s: url=%s sha=%s\n", osArch, a.URL, a.SHA)
103+
}
104+
plugin.Versions[version] = v
105+
106+
if cfg.Latest {
107+
plugin.Latest = version
108+
}
109+
110+
manifest.Plugins[cfg.Plugin] = plugin
111+
112+
data, err := json.Marshal(manifest)
113+
if err != nil {
114+
_, _ = fmt.Fprintf(os.Stderr, "Error serializing manifest: %v\n", err)
115+
os.Exit(1)
116+
}
117+
118+
if err := os.MkdirAll(filepath.Dir(cfg.Target), 0750); err != nil {
119+
_, _ = fmt.Fprintf(os.Stderr, "Error creating directory: %v\n", err)
120+
os.Exit(1)
121+
}
122+
123+
if err := os.WriteFile(cfg.Target, data, 0600); err != nil {
124+
_, _ = fmt.Fprintf(os.Stderr, "Error writing manifest: %v\n", err)
125+
os.Exit(1)
126+
}
127+
}

0 commit comments

Comments
 (0)