Skip to content

Commit 824204f

Browse files
committed
prep for version update, added yaml2json, toml2json, json2yaml, json2toml
1 parent 2d15aa9 commit 824204f

File tree

27 files changed

+1262
-14
lines changed

27 files changed

+1262
-14
lines changed

Makefile

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ build$(EXT): bin/csvcols$(EXT) bin/csvrows$(EXT) bin/csvfind$(EXT) bin/csvjoin$(
2020
bin/csv2mdtable$(EXT) bin/csv2xlsx$(EXT) bin/csv2json$(EXT) bin/jsonjoin$(EXT) \
2121
bin/jsonmunge$(EXT) bin/findfile$(EXT) bin/finddir$(EXT) bin/mergepath$(EXT) \
2222
bin/reldate$(EXT) bin/range$(EXT) bin/timefmt$(EXT) bin/urlparse$(EXT) \
23-
bin/csvcleaner$(EXT) bin/string$(EXT)
23+
bin/csvcleaner$(EXT) bin/string$(EXT) \
24+
bin/toml2json$(EXT) bin/json2toml$(EXT) \
25+
bin/yaml2json$(EXT) bin/json2yaml$(EXT)
2426

2527

2628
bin/csvcols$(EXT): datatools.go cmd/csvcols/csvcols.go
@@ -89,6 +91,18 @@ bin/csvcleaner$(EXT): datatools.go cmd/csvcleaner/csvcleaner.go
8991
bin/string$(EXT): datatools.go cmd/string/string.go
9092
go build -o bin/string$(EXT) cmd/string/string.go
9193

94+
bin/toml2json$(EXT): datatools.go cmd/toml2json/main.go
95+
go build -o bin/toml2json$(EXT) cmd/toml2json/main.go
96+
97+
bin/json2toml$(EXT): datatools.go cmd/json2toml/main.go
98+
go build -o bin/json2toml$(EXT) cmd/json2toml/main.go
99+
100+
bin/yaml2json$(EXT): datatools.go cmd/yaml2json/main.go
101+
go build -o bin/yaml2json$(EXT) cmd/yaml2json/main.go
102+
103+
bin/json2yaml$(EXT): datatools.go cmd/json2yaml/main.go
104+
go build -o bin/json2yaml$(EXT) cmd/json2yaml/main.go
105+
92106
test: build
93107
go test
94108
bash test_cmd.bash
@@ -142,6 +156,10 @@ man: build
142156
bin/xlsx2csv -generate-manpage | nroff -Tutf8 -man > man/man1/xlsx2csv.1
143157
bin/csvcleaner -generate-manpage | nroff -Tutf8 -man > man/man1/csvcleaner.1
144158
bin/string -generate-manpage | nroff -Tutf8 -man > man/man1/string.1
159+
bin/toml2json -generate-manpage | nroff -Tutf8 -man > man/man1/toml2json.1
160+
bin/json2toml -generate-manpage | nroff -Tutf8 -man > man/man1/json2toml.1
161+
bin/yaml2json -generate-manpage | nroff -Tutf8 -man > man/man1/yaml2json.1
162+
bin/json2yaml -generate-manpage | nroff -Tutf8 -man > man/man1/json2yaml.1
145163

146164
install:
147165
env GOBIN=$(GOPATH)/bin go install cmd/csvcols/csvcols.go
@@ -166,6 +184,10 @@ install:
166184
env GOBIN=$(GOPATH)/bin go install cmd/xlsx2csv/xlsx2csv.go
167185
env GOBIN=$(GOPATH)/bin go install cmd/csvcleaner/csvcleaner.go
168186
env GOBIN=$(GOPATH)/bin go install cmd/string/string.go
187+
env GOBIN=$(GOPATH)/bin go install cmd/toml2json/toml2json.go
188+
env GOBIN=$(GOPATH)/bin go install cmd/json2toml/json2toml.go
189+
env GOBIN=$(GOPATH)/bin go install cmd/yaml2json/yaml2json.go
190+
env GOBIN=$(GOPATH)/bin go install cmd/json2yaml/json2yaml.go
169191

170192
dist/linux-amd64:
171193
mkdir -p dist/bin
@@ -191,6 +213,10 @@ dist/linux-amd64:
191213
env GOOS=linux GOARCH=amd64 go build -o dist/bin/urlparse cmd/urlparse/urlparse.go
192214
env GOOS=linux GOARCH=amd64 go build -o dist/bin/csvcleaner cmd/csvcleaner/csvcleaner.go
193215
env GOOS=linux GOARCH=amd64 go build -o dist/bin/string cmd/string/string.go
216+
env GOOS=linux GOARCH=amd64 go build -o dist/bin/toml2json cmd/toml2json/toml2json.go
217+
env GOOS=linux GOARCH=amd64 go build -o dist/bin/json2toml cmd/json2toml/json2toml.go
218+
env GOOS=linux GOARCH=amd64 go build -o dist/bin/yaml2json cmd/yaml2json/yaml2json.go
219+
env GOOS=linux GOARCH=amd64 go build -o dist/bin/json2yaml cmd/json2yaml/json2yaml.go
194220
cd dist && zip -r $(PROJECT)-$(VERSION)-linux-amd64.zip README.md LICENSE INSTALL.md bin/* docs/* how-to/* demos/*
195221
rm -fR dist/bin
196222

@@ -219,6 +245,10 @@ dist/macosx-amd64:
219245
env GOOS=darwin GOARCH=amd64 go build -o dist/bin/urlparse cmd/urlparse/urlparse.go
220246
env GOOS=darwin GOARCH=amd64 go build -o dist/bin/csvcleaner cmd/csvcleaner/csvcleaner.go
221247
env GOOS=darwin GOARCH=amd64 go build -o dist/bin/string cmd/string/string.go
248+
env GOOS=darwin GOARCH=amd64 go build -o dist/bin/toml2json cmd/toml2json/toml2json.go
249+
env GOOS=darwin GOARCH=amd64 go build -o dist/bin/json2toml cmd/json2toml/json2toml.go
250+
env GOOS=darwin GOARCH=amd64 go build -o dist/bin/yaml2json cmd/yaml2json/yaml2json.go
251+
env GOOS=darwin GOARCH=amd64 go build -o dist/bin/json2yaml cmd/json2yaml/json2yaml.go
222252
cd dist && zip -r $(PROJECT)-$(VERSION)-macosx-amd64.zip README.md LICENSE INSTALL.md bin/* docs/* how-to/* demos/*
223253
rm -fR dist/bin
224254

@@ -248,6 +278,10 @@ dist/windows-amd64:
248278
env GOOS=windows GOARCH=amd64 go build -o dist/bin/urlparse.exe cmd/urlparse/urlparse.go
249279
env GOOS=windows GOARCH=amd64 go build -o dist/bin/csvcleaner.exe cmd/csvcleaner/csvcleaner.go
250280
env GOOS=windows GOARCH=amd64 go build -o dist/bin/string.exe cmd/string/string.go
281+
env GOOS=windows GOARCH=amd64 go build -o dist/bin/toml2json.exe cmd/toml2json/toml2json.go
282+
env GOOS=windows GOARCH=amd64 go build -o dist/bin/json2toml.exe cmd/json2toml/json2toml.go
283+
env GOOS=windows GOARCH=amd64 go build -o dist/bin/yaml2json.exe cmd/yaml2json/yaml2json.go
284+
env GOOS=windows GOARCH=amd64 go build -o dist/bin/json2yaml.exe cmd/json2yaml/json2yaml.go
251285
cd dist && zip -r $(PROJECT)-$(VERSION)-windows-amd64.zip README.md LICENSE INSTALL.md bin/* docs/* how-to/* demos/*
252286
rm -fR dist/bin
253287

@@ -278,6 +312,10 @@ dist/raspbian-arm7:
278312
env GOOS=linux GOARCH=arm GOARM=7 go build -o dist/bin/urlparse cmd/urlparse/urlparse.go
279313
env GOOS=linux GOARCH=arm GOARM=7 go build -o dist/bin/csvcleaner cmd/csvcleaner/csvcleaner.go
280314
env GOOS=linux GOARCH=arm GOARM=7 go build -o dist/bin/string cmd/string/string.go
315+
env GOOS=linux GOARCH=arm GOARM=7 go build -o dist/bin/toml2json cmd/toml2json/toml2json.go
316+
env GOOS=linux GOARCH=arm GOARM=7 go build -o dist/bin/json2toml cmd/json2toml/json2toml.go
317+
env GOOS=linux GOARCH=arm GOARM=7 go build -o dist/bin/yaml2json cmd/yaml2json/yaml2json.go
318+
env GOOS=linux GOARCH=arm GOARM=7 go build -o dist/bin/json2yaml cmd/json2yaml/json2yaml.go
281319
cd dist && zip -r $(PROJECT)-$(VERSION)-raspbian-arm7.zip README.md LICENSE INSTALL.md bin/* docs/* how-to/* demos/*
282320
rm -fR dist/bin
283321

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Posix shell scripting (e.g. writing scripts that run under Bash). The tools are
66

77
## For data
88

9-
Command line utilities for simplifying work with CSV, JSON, Excel Workbooks and plain text files or content.
9+
Command line utilities for simplifying work with CSV, JSON, TOML, YAML, Excel Workbooks and plain text files or content.
1010

1111
+ [csv2json](docs/csv2json/) - a tool to take a CSV file and convert it into a JSON array or a list of JSON blobs one per line
1212
+ [csv2mdtable](docs/csv2mdtable/) - a tool to render CSV as a Github Flavored Markdown table
@@ -16,12 +16,16 @@ Command line utilities for simplifying work with CSV, JSON, Excel Workbooks and
1616
+ [csvfind](docs/csvfind/) - a tool for filtering a CSV file rows by column
1717
+ [csvjoin](docs/csvjoin/) - a tool to join two CSV files on common values in designated columns, writes combined CSV rows
1818
+ [csvrows](docs/csvrows/) - a tool for formatting command line arguments into CSV columns of rows or filtering CSV for specific rows
19+
+ [json2toml](docs/json2toml/) - a tool for converting JSON to TOML
20+
+ [json2yaml](docs/json2yaml/) - a tool for converting JSON to YAML
1921
+ [jsoncols](docs/jsoncols/) - a tool for exploring and extracting JSON values into columns
2022
+ [jsonjoin](docs/jsonjoin/) - a tool for joining JSON object documents
2123
+ [jsonmunge](docs/jsonmunge/) - a tool to transform JSON documents into something else
2224
+ [jsonrange](docs/jsonrange/) - a tool for iterating over JSON objects and arrays (return keys or values)
25+
+ [toml2json](docs/toml2json/) - a tool for converting TOML to JSON
2326
+ [xlsx2csv](docs/xlsx2csv/) - a tool for converting Excel Workbooks sheets to CSV files
2427
+ [xlsx2json](docs/xlsx2json/) - a tool for converting Excel Workbooks to JSON files
28+
+ [yaml2json](docs/yaml2json/) - a tool for converting YAML files to JSON
2529

2630

2731
Compiled versions are provided for Linux (amd64), Mac OS X (amd64),
@@ -76,7 +80,7 @@ Use the utilities try "-help" option for a full list of options.
7680

7781
See [INSTALL.md](install/) for details for installing pre-compiled versions of the programs.
7882

79-
_datatools_ are go get-able. If you have go v1.8 (or newer) you can install with the command below.
83+
_datatools_ are go get-able. If you have go v1.12 (or newer) you can install with the command below.
8084

8185
```
8286
go get github.com/caltechlibrary/datatools/...

cmd/json2toml/main.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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

Comments
 (0)