Skip to content

Instantly share code, notes, and snippets.

@niamtokik
Created February 7, 2017 14:57
Show Gist options
  • Save niamtokik/4ff9450f1fa2668ce1fb6ec1be999838 to your computer and use it in GitHub Desktop.
Save niamtokik/4ff9450f1fa2668ce1fb6ec1be999838 to your computer and use it in GitHub Desktop.
xbps-src templating in go
package main
import "fmt"
import "os"
import "flag"
import "strconv"
import "strings"
import "text/template"
import "crypto/sha256"
import "encoding/hex"
func usage() {
fmt.Printf("Usage: %s %s\n",
os.Args[0],
"opts")
os.Exit(1)
}
func setflag(s interface{}, e string, r bool) {
switch s := s.(type) {
case string:
setstring(s, e, r)
case int:
setint(s, e, r)
default:
fmt.Printf("%s\n", s)
}
}
func setstring(s string, e string, r bool) {
var env string
env = "XTEMPLATE_" + strings.ToUpper(e)
if s == "" {
if os.Getenv(env) != "" {
s = os.Getenv(strings.ToUpper(env))
} else {
if r == true {
fmt.Printf("%s is required\n", e)
os.Exit(1)
}
}
}
}
func setint(s int, e string, r bool) {
var env string
env = "XTEMPLATE_" + strings.ToUpper(e)
if s == 0 {
if os.Getenv(env) != "" {
s, _ = strconv.Atoi(os.Getenv(env))
} else {
if r == true {
fmt.Printf("%s is required\n", e)
os.Exit(1)
}
}
}
}
func template_file() string{
return `# Template file for '{{.pkgname}}'
pkgname="{{.pkgname}}"
version="{{.version}}"
revision="{{.revision}}"
short_desc="{{.shortdesc}}"
maintainer="{{.maintainer}}"
license="{{.license}}"
homepage="{{.homepage}}"
distfiles="{{.distfiles}}"
checksum="{{.checksum}}"
{{if .buildstyle}}build_style={{.buildstyle}}
{{end}}
`
}
func ret(opts map[string]interface{}) {
t := template.Must(template.New("temp").Parse(template_file()))
t.Execute(os.Stdout, opts)
}
func args() {
usageFlag := flag.Bool("usage", false, "return help")
homepageFlag := flag.String("homepage", "", "homepage project")
licenseFlag := flag.String("license", "", "project licence")
maintainerFlag := flag.String("maintainer", "", "template maintainer")
pkgnameFlag := flag.String("pkgname", "", "package name")
revisionFlag := flag.Int("revision", 1, "template revision")
shortdescFlag := flag.String("shortdesc", "", "short description of the project")
versionFlag := flag.String("version", "", "project version")
buildstyleFlag := flag.String("buildstyle", "", "build style helper")
distfilesFlag := flag.String("distfiles", "", "package target")
checksumFlag := flag.String("checksum", "", "package checksum")
//outputFlag := flag.String("output", "", "output")
flag.Parse()
if *usageFlag == true {
usage()
}
setflag(*homepageFlag, "homepage", true)
setflag(*licenseFlag, "license", true)
setflag(*maintainerFlag, "maintainer", true)
setflag(*pkgnameFlag, "pkgname", true)
setflag(*revisionFlag, "revision", true)
setflag(*shortdescFlag, "shortdesc", true)
setflag(*versionFlag, "version", true)
setflag(*buildstyleFlag, "buildstyle", false)
if *distfilesFlag != "" && *checksumFlag == "" {
fmt.Println("getfile")
sha := sha256.Sum256([]byte(*distfilesFlag))
shas := sha[:]
enc := hex.EncodeToString(shas)
fmt.Println(enc)
*checksumFlag = enc
}
var opts = map[string]interface{} {
"homepage": *homepageFlag,
"license": *licenseFlag,
"maintainer": *maintainerFlag,
"pkgname": *pkgnameFlag,
"revision": *revisionFlag,
"shortdesc": *shortdescFlag,
"version": *versionFlag,
"buildstyle": *buildstyleFlag,
"distfiles": *distfilesFlag,
"checksum": *checksumFlag,
}
ret(opts)
}
func main() {
args()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment