package build import ( "fmt" "io" "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 } 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 }