terraform-provider-stackitp.../cmd/cmd/publish/provider.go
Marcel S. Henselin 3a2daf9c56
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 13s
CI Workflow / CI (pull_request) Failing after 29s
Publish / Check GoReleaser config (pull_request) Has been skipped
CI Workflow / Code coverage report (pull_request) Has been skipped
Publish / Publish provider (pull_request) Has been skipped
fix: refactor publish command
feat: add connection info

fix: prevent postgresql from failing when encryption is empty
2026-01-30 11:15:10 +01:00

233 lines
5.3 KiB
Go

package publish
import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"path"
"strings"
)
type Provider struct {
Namespace string
Provider string
DistPath string
RepoName string
Version string
GpgFingerprint string
GpgPubKeyFile string
Domain string
}
func (p *Provider) CreateV1Dir() error {
// Path to semantic version dir
versionPath := p.providerDirs()
// Files to create under v1/providers/[namespace]/[provider_name]
err := p.createVersionsFile()
if err != nil {
return fmt.Errorf("[CreateV1Dir] - create versions file:%w", err)
} // Creates version file one above download, which is why downloadPath isn't used
// Files/Directories to create under v1/providers/[namespace]/[provider_name]/[version]
err = p.copyShaFiles(versionPath)
if err != nil {
return fmt.Errorf("[CreateV1Dir] - copy sha files: %w", err)
}
log.Printf("* Creating download/ in %s directory", versionPath)
downloadsPath := path.Join(versionPath, "download")
err = CreateDir(downloadsPath)
if err != nil {
return err
}
// Create darwin, freebsd, linux, windows dirs
for _, v := range [4]string{"darwin", "freebsd", "linux", "windows"} {
err = CreateDir(path.Join(downloadsPath, v))
if err != nil {
return fmt.Errorf("error creating dir '%s': %w", path.Join(downloadsPath, v), err)
}
}
// Copy all zips
err = p.copyBuildZips(downloadsPath)
if err != nil {
return err
}
// Create all individual files for build targets and each architecture for the build targets
err = p.CreateArchitectureFiles()
if err != nil {
return err
}
return nil
}
func (p *Provider) copyBuildZips(destPath string) error {
log.Println("* Copying build zips")
shaSums, err := p.GetShaSums()
if err != nil {
return err
}
// Loop through and copy each
for _, sum := range shaSums {
zipSrcPath := path.Join(p.DistPath, sum.Path)
zipDestPath := path.Join(destPath, sum.Path)
log.Printf(" - Zip Source: %s", zipSrcPath)
log.Printf(" - Zip Dest: %s", zipDestPath)
// Copy the zip
_, err = CopyFile(zipSrcPath, zipDestPath)
if err != nil {
return fmt.Errorf("error copying file '%s': %w", zipSrcPath, err)
}
}
return nil
}
func (p *Provider) copyShaFiles(destPath string) error {
log.Printf("* Copying SHA files in %s directory", p.DistPath)
// Copy files from srcPath
shaSum := p.RepoName + "_" + p.Version + "_SHA256SUMS"
shaSumPath := path.Join(p.DistPath, shaSum)
// _SHA256SUMS file
_, err := CopyFile(shaSumPath, path.Join(destPath, shaSum))
if err != nil {
return err
}
// _SHA256SUMS.sig file
_, err = CopyFile(shaSumPath+".sig", path.Join(destPath, shaSum+".sig"))
if err != nil {
return err
}
return nil
}
func (p *Provider) createVersionsFile() error {
log.Println("* Writing to release/v1/providers/[namespace]/[repo]/versions file")
versionPath := path.Join("release", "v1", "providers", p.Namespace, p.Provider, "versions")
shasums, err := p.GetShaSums()
if err != nil {
return fmt.Errorf("error getting sha sums: %w", err)
}
// Build the versions file...
version := Version{}
for _, sum := range shasums {
// 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]
version.Platforms = append(version.Platforms, Platform{
OS: target,
Arch: arch,
})
}
data := Data{}
downloadPath := path.Join(p.Domain, "v1", "providers", p.Namespace, p.Provider, "versions")
err = data.LoadFromUrl(fmt.Sprintf("https://%s", downloadPath))
if err != nil {
return fmt.Errorf("error getting existing versions file: %w", err)
}
err = data.AddVersion(version)
if err != nil {
return fmt.Errorf("error appending version: %w", err)
}
err = data.WriteToFile(versionPath)
if err != nil {
return fmt.Errorf("error saving file '%s':%w", versionPath, err)
}
return nil
}
func (p *Provider) providerDirs() string {
log.Println("* Creating release/v1/providers/[namespace]/[repo]/[version] directories")
target := path.Join("release", "v1", "providers", p.Namespace, p.RepoName, p.Version)
err := CreateDir(target)
if err != nil {
return ""
}
return target
}
func CreateDir(path string) error {
log.Printf("* Creating %s directory", path)
err := os.MkdirAll(path, os.ModePerm)
if errors.Is(err, fs.ErrExist) {
return nil
}
return err
}
func ReadFile(filePath string) ([]string, error) {
rFile, err := os.Open(filePath)
if err != nil {
return nil, err
}
fileScanner := bufio.NewScanner(rFile)
fileScanner.Split(bufio.ScanLines)
var fileLines []string
for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}
err = rFile.Close()
if err != nil {
return nil, err
}
return fileLines, nil
}
func CopyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}