|
| 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