Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Quick code review.

  • Loading branch information...
commit 87e3a8f475093dce7cacb956929d275e83164214 1 parent 930b15e
Wayne E. Seguin wayneeseguin authored

Showing 2 changed files with 76 additions and 70 deletions. Show diff stats Hide diff stats

  1. +26 22 README.md
  2. +50 48 sm-mustache.go
48 README.md
Source Rendered
... ... @@ -1,32 +1,36 @@
1   - NAME
  1 + NAME
2 2
3   - sm-mustache - Mustache template command line
  3 + sm-mustache - Mustache template command line
4 4
5   - SYNOPSIS
  5 + SYNOPSIS
6 6
7   - Usage of ./sm-mustache:
8   - -data="{key1=value1}:~{key2=value2}": key=value data pair, overrides json file data
9   - -json="{string}": json data string
10   - -output="{file}": path to output file
11   - -template="{file}": path to template file
  7 + sm-mustache [options]
12 8
13   - -data|--data "{key1=value1:~key2=value2}
  9 + OPTIONS
14 10
15   - Assign a value to a given key for the template rendering
16   -
17   - -json|--json "{string}"
18   -
19   - To be implemented
  11 + -data="{key1=value1}:~{key2=value2}": key=value data pair, overrides json file data
  12 + -json="{string}": json data string
  13 + -output="{file}": path to output file
  14 + -template="{file}": path to template file
20 15
21   - -output|--output "{file}"
  16 + -data|--data "{key1=value1:~key2=value2}
22 17
23   - specify the output file to write the rendered template to
  18 + Assign a value to a given key for the template rendering
24 19
25   - -template|--template "{file}"
  20 + -json|--json "{string}"
26 21
27   - specify the template file to use
28   -
29   - DESCRIPTION
  22 + To be implemented
  23 +
  24 + -output|--output "{file}"
  25 +
  26 + specify the output file to write the rendered template to
  27 +
  28 + -template|--template "{file}"
  29 +
  30 + specify the template file to use
  31 +
  32 + DESCRIPTION
  33 +
  34 + sm-mustache will read the given template file, and using the given data render
  35 + it to the specified output file location.
30 36
31   - sm-mustache will read the given template file, and using the given data render
32   - it to the specified output file location.
98 sm-mustache.go
@@ -6,7 +6,10 @@
6 6
7 7 SYNOPSIS
8 8
9   - Usage of ./sm-mustache:
  9 + sm-mustache [options]
  10 +
  11 + OPTIONS
  12 +
10 13 -data="{key1=value1}:~{key2=value2}": key=value data pair, overrides json file data
11 14 -json="{string}": json data string
12 15 -output="{file}": path to output file
@@ -15,9 +18,9 @@
15 18 -data|--data "{key1=value1:~key2=value2}
16 19
17 20 Assign a value to a given key for the template rendering
18   -
  21 +
19 22 -json|--json "{string}"
20   -
  23 +
21 24 To be implemented
22 25
23 26 -output|--output "{file}"
@@ -27,65 +30,64 @@
27 30 -template|--template "{file}"
28 31
29 32 specify the template file to use
30   -
  33 +
31 34 DESCRIPTION
32 35
33   - sm-mustache will read the given template file, and using the given data render
  36 + sm-mustache will read the given template file, and using the given data render
34 37 it to the specified output file location.
35   -
36 38
37 39 */
38 40
39 41 package main
40 42
41 43 import (
42   - "github.com/hoisie/mustache"
43   - "flag"
44   - "strings"
45   - "log"
46   - "os"
  44 + "flag"
  45 + "github.com/hoisie/mustache"
  46 + "log"
  47 + "os"
  48 + "strings"
47 49 )
48 50
49   -var json = flag.String("json","{string}","json data string")
50   -var template = flag.String("template","{file}","path to template file")
51   -var output = flag.String("output","{file}","path to output file")
52   -var data = flag.String("data","{key1=value1}:~{key2=value2}","key=value data pair, overrides json file data")
  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")
53 55
54 56 var mapped_data map[string]string
55 57
56 58 func main() {
57 59
58   - mapped_data = make(map[string]string)
59   -
60   - flag.Parse()
61   -
62   - //Error checking on arguements passed in
63   - if (*template == "{file}") {
64   - log.Fatal("ERROR: A template file must be given!")
65   - } else {
66   - file, err := os.Open(*template)
67   - if file == nil && err != nil {
68   - log.Fatalf("ERROR: The template file %s does not exist!",*template)
69   - }
70   - }
71   -
72   - //Split up the data set passed in
73   - each_set := strings.Split(*data,":~")
74   -
75   - for i := range each_set {
76   - split_data := strings.Split(each_set[i],"=")
77   - if (len(split_data) > 1) {
78   - mapped_data[split_data[0]] = split_data[1]
79   - }
80   - }
81   -
82   - file_data := mustache.RenderFile(*template,mapped_data)
83   -
84   - if(*output != "{file}") {
85   - out_file,err := os.Create(*output)
86   - if err != nil {
87   - log.Fatal("ERROR: Output file was not created")
88   - }
89   - out_file.WriteString(file_data)
90   - }
  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 + }
91 93 }

0 comments on commit 87e3a8f

Please sign in to comment.
Something went wrong with that request. Please try again.