WIP: Rewrite bpm utils in go #1

Draft
EnumDev wants to merge 8 commits from go_rewrite into master
Showing only changes of commit 23b5f0eee1 - Show all commits

View File

@ -1,9 +1,15 @@
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
var directory = flag.String("D", "", "Path to package directory")
@ -13,7 +19,7 @@ var version = flag.String("v", "1.0", "Set the package version (Defaults to \"1.
var url = flag.String("u", "", "Set the package URL (Optional)")
var license = flag.String("l", "", "Set the package licenses (Optional)")
var template = flag.String("t", "source.default", "Set the package template (Defaults to \"source.default\")")
var git = flag.String("g", "", "Create git repository (Defaults to true)")
var git = flag.Bool("g", true, "Create git repository (Defaults to true)")
func main() {
// Setup flags
@ -24,6 +30,21 @@ func main() {
help()
os.Exit(1)
}
// Show summary
showSummary()
// Confirmation prompt
fmt.Printf("Create package directory? [y\\N]: ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
fmt.Println("Cancelling package directory creation...")
os.Exit(1)
}
// Create package directory
createDirectory()
}
func setupFlags() {
@ -46,3 +67,79 @@ func help() {
fmt.Println(" -t=<template file> | Use a template file (Defaults to source.default)")
fmt.Println(" -g=<true/false> | Create git repository (Defaults to true)")
}
func showSummary() {
absPath, err := filepath.Abs(*directory)
if err != nil {
log.Fatalf("Failed to determine absolute path: %s", err)
}
fmt.Printf("Setting up package directory at %s with the following information:\n", absPath)
fmt.Printf("Package name: %s\n", *name)
fmt.Printf("Package description: %s\n", *description)
fmt.Printf("Package version: %s\n", *version)
if url != nil && *url != "" {
fmt.Printf("Package URL: %s\n", *url)
} else {
fmt.Printf("Package URL: Not Set\n")
}
if license != nil && *license != "" {
fmt.Printf("Package license: %s\n", *license)
} else {
fmt.Printf("Package license: Not Set\n")
}
fmt.Printf("Template file: %s\n", *template)
fmt.Printf("Create git repository: %t\n", *git)
}
func createDirectory() {
// Create directory
err := os.Mkdir(*directory, 0755)
if err != nil {
log.Fatalf("Error: could not create directory: %s", err)
}
// Create pkg.info contents string
pkgInfo := "name: " + *name + "\n"
pkgInfo += "description: " + *description + "\n"
pkgInfo += "version: " + *version + "\n"
if url != nil && *url != "" {
pkgInfo += "url: " + *url + "\n"
}
if license != nil && *license != "" {
pkgInfo += "license: " + *license + "\n"
}
pkgInfo += "architecture: any\n"
pkgInfo += "type: source\n"
// Write string to file
err = os.WriteFile(path.Join(*directory, "pkg.info"), []byte(pkgInfo), 0644)
if err != nil {
log.Fatalf("Could not write to pkg.info: %s", err)
}
// Create source-files directory
err = os.Mkdir(path.Join(*directory, "source-files"), 0755)
if err != nil {
log.Fatalf("Error: could not create directory: %s", err)
}
// Copy source template file
input, err := os.ReadFile(path.Join("/etc/bpm-utils/", *template))
if err != nil {
log.Fatalf("Error: could not read template file: %s", err)
return
}
err = os.WriteFile(path.Join(*directory, "source.sh"), input, 0644)
if err != nil {
log.Fatalf("Error: could not write to source file: %s", err)
return
}
// Create git repository
if git != nil && *git {
err = exec.Command("git", "init", *directory).Run()
if err != nil {
log.Fatalf("Error: could not initialize git repository: %s", err)
}
}
}