diff --git a/README.md b/README.md index 37f7b3e..fe9202f 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,33 @@ $ yaml json write test.yaml } ``` +### toml + +You can read and write TOML, too. + +To convert from TOML to YAML: + +```tty +$ yaml toml read test.toml +foo: + bar: 7 +somename: + partone: + one: 7 + two: + - some string + - hello world +``` + +To convert from YAML to TOML: + +```tty +$ yaml toml write test-toml.yaml +[foo] +bar = 7 +baz = [ "some string", "hello world" ] +``` + You can get more help by just typing `yaml`. ```bash @@ -111,8 +138,10 @@ Usage: yaml [] Some useful yaml commands are: commands List all yaml commands get Get a value from a YAML file + json Import/export YAML to/from JSON set Set a value in a YAML file template Instantiate a template file with a YAML file. + toml Import/export YAML to/from TOML See 'yaml help ' for information on a specific command. ``` diff --git a/lib/errors.js b/lib/errors.js index 861edc3..2f5a8b4 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { module.exports = { readingPath: function(error, path) { @@ -24,6 +24,14 @@ formattingJSON: function(error) { console.error("yaml: unable to format result as JSON\n" + error.message); return process.exit(-1); + }, + parsingTOML: function(error) { + console.error("yaml: error parsing TOML\n" + error.message); + return process.exit(-1); + }, + formattingTOML: function(error) { + console.error("yaml: unable to format result as TOML\n" + error.message); + return process.exit(-1); } }; diff --git a/lib/get.js b/lib/get.js index 390b6b4..678d619 100644 --- a/lib/get.js +++ b/lib/get.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { var YAML, call, isArray, isDefined, isObject, path, ref, ref1, reference, slice = [].slice; diff --git a/lib/json.js b/lib/json.js index 1cc8b6d..6e63c40 100644 --- a/lib/json.js +++ b/lib/json.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { var YAML, _JSON, async, call, errors, isArray, isObject, path, read, ref, ref1, subcommand; @@ -12,7 +12,7 @@ _JSON = { read: async(function*(_path) { - var data, error, error1, error2, json; + var data, error, json; path = _path === "-" ? process.stdin : _path; try { json = (yield read(path)); @@ -22,8 +22,8 @@ } try { data = JSON.parse(json); - } catch (error2) { - error = error2; + } catch (error1) { + error = error1; errors.parsingJSON(error); } return data; @@ -31,7 +31,6 @@ write: function(data) { var error, result; result = (function() { - var error1; if (data != null) { if ((isObject(data)) || (isArray(data))) { try { diff --git a/lib/set.js b/lib/set.js index d746c39..fdae5d1 100644 --- a/lib/set.js +++ b/lib/set.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { var YAML, call, path, ref, reference, value, slice = [].slice; diff --git a/lib/template.js b/lib/template.js index 2548007..70ee9cb 100644 --- a/lib/template.js +++ b/lib/template.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { var YAML, call, get, path, read, ref, ref1, ref2, set, template; diff --git a/lib/toml.js b/lib/toml.js new file mode 100644 index 0000000..32dbf5f --- /dev/null +++ b/lib/toml.js @@ -0,0 +1,65 @@ +// Generated by CoffeeScript 1.12.7 +(function() { + var TOML, YAML, _TOML, async, call, errors, path, read, ref, ref1, subcommand; + + ref = require("fairmont"), async = ref.async, call = ref.call, read = ref.read; + + TOML = require("@iarna/toml"); + + YAML = require("./yaml"); + + errors = require("./errors"); + + ref1 = process.argv.slice(2), subcommand = ref1[0], path = ref1[1]; + + _TOML = { + read: async(function*(_path) { + var data, error, toml; + path = _path === "-" ? process.stdin : _path; + try { + toml = (yield read(path)); + } catch (error1) { + error = error1; + errors.readingPath(error, path); + } + try { + data = TOML.parse(toml); + } catch (error1) { + error = error1; + errors.parsingTOML(error); + } + return data; + }), + write: function(data) { + var error, result; + result = (function() { + if (data != null) { + try { + return TOML.stringify(data); + } catch (error1) { + error = error1; + return errors.formattingTOML(error); + } + } else { + return ""; + } + })(); + return console.log(result); + } + }; + + if ((subcommand != null) && (subcommand === "read" || subcommand === "write") && (path != null)) { + call(function*() { + if (subcommand === "write") { + return _TOML.write((yield YAML.read(path))); + } else { + return YAML.write((yield _TOML.read(path))); + } + }); + } else { + console.error("yaml toml: invalid arguments"); + console.error("yaml toml [read|write] "); + process.exit(-1); + } + +}).call(this); diff --git a/lib/yaml.js b/lib/yaml.js index cb6ed81..da953c5 100644 --- a/lib/yaml.js +++ b/lib/yaml.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { var YAML, async, errors, isArray, isObject, read, ref; @@ -10,7 +10,7 @@ module.exports = { read: async(function*(_path) { - var data, error, error1, error2, path, yaml; + var data, error, path, yaml; path = _path === "-" ? process.stdin : _path; try { yaml = (yield read(path)); @@ -20,8 +20,8 @@ } try { data = YAML.safeLoad(yaml); - } catch (error2) { - error = error2; + } catch (error1) { + error = error1; errors.parsingYAML(error); } return data; @@ -29,7 +29,6 @@ write: function(data) { var error, result; result = (function() { - var error1; if (data != null) { if (isObject(data)) { try { diff --git a/libexec/yaml-toml b/libexec/yaml-toml new file mode 100755 index 0000000..96a340c --- /dev/null +++ b/libexec/yaml-toml @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# Usage: yaml toml [read|write] +# Summary: Import/export YAML to/from TOML +# Help: Read a TOML file and dump YAML. +# +# yaml toml read test.json +# +# Read a YAML file and dump TOML +# +# yaml toml write test.yaml +# +exec node $_YAML_ROOT/lib/toml.js $@ diff --git a/package.json b/package.json index e747b10..bea3817 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "homepage": "https://github.com/pandastrike/yaml-cli#readme", "dependencies": { + "@iarna/toml": "^2.2.1", "fairmont": "^1.0.0-beta-40", "js-yaml": "^3.5.3", "markup-js": "^1.5.21" diff --git a/src/errors.coffee b/src/errors.coffee index ed0b474..1498aa8 100644 --- a/src/errors.coffee +++ b/src/errors.coffee @@ -22,3 +22,11 @@ module.exports = formattingJSON: (error) -> console.error "yaml: unable to format result as JSON\n#{error.message}" process.exit -1 + + parsingTOML: (error) -> + console.error "yaml: error parsing TOML\n#{error.message}" + process.exit -1 + + formattingTOML: (error) -> + console.error "yaml: unable to format result as TOML\n#{error.message}" + process.exit -1 diff --git a/src/toml.coffee b/src/toml.coffee new file mode 100644 index 0000000..bef1b40 --- /dev/null +++ b/src/toml.coffee @@ -0,0 +1,49 @@ +{async, call, read} = require "fairmont" +TOML = require "@iarna/toml" +YAML = require "./yaml" +errors = require "./errors" + +[subcommand, path] = process.argv[2..] + +_TOML = + + read: async (_path) -> + + path = if _path == "-" then process.stdin else _path + + try + toml = yield read path + catch error + errors.readingPath error, path + + try + data = TOML.parse toml + catch error + errors.parsingTOML error + + data + + write: (data) -> + + result = + if data? + try + TOML.stringify data + catch error + errors.formattingTOML error + else + "" + + console.log result + +if subcommand? && subcommand in ["read", "write"] && path? + call -> + if subcommand == "write" + _TOML.write yield YAML.read path + else + YAML.write yield _TOML.read path + +else + console.error "yaml toml: invalid arguments" + console.error "yaml toml [read|write] " + process.exit -1 diff --git a/test/test-toml.yaml b/test/test-toml.yaml new file mode 100644 index 0000000..ad1dfa9 --- /dev/null +++ b/test/test-toml.yaml @@ -0,0 +1,5 @@ +foo: + bar: 7 + baz: + - "some string" # TOML does not allow mixed data types in an array + - "hello world" diff --git a/test/test.coffee b/test/test.coffee index f25aabb..d6a85f4 100755 --- a/test/test.coffee +++ b/test/test.coffee @@ -84,3 +84,19 @@ Amen.describe "YAML CLI", (context) -> data = JSON.parse yield sh "bin/yaml json write test/test.yaml" assert.equal "7", data.foo.bar assert.equal "hello world", data.foo.baz[1] + + context.test "toml", (context) -> + + context.test "read", -> + YAML = require "js-yaml" + data = YAML.safeLoad yield sh "bin/yaml toml read test/test.toml" + assert.equal "7", data.foo.bar + assert.equal "7", data.somename.partone.one + assert.equal "hello world", data.somename.partone.two[1] + + + context.test "write", -> + TOML = require "@iarna/toml" + data = TOML.parse yield sh "bin/yaml toml write test/test-toml.yaml" + assert.equal "7", data.foo.bar + assert.equal "hello world", data.foo.baz[1] diff --git a/test/test.toml b/test/test.toml new file mode 100644 index 0000000..03f88cd --- /dev/null +++ b/test/test.toml @@ -0,0 +1,6 @@ +[foo] +bar = 7 + +[somename.partone] +one = 7 +two = ["some string", "hello world"]