terraform-provider-stackitp.../generator/cmd/publish/architecture.go
Marcel S. Henselin 5958474f9a
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 14s
CI Workflow / CI run build and linting (pull_request) Failing after 10s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / CI run tests (pull_request) Failing after 57s
fix: code cleanup
2026-03-02 10:32:58 +01:00

137 lines
3.4 KiB
Go

package publish
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"path"
"strings"
)
type Architecture struct {
Protocols []string `json:"protocols"`
OS string `json:"os"`
Arch string `json:"arch"`
FileName string `json:"filename"`
DownloadUrl string `json:"download_url"`
ShaSumsUrl string `json:"shasums_url"`
ShaSumsSignatureUrl string `json:"shasums_signature_url"`
ShaSum string `json:"shasum"`
SigningKeys SigningKey `json:"signing_keys"`
}
type SigningKey struct {
GpgPublicKeys []GpgPublicKey `json:"gpg_public_keys"`
}
type GpgPublicKey struct {
KeyId string `json:"key_id"`
AsciiArmor string `json:"ascii_armor"`
TrustSignature string `json:"trust_signature"`
Source string `json:"source"`
SourceUrl string `json:"source_url"`
}
func (p *Provider) CreateArchitectureFiles() error {
log.Println("* Creating architecture files in target directories")
prefix := path.Join("v1", "providers", p.Namespace, p.Provider, p.Version)
pathPrefix := path.Join("release", prefix)
urlPrefix, err := url.JoinPath("https://", p.Domain, prefix)
if err != nil {
return fmt.Errorf("error creating base url: %w", err)
}
downloadUrlPrefix, err := url.JoinPath(urlPrefix, "download")
if err != nil {
return fmt.Errorf("error crearting download url: %w", err)
}
downloadPathPrefix := path.Join(pathPrefix, "download")
shasumsUrl, err := url.JoinPath(urlPrefix, fmt.Sprintf("%s_%s_SHA256SUMS", p.RepoName, p.Version))
if err != nil {
return fmt.Errorf("error creating shasums url: %w", err)
}
shasumsSigUrl := shasumsUrl + ".sig"
gpgAsciiPub, err := p.ReadGpgFile()
if err != nil {
return err
}
shaSums, err := p.GetShaSums()
if err != nil {
return err
}
for _, sum := range shaSums {
downloadUrl, err := url.JoinPath(downloadUrlPrefix, sum.Path)
if err != nil {
return fmt.Errorf("error creating url: %w", err)
}
// get os and arch from filename
removeFileExtension := strings.Split(sum.Path, ".zip")
fileNameSplit := strings.Split(removeFileExtension[0], "_")
// Get build target and architecture from the zip file name
target := fileNameSplit[2]
arch := fileNameSplit[3]
// build filepath
archFileName := path.Join(downloadPathPrefix, target, arch)
a := Architecture{
Protocols: []string{"5.1", "6.0"},
OS: target,
Arch: arch,
FileName: sum.Path,
DownloadUrl: downloadUrl,
ShaSumsUrl: shasumsUrl,
ShaSumsSignatureUrl: shasumsSigUrl,
ShaSum: sum.Sum,
SigningKeys: SigningKey{},
}
a.SigningKeys = SigningKey{
GpgPublicKeys: []GpgPublicKey{
{
KeyId: p.GpgFingerprint,
AsciiArmor: gpgAsciiPub,
TrustSignature: "",
Source: "",
SourceUrl: "",
},
},
}
log.Printf(" - Arch file: %s", archFileName)
err = WriteArchitectureFile(archFileName, a)
if err != nil {
return err
}
}
return nil
}
func WriteArchitectureFile(filePath string, arch Architecture) error {
jsonString, err := json.Marshal(arch)
if err != nil {
return fmt.Errorf("error encoding data: %w", err)
}
//nolint:gosec // this file is not sensitive, so we can use os.ModePerm
err = os.WriteFile(
filePath,
jsonString,
os.ModePerm,
)
if err != nil {
return fmt.Errorf("error writing data: %w", err)
}
return nil
}