cert-manager-webhook-sthome/cmd/buildversion.go

79 lines
2.0 KiB
Go

package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
const (
version = "0.0.3-alpha"
chartfile = "./deploy/sthome-webhook/Chart.yaml"
valuesfile = "./deploy/sthome-webhook/values.yaml"
tagprefix = " tag: "
versiontxt = "./version.txt"
apiVersion = "v1"
description = "Cert-Manager webhook for sthome"
name = "sthome-webhook"
)
var (
mfimagetag string
vfimagetag string
buildTime string
appVersion string
longversion string
versiontext 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")
// Load the count from the 3rd line of the file
// It's a string so we need to convert to integer
// Then increment it by 1
bNum, _ := strconv.Atoi(vLines[2])
bNum++
longversion = version + "." + fmt.Sprint(bNum)
mfimagetag = longversion
appVersion = "v" + longversion
// Generate a single string to write back to the file
versiontext = 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(versiontxt, []byte(versiontext), 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)
}
}