|
| 1 | +// |
| 2 | +// jsonmunge is a command line tool that takes a JSON document and |
| 3 | +// a Go text/template rendering the result. Useful for |
| 4 | +// reshaping a JSON document or transforming into a new format, |
| 5 | +// or filter for specific content. |
| 6 | +// |
| 7 | +// @author R. S. Doiel, <[email protected]> |
| 8 | +// |
| 9 | +package main |
| 10 | + |
| 11 | +import ( |
| 12 | + "flag" |
| 13 | + "fmt" |
| 14 | + "io/ioutil" |
| 15 | + "os" |
| 16 | + "path" |
| 17 | + "text/template" |
| 18 | + |
| 19 | + // Caltech Library Packages |
| 20 | + "github.com/caltechlibrary/cli" |
| 21 | + "github.com/caltechlibrary/datatools" |
| 22 | + "github.com/caltechlibrary/dotpath" |
| 23 | + "github.com/caltechlibrary/tmplfn" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + usage = `USAGE: %s [OPTIONS] TEMPLATE_FILENAME` |
| 28 | + |
| 29 | + description = ` |
| 30 | +SYSNOPSIS |
| 31 | +
|
| 32 | +%s is a command line tool that takes a JSON document and |
| 33 | +one or more Go templates rendering the results. Useful for |
| 34 | +reshaping a JSON document, transforming into a new format, |
| 35 | +or filter for specific content. |
| 36 | +
|
| 37 | ++ TEMPLATE_FILENAME is the name of a Go text tempate file used to render |
| 38 | + the outbound JSON document |
| 39 | +` |
| 40 | + |
| 41 | + examples = ` |
| 42 | +EXAMPLES |
| 43 | +
|
| 44 | +If data.json contained |
| 45 | +
|
| 46 | + {"name": "Doe, Jane", "email":"[email protected]", "age": 42} |
| 47 | +
|
| 48 | +and the template, name.tmpl, contained |
| 49 | +
|
| 50 | + {{- .name -}} |
| 51 | +
|
| 52 | +Getting just the name could be done with |
| 53 | +
|
| 54 | + cat data.json | %s name.tmpl |
| 55 | +
|
| 56 | +This would yeild |
| 57 | +
|
| 58 | + "Doe, Jane" |
| 59 | +` |
| 60 | + |
| 61 | + // Basic Options |
| 62 | + showHelp bool |
| 63 | + showLicense bool |
| 64 | + showVersion bool |
| 65 | + inputFName string |
| 66 | + outputFName string |
| 67 | + |
| 68 | + // Application Specific Options |
| 69 | +) |
| 70 | + |
| 71 | +func init() { |
| 72 | + // Basic Options |
| 73 | + flag.BoolVar(&showHelp, "h", false, "display help") |
| 74 | + flag.BoolVar(&showLicense, "l", false, "display license") |
| 75 | + flag.BoolVar(&showVersion, "v", false, "display version") |
| 76 | + flag.StringVar(&inputFName, "i", "", "input filename") |
| 77 | + flag.StringVar(&inputFName, "input", "", "input filename") |
| 78 | + flag.StringVar(&outputFName, "o", "", "output filename") |
| 79 | + flag.StringVar(&outputFName, "output", "", "output filename") |
| 80 | + |
| 81 | + // Application Specific Options |
| 82 | +} |
| 83 | + |
| 84 | +func main() { |
| 85 | + appName := path.Base(os.Args[0]) |
| 86 | + flag.Parse() |
| 87 | + args := flag.Args() |
| 88 | + |
| 89 | + // Configuration and command line interation |
| 90 | + cfg := cli.New(appName, "DATATOOLS", fmt.Sprintf(datatools.LicenseText, appName, datatools.Version), datatools.Version) |
| 91 | + cfg.UsageText = fmt.Sprintf(usage, appName) |
| 92 | + cfg.DescriptionText = fmt.Sprintf(description, appName) |
| 93 | + cfg.ExampleText = fmt.Sprintf(examples, appName) |
| 94 | + |
| 95 | + if showHelp == true { |
| 96 | + fmt.Println(cfg.Usage()) |
| 97 | + os.Exit(0) |
| 98 | + } |
| 99 | + |
| 100 | + if showLicense == true { |
| 101 | + fmt.Println(cfg.License()) |
| 102 | + os.Exit(0) |
| 103 | + } |
| 104 | + |
| 105 | + if showVersion == true { |
| 106 | + fmt.Println(cfg.Version()) |
| 107 | + os.Exit(0) |
| 108 | + } |
| 109 | + |
| 110 | + if len(args) == 0 { |
| 111 | + fmt.Fprintf(os.Stderr, "Need to provide at least one template name\n") |
| 112 | + os.Exit(1) |
| 113 | + } |
| 114 | + // Read in and compile our templates |
| 115 | + tmpl, err := template.New(path.Base(args[0])).Funcs(tmplfn.AllFuncs()).ParseFiles(args...) |
| 116 | + if err != nil { |
| 117 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 118 | + os.Exit(1) |
| 119 | + } |
| 120 | + |
| 121 | + in, err := cli.Open(inputFName, os.Stdin) |
| 122 | + if err != nil { |
| 123 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 124 | + os.Exit(1) |
| 125 | + } |
| 126 | + defer cli.CloseFile(inputFName, in) |
| 127 | + |
| 128 | + out, err := cli.Create(outputFName, os.Stdout) |
| 129 | + if err != nil { |
| 130 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 131 | + os.Exit(1) |
| 132 | + } |
| 133 | + defer cli.CloseFile(outputFName, out) |
| 134 | + |
| 135 | + // READ in the JSON document |
| 136 | + buf, err := ioutil.ReadAll(in) |
| 137 | + if err != nil { |
| 138 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 139 | + os.Exit(1) |
| 140 | + } |
| 141 | + // JSON Decode our document |
| 142 | + data, err := dotpath.JSONDecode(buf) |
| 143 | + if err != nil { |
| 144 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 145 | + os.Exit(1) |
| 146 | + } |
| 147 | + |
| 148 | + // Execute template with data |
| 149 | + if err := tmpl.Execute(out, data); err != nil { |
| 150 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 151 | + os.Exit(1) |
| 152 | + } |
| 153 | +} |
0 commit comments