cert-manager-webhook-sthome/cmd/buildversion.go
2024-04-06 12:39:47 +02:00

77 lines
2.0 KiB
Go

package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
const (
chartfile = "./deploy/sthome-webhook/Chart.yaml"
valuesfile = "./deploy/sthome-webhook/values.yaml"
tagprefix = " tag: "
vertxtfile = "./version.txt"
apiVersion = "v1"
description = "Cert-Manager webhook for sthome"
name = "sthome-webhook"
)
var (
buildTime string
appVersion string
longversion string
multilineversion string
)
func main() {
// Load the file content
vFileData, _ := os.ReadFile("version.txt")
// Convert from Byte array to string and split
// on newlines. We now have a slice of strings
vLines := strings.Split(string(vFileData), "\n")
// Generate a timestamp.
buildTime = time.Now().Format("20060102-1504")
// deconstruct version line and remove build increment part
verparts := strings.Split(vLines[0], "-")
versionmins := strings.Split(verparts[1], ".")
version := verparts[0] + "-" + versionmins[0]
bNum, _ := strconv.Atoi(vLines[2])
bNum++
longversion = version + "." + fmt.Sprint(bNum)
appVersion = "v" + longversion
// Generate a single string to write back to the file
multilineversion = longversion + "\n" + buildTime + "\n" + fmt.Sprint(bNum)
chartStr := "apiVersion: " + apiVersion + "\nappVersion: " + appVersion + "\ndescription: " + description + "\nname: " + name + "\nversion: " + longversion + "\n"
// Write the data back to the file.
_ = os.WriteFile(vertxtfile, []byte(multilineversion), 0777)
_ = os.WriteFile(chartfile, []byte(chartStr), 0777)
replacetxtfilelines(valuesfile, tagprefix, tagprefix+longversion)
}
func replacetxtfilelines(filename string, textLinePrefix string, replacetext string) {
input, err := os.ReadFile(filename)
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.HasPrefix(line, textLinePrefix) {
lines[i] = replacetext
}
}
output := strings.Join(lines, "\n")
err = os.WriteFile(filename, []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}
}