Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 17s
CI Workflow / Prepare GO cache (pull_request) Successful in 2m10s
CI Workflow / CI run build and linting (pull_request) Successful in 7m10s
CI Workflow / Code coverage report (pull_request) Successful in 4s
CI Workflow / CI run tests (pull_request) Failing after 9m13s
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 16m53s
120 lines
2 KiB
Go
120 lines
2 KiB
Go
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
func FileExists(pathValue string) bool {
|
|
_, err := os.Stat(pathValue)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func ucfirst(s string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
return strings.ToUpper(s[:1]) + s[1:]
|
|
}
|
|
|
|
func writeTemplateToFile(tplName, tplFile, outFile string, data *templateData) error {
|
|
fn := template.FuncMap{
|
|
"ucfirst": ucfirst,
|
|
}
|
|
|
|
tmpl, err := template.New(tplName).Funcs(fn).ParseFiles(tplFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var f *os.File
|
|
f, err = os.Create(outFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tmpl.Execute(f, *data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = f.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/* saved for later
|
|
func deleteFiles(fNames ...string) error {
|
|
for _, fName := range fNames {
|
|
if _, err := os.Stat(fName); !os.IsNotExist(err) {
|
|
err = os.Remove(fName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return 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 func(source *os.File) {
|
|
err := source.Close()
|
|
if err != nil {
|
|
slog.Error("copyFile", "err", err)
|
|
}
|
|
}(source)
|
|
|
|
destination, err := os.Create(dst)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer func(destination *os.File) {
|
|
err := destination.Close()
|
|
if err != nil {
|
|
slog.Error("copyFile", "err", err)
|
|
}
|
|
}(destination)
|
|
nBytes, err := io.Copy(destination, source)
|
|
return nBytes, err
|
|
}
|
|
*/
|
|
|
|
func checkCommands(commands []string) error {
|
|
for _, commandName := range commands {
|
|
if !commandExists(commandName) {
|
|
return fmt.Errorf("missing command %s", commandName)
|
|
}
|
|
slog.Info(" found", "command", commandName)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func commandExists(cmd string) bool {
|
|
_, err := exec.LookPath(cmd)
|
|
return err == nil
|
|
}
|