git-docker/scripts/includetext.go

88 lines
1.5 KiB
Go
Raw Normal View History

2019-04-11 05:03:12 +00:00
// +build ignore
package main
import (
"bytes"
"flag"
"io/ioutil"
"log"
2019-04-11 05:10:20 +00:00
"path/filepath"
2019-04-11 05:03:12 +00:00
"strings"
"text/template"
)
var (
input stringSlice
output string
name string
)
func main() {
flag.Var(&input, "input", "input files")
flag.StringVar(&output, "output", "", "output file")
flag.StringVar(&name, "package", "", "package name")
flag.Parse()
var files []File
for _, file := range input {
out, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalln(err)
}
files = append(files, File{
Name: file,
2019-04-11 05:10:20 +00:00
Slug: slugify(file),
2019-04-11 05:03:12 +00:00
Data: string(out),
})
}
data := map[string]interface{}{
"Files": files,
"Package": name,
}
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, data)
if err != nil {
log.Fatalln(err)
}
ioutil.WriteFile(output, buf.Bytes(), 0644)
}
2019-04-11 05:10:20 +00:00
func slugify(s string) string {
ext := filepath.Ext(s)
s = strings.TrimSuffix(s, ext)
s = strings.Title(s)
s = strings.ReplaceAll(s, "-", "")
s = strings.ReplaceAll(s, "_", "")
return s
}
2019-04-11 05:03:12 +00:00
type stringSlice []string
func (s *stringSlice) String() string {
return strings.Join(*s, ",")
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
type File struct {
Name string
Data string
Slug string
}
var tmpl = template.Must(template.New("_").Parse(`package {{ .Package }}
// DO NOT EDIT. This file is automatically generated.
{{ range .Files -}}
// Contents of {{ .Name }}
const {{ .Slug }} = ` + "`{{ .Data }}`" + `
2019-04-11 05:10:20 +00:00
{{ end -}}`))