summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore7
-rw-r--r--Makefile12
-rw-r--r--html.go22
-rw-r--r--main.go93
-rw-r--r--text.go22
5 files changed, 156 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..15bc182
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,7 @@
+tg
+*.exe
+*.a
+*.o
+*.gz
+*.tar
+test/
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..43fa093
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+APP=tg
+SOURCES=$(wildcard *.go) $(GENERATED)
+
+all: build
+
+build: tg
+
+tg: $(SOURCES)
+ go build -o $(APP)
+
+clean:
+ $(RM) $(RMFLAG) $(APP)
diff --git a/html.go b/html.go
new file mode 100644
index 0000000..534132a
--- /dev/null
+++ b/html.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+ "html/template"
+ "log"
+ "os"
+)
+
+func ParseHTML(templates []string, data interface{}) {
+ // Parse the template dir
+ tmpl := template.Must(template.New("template").ParseGlob(*template_dir_f + "/*" + *ext_f))
+ for _, template := range templates {
+ if template == "" {
+ continue
+ }
+
+ err := tmpl.ExecuteTemplate(os.Stdout, template, data)
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+}
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..8ac03ef
--- /dev/null
+++ b/main.go
@@ -0,0 +1,93 @@
+package main
+
+import (
+ "encoding/json"
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "strings"
+)
+
+var (
+ conf_f = flag.String("c", "", "Path to the configuration file.")
+ template_dir_f = flag.String("d", ".", "Template directory.")
+ template_f = flag.String("f", "", "Templates to render, a comma separated list.")
+ ext_f = flag.String("e", ".tmpl", "Extension of template files")
+ is_html_f = flag.Bool("html", false, "Enable HTML context sensitivy.")
+)
+
+func main() {
+ flag.Parse()
+
+ if *template_f == "" {
+ fmt.Println("Please provide a template to render with the -f option.")
+ os.Exit(1)
+ }
+
+ // Reading config from environment if not set (case insensitive)
+ *conf_f = GetVarFromEnv(*conf_f, "config")
+ *template_dir_f = GetVarFromEnv(*template_dir_f, "template_directory")
+ *template_f = GetVarFromEnv(*template_f, "templates")
+
+ // Read configuration file with the template data
+ data := NewConfiguration(*conf_f)
+
+ // Splits the templates
+ var templates []string
+ if strings.Contains(*template_f, ",") {
+ templates = strings.Split(*template_f, ",")
+ } else {
+ templates = strings.Split(*template_f, " ")
+ }
+
+ // Clean out the filenames if they start with the template dirname (shell globbing)
+ for index, template := range templates {
+ if strings.HasPrefix(template, *template_dir_f+"/") {
+ templates[index] = strings.TrimPrefix(template, *template_dir_f+"/")
+ } else {
+ templates[index] = strings.TrimPrefix(template, *template_dir_f)
+ }
+ }
+
+ // Enable HTML context sensitivy on demand
+ if *is_html_f {
+ ParseHTML(templates, data)
+ } else {
+ ParseText(templates, data)
+ }
+}
+
+func NewConfiguration(path string) interface{} {
+ if path == "" {
+ return nil
+ }
+ file, err := os.Open(path)
+ if err != nil {
+ log.Print("INFO: Error opening config file. Is it a valid file?")
+ return nil
+ }
+
+ fi, err := file.Stat()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ data := make([]byte, int64(fi.Size()))
+ file.Read(data)
+
+ var json_data interface{}
+ json.Unmarshal(data, &json_data)
+
+ return json_data
+}
+
+func GetVarFromEnv(s, key string) string {
+ if s == "" {
+ s = os.Getenv(key)
+ }
+ if s == "" {
+ s = os.Getenv(strings.ToUpper(key))
+ }
+ return s
+}
diff --git a/text.go b/text.go
new file mode 100644
index 0000000..a28c83d
--- /dev/null
+++ b/text.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+ "log"
+ "os"
+ "text/template"
+)
+
+func ParseText(templates []string, data interface{}) {
+ // Parse the template dir
+ tmpl := template.Must(template.New("template").ParseGlob(*template_dir_f + "/*" + *ext_f))
+ for _, template := range templates {
+ if template == "" {
+ continue
+ }
+
+ err := tmpl.ExecuteTemplate(os.Stdout, template, data)
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+}