Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 94 lines (64 sloc) 2.129 kB
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
1 /*
2
3 NAME
4
5 sm-mustache - Mustache template command line
6
7 SYNOPSIS
8
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
9 sm-mustache [options]
10
11 OPTIONS
12
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
13 -data="{key1=value1}:~{key2=value2}": key=value data pair, overrides json file data
14 -json="{string}": json data string
15 -output="{file}": path to output file
16 -template="{file}": path to template file
17
18 -data|--data "{key1=value1:~key2=value2}
19
20 Assign a value to a given key for the template rendering
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
21
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
22 -json|--json "{string}"
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
23
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
24 To be implemented
25
26 -output|--output "{file}"
27
28 specify the output file to write the rendered template to
29
30 -template|--template "{file}"
31
32 specify the template file to use
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
33
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
34 DESCRIPTION
35
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
36 sm-mustache will read the given template file, and using the given data render
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
37 it to the specified output file location.
38
39 */
40
41 package main
42
43 import (
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
44 "flag"
45 "github.com/hoisie/mustache"
46 "log"
47 "os"
48 "strings"
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
49 )
50
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
51 var json = flag.String("json", "{string}", "json data string")
52 var template = flag.String("template", "{file}", "path to template file")
53 var output = flag.String("output", "{file}", "path to output file")
54 var data = flag.String("data", "{key1=value1}:~{key2=value2}", "key=value data pair, overrides json file data")
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
55
56 var mapped_data map[string]string
57
58 func main() {
59
87e3a8f @wayneeseguin Quick code review.
wayneeseguin authored
60 mapped_data = make(map[string]string)
61
62 flag.Parse()
63
64 //Error checking on arguements passed in
65 if *template == "{file}" {
66 log.Fatal("ERROR: A template file location must be specified with --template={{path to template file}}")
67 } else {
68 file, err := os.Open(*template)
69 if file == nil && err != nil {
70 log.Fatalf("ERROR: Unable to open template file '%s'", *template)
71 }
72 }
73
74 //Split up the data set passed in
75 each_set := strings.Split(*data, ":~")
76
77 for i := range each_set {
78 split_data := strings.Split(each_set[i], "=")
79 if len(split_data) > 1 {
80 mapped_data[split_data[0]] = split_data[1]
81 }
82 }
83
84 file_data := mustache.RenderFile(*template, mapped_data)
85
86 if *output != "{file}" {
87 out_file, err := os.Create(*output)
88 if err != nil {
89 log.Fatalf("ERROR: Unable to create output file %s", *output)
90 }
91 out_file.WriteString(file_data)
92 }
930b15e @longnguyen11288 Initial commit
longnguyen11288 authored
93 }
Something went wrong with that request. Please try again.