|
| 1 | +// |
| 2 | +// json2toml is a command line utility that converts JSON objects to TOML. |
| 3 | +// |
| 4 | +// @Author R. S. Doiel |
| 5 | +// |
| 6 | +// Copyright (c) 2019, Caltech |
| 7 | +// All rights not granted herein are expressly reserved by Caltech. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 10 | +// |
| 11 | +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 12 | +// |
| 13 | +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 14 | +// |
| 15 | +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | +// |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "io" |
| 25 | + "os" |
| 26 | + |
| 27 | + // CaltechLibrary packages |
| 28 | + "github.com/caltechlibrary/cli" |
| 29 | + "github.com/caltechlibrary/datatools" |
| 30 | + |
| 31 | + // 3rd Party packages |
| 32 | + "github.com/BurntSushi/toml" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + description = ` |
| 37 | +%s is a tool that converts JSON objects into TOML output. |
| 38 | +` |
| 39 | + |
| 40 | + examples = ` |
| 41 | +These would get the file named "my.json" and save it as my.toml |
| 42 | +
|
| 43 | + %s my.json > my.toml |
| 44 | +
|
| 45 | + %s my.json my.toml |
| 46 | +
|
| 47 | + cat my.json | %s -i - > my.toml |
| 48 | +
|
| 49 | +` |
| 50 | + |
| 51 | + // Standard Options |
| 52 | + showHelp bool |
| 53 | + showLicense bool |
| 54 | + showVersion bool |
| 55 | + showExamples bool |
| 56 | + inputFName string |
| 57 | + outputFName string |
| 58 | + generateMarkdown bool |
| 59 | + generateManPage bool |
| 60 | + quiet bool |
| 61 | + newLine bool |
| 62 | + eol string |
| 63 | + |
| 64 | + // Application Options |
| 65 | + prettyPrint bool |
| 66 | +) |
| 67 | + |
| 68 | +func json2TOML(in io.Reader, out io.Writer, printPrint bool) error { |
| 69 | + var ( |
| 70 | + err error |
| 71 | + ) |
| 72 | + m := map[string]interface{}{} |
| 73 | + jsonDecoder := json.NewDecoder(in) |
| 74 | + jsonDecoder.UseNumber() |
| 75 | + err = jsonDecoder.Decode(&m) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + tomlEncoder := toml.NewEncoder(out) |
| 80 | + if prettyPrint == true { |
| 81 | + tomlEncoder.Indent = " " |
| 82 | + } else { |
| 83 | + tomlEncoder.Indent = "" |
| 84 | + } |
| 85 | + if err = tomlEncoder.Encode(m); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func main() { |
| 92 | + app := cli.NewCli(datatools.Version) |
| 93 | + appName := app.AppName() |
| 94 | + |
| 95 | + // Add Help Docs |
| 96 | + app.AddHelp("license", []byte(fmt.Sprintf(datatools.LicenseText, appName, datatools.Version))) |
| 97 | + app.AddHelp("description", []byte(fmt.Sprintf(description, appName))) |
| 98 | + app.AddHelp("examples", []byte(fmt.Sprintf(examples, appName))) |
| 99 | + |
| 100 | + // Document non-option parameters |
| 101 | + app.SetParams("[JSON_FILENAME]", "[TOML_FILENAME]") |
| 102 | + |
| 103 | + // Standard Options |
| 104 | + app.BoolVar(&showHelp, "h,help", false, "display help") |
| 105 | + app.BoolVar(&showLicense, "l,license", false, "display license") |
| 106 | + app.BoolVar(&showVersion, "v,version", false, "display version") |
| 107 | + app.BoolVar(&showExamples, "examples", false, "display example(s)") |
| 108 | + //app.StringVar(&inputFName, "i,input", "", "input filename") |
| 109 | + app.StringVar(&outputFName, "o,output", "", "output filename") |
| 110 | + app.BoolVar(&quiet, "quiet", false, "suppress error messages") |
| 111 | + app.BoolVar(&generateMarkdown, "generate-markdown", false, "generate markdown documentation") |
| 112 | + app.BoolVar(&generateManPage, "generate-manpage", false, "generate man page") |
| 113 | + app.BoolVar(&newLine, "nl,newline", false, "if true add a trailing newline") |
| 114 | + |
| 115 | + // App Specific Options |
| 116 | + app.BoolVar(&prettyPrint, "p,pretty", false, "pretty print output") |
| 117 | + |
| 118 | + // Parse env and options |
| 119 | + app.Parse() |
| 120 | + args := app.Args() |
| 121 | + |
| 122 | + // Handle case of input/output filenames provided without -i, -o |
| 123 | + if len(args) > 0 { |
| 124 | + inputFName = args[0] |
| 125 | + if len(args) > 1 { |
| 126 | + outputFName = args[1] |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Setup IO |
| 131 | + var err error |
| 132 | + app.Eout = os.Stderr |
| 133 | + |
| 134 | + app.In, err = cli.Open(inputFName, os.Stdin) |
| 135 | + cli.ExitOnError(app.Eout, err, quiet) |
| 136 | + defer cli.CloseFile(inputFName, app.In) |
| 137 | + |
| 138 | + app.Out, err = cli.Create(outputFName, os.Stdout) |
| 139 | + cli.ExitOnError(app.Eout, err, quiet) |
| 140 | + defer cli.CloseFile(outputFName, app.Out) |
| 141 | + |
| 142 | + // Process options |
| 143 | + if generateMarkdown { |
| 144 | + app.GenerateMarkdown(app.Out) |
| 145 | + os.Exit(0) |
| 146 | + } |
| 147 | + if generateManPage { |
| 148 | + app.GenerateManPage(app.Out) |
| 149 | + os.Exit(0) |
| 150 | + } |
| 151 | + if showHelp || showExamples { |
| 152 | + if len(args) > 0 { |
| 153 | + fmt.Fprintln(app.Out, app.Help(args...)) |
| 154 | + } else { |
| 155 | + app.Usage(app.Out) |
| 156 | + } |
| 157 | + os.Exit(0) |
| 158 | + } |
| 159 | + if showLicense { |
| 160 | + fmt.Fprintln(app.Out, app.License()) |
| 161 | + os.Exit(0) |
| 162 | + } |
| 163 | + if showVersion { |
| 164 | + fmt.Fprintln(app.Out, app.Version()) |
| 165 | + os.Exit(0) |
| 166 | + } |
| 167 | + if newLine { |
| 168 | + eol = "\n" |
| 169 | + } |
| 170 | + |
| 171 | + err = json2TOML(app.In, app.Out, prettyPrint) |
| 172 | + cli.ExitOnError(app.Eout, err, quiet) |
| 173 | + fmt.Fprintf(app.Out, "%s", eol) |
| 174 | +} |
0 commit comments