1
0
Fork 0
mirror of synced 2024-11-03 00:48:57 -04:00

Use standard library JSON parser for JSON files

This patch reverts the changes to the README made in
57265f78b4 and makes it so that Dotbot
supports JSON files with tab characters.
This commit is contained in:
Anish Athalye 2016-01-13 11:29:12 -05:00
parent 9250bef422
commit c48d16cbce
5 changed files with 63 additions and 8 deletions

View file

@ -118,13 +118,9 @@ Configuration
------------- -------------
Dotbot uses YAML or JSON formatted configuration files to let you specify how 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 to set up your dotfiles. Currently, Dotbot knows how to [link](#link) files and
cleaner. JSON will be parsed as a subset of YAML, so tab characters are not folders, execute [shell](#shell) commands, and [clean](#clean) directories of
permitted. broken symbolic links.
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 **Ideally, bootstrap configurations should be idempotent. That is, the
installer should be able to be run multiple times without causing any installer should be able to be run multiple times without causing any

View file

@ -1,4 +1,5 @@
import yaml import yaml
import json
from .util import string from .util import string
class ConfigReader(object): class ConfigReader(object):
@ -8,7 +9,16 @@ class ConfigReader(object):
def _read(self, config_file_path): def _read(self, config_file_path):
try: try:
with open(config_file_path) as fin: with open(config_file_path) as fin:
try:
data = yaml.safe_load(fin) 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 return data
except Exception as e: except Exception as e:
msg = string.indent_lines(str(e)) msg = string.indent_lines(str(e))

View file

@ -1,6 +1,7 @@
DEBUG=false DEBUG=false
DOTFILES='/home/vagrant/dotfiles' DOTFILES='/home/vagrant/dotfiles'
INSTALL_CONF='install.conf.yaml' INSTALL_CONF='install.conf.yaml'
INSTALL_CONF_JSON='install.conf.json'
test_run_() { test_run_() {
if ! ${DEBUG}; then 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 initialize

View file

@ -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
'

View file

@ -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
'