summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnish Athalye <me@anishathalye.com>2016-01-13 11:29:12 -0500
committerAnish Athalye <me@anishathalye.com>2016-01-13 11:29:12 -0500
commitc48d16cbce1dc082bc0b2f8ab3f34e7c0829cf89 (patch)
tree015da0cf2c332143e3c5f6734d1baabbefb23bb4
parent9250bef422703ff9f676da84f6f5c28c61ad3fc4 (diff)
Use standard library JSON parser for JSON files
This patch reverts the changes to the README made in 57265f78b4e4f6ca67393f55b2b08f6f45a02ff1 and makes it so that Dotbot supports JSON files with tab characters.
-rw-r--r--README.md10
-rw-r--r--dotbot/config.py12
-rw-r--r--test/test-lib.bash9
-rw-r--r--test/tests/config-json-tabs.bash20
-rw-r--r--test/tests/config-json.bash20
5 files changed, 63 insertions, 8 deletions
diff --git a/README.md b/README.md
index 7971de9..6dae828 100644
--- a/README.md
+++ b/README.md
@@ -118,13 +118,9 @@ Configuration
-------------
Dotbot uses YAML or JSON formatted configuration files to let you specify how
-to set up your dotfiles. The YAML format is recommended because it looks
-cleaner. JSON will be parsed as a subset of YAML, so tab characters are not
-permitted.
-
-Currently, Dotbot knows how to [link](#link) files and folders, execute
-[shell](#shell) commands, and [clean](#clean) directories of broken symbolic
-links.
+to set up your dotfiles. Currently, Dotbot knows how to [link](#link) files and
+folders, execute [shell](#shell) commands, and [clean](#clean) directories of
+broken symbolic links.
**Ideally, bootstrap configurations should be idempotent. That is, the
installer should be able to be run multiple times without causing any
diff --git a/dotbot/config.py b/dotbot/config.py
index 9ecf9ac..a9aafa0 100644
--- a/dotbot/config.py
+++ b/dotbot/config.py
@@ -1,4 +1,5 @@
import yaml
+import json
from .util import string
class ConfigReader(object):
@@ -8,7 +9,16 @@ class ConfigReader(object):
def _read(self, config_file_path):
try:
with open(config_file_path) as fin:
- data = yaml.safe_load(fin)
+ try:
+ data = yaml.safe_load(fin)
+ except Exception as e:
+ # try falling back to JSON, but return original exception
+ # if that fails too
+ try:
+ fin.seek(0)
+ data = json.load(fin)
+ except Exception:
+ raise e
return data
except Exception as e:
msg = string.indent_lines(str(e))
diff --git a/test/test-lib.bash b/test/test-lib.bash
index 425ae56..bb4b234 100644
--- a/test/test-lib.bash
+++ b/test/test-lib.bash
@@ -1,6 +1,7 @@
DEBUG=false
DOTFILES='/home/vagrant/dotfiles'
INSTALL_CONF='install.conf.yaml'
+INSTALL_CONF_JSON='install.conf.json'
test_run_() {
if ! ${DEBUG}; then
@@ -48,4 +49,12 @@ run_dotbot() {
)
}
+run_dotbot_json() {
+ (
+ cd "${DOTFILES}"
+ cat > "${INSTALL_CONF_JSON}"
+ /dotbot/bin/dotbot -d . -c "${INSTALL_CONF_JSON}" "${@}"
+ )
+}
+
initialize
diff --git a/test/tests/config-json-tabs.bash b/test/tests/config-json-tabs.bash
new file mode 100644
index 0000000..4acc230
--- /dev/null
+++ b/test/tests/config-json-tabs.bash
@@ -0,0 +1,20 @@
+test_description='json config with tabs allowed'
+. '../test-lib.bash'
+
+test_expect_success 'setup' '
+echo "grape" > ${DOTFILES}/h
+'
+
+test_expect_success 'run' '
+run_dotbot_json <<EOF
+[{
+ "link": {
+ "~/.i": "h"
+ }
+}]
+EOF
+'
+
+test_expect_success 'test' '
+grep "grape" ~/.i
+'
diff --git a/test/tests/config-json.bash b/test/tests/config-json.bash
new file mode 100644
index 0000000..94ef0ac
--- /dev/null
+++ b/test/tests/config-json.bash
@@ -0,0 +1,20 @@
+test_description='json config allowed'
+. '../test-lib.bash'
+
+test_expect_success 'setup' '
+echo "grape" > ${DOTFILES}/h
+'
+
+test_expect_success 'run' '
+run_dotbot_json <<EOF
+[{
+ "link": {
+ "~/.i": "h"
+ }
+}]
+EOF
+'
+
+test_expect_success 'test' '
+grep "grape" ~/.i
+'