Skip to content

Commit

Permalink
Merge pull request #13 from thecodingmachine/jindun
Browse files Browse the repository at this point in the history
 fixing merge and delete commands, implementing comment command
  • Loading branch information
Jindun SHAO committed Jun 27, 2018
2 parents ef1f798 + 0f1a516 commit 1c28c88
Show file tree
Hide file tree
Showing 10 changed files with 551 additions and 187 deletions.
37 changes: 25 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,46 @@ A set of CLI tools to manipulate YAML files (merge, delete, etc...) with comment
(For development see section at the end)
```
$ pip install ruamel.yaml
$ export YAML_TOOLS_VERSION=0.2.0
$ export YAML_TOOLS_VERSION=0.0.3
$ sudo wget https://raw.githubusercontent.com/thecodingmachine/yaml-tools/${YAML_TOOLS_VERSION}/src/yaml_tools.py -O /usr/local/bin/yaml-tools
$ sudo chmod +x /usr/bin/yaml-tools
$ sudo chmod +x /usr/bin/local/yaml-tools
```

## Usage
```
$ yaml-tools <command> [<args>]
```

There are only 2 commands at the moments :
There are 3 commands at the moments :

### merge
Merge two or more yaml files and preserve the comments
Merges two or more yaml files and preserves the comments.
```
$ yaml-tools merge -i INPUTS [INPUTS ...] [-o OUTPUT] [--indent INDENT]
$ yaml-tools merge -i INPUTS [INPUTS ...] [-o OUTPUT]
```
- INPUTS: paths to input yaml files, which will be merged from the last to the first.
- OUTPUT: path to output yaml file (or sys.stdout by default).
- INDENT: number of space(s) for each indent.

### delete
Delete one item from the input yaml file
Deletes one item/block (and its preceding comments) from the input yaml file.
```
$ yaml-tools delete ITEM_PATH -i INPUT [-o OUTPUT] [--indent INDENT]
$ yaml-tools delete PATH_TO_KEY -i INPUT [-o OUTPUT]
```
- ITEM_PATH: yaml item to be deleted, e.g. `key1.list[0].key2`
- PATH_TO_KEY: "path" to access the yaml item/block which will be deleted, e.g. `key1 0 key2`
- INPUT: path to input yaml file.
- OUTPUT: path to output yaml file (or sys.stdout by default).

### comment (/!\ EXPERIMENTAL)
Comments one item/block from the input yaml file and preserves the comments.

There are somme issues with comments which are at the end of any intermediate level/block,
and also commenting the last item from a list, so use it with caution.
```
$ yaml-tools comment PATH_TO_KEY -i INPUT [-o OUTPUT]
```
- PATH_TO_KEY: "path" to access the yaml item which will be commented, e.g. `key1 0 key2`
- INPUT: path to input yaml file.
- OUTPUT: path to output yaml file (or sys.stdout by default).
- INDENT: number of space(s) for each indent.

## Development

Expand All @@ -53,9 +63,12 @@ $ yaml-tools delete ITEM_PATH -i INPUT [-o OUTPUT] [--indent INDENT]
- Activate your venv with `.\venv\Scripts\activate` (Windows) or `source ./venv/bin/activate` (Linux or MacOS)
- Install all required packages with `pip install -r requirements.txt`

## Running the tests
## Running tests
```
$ cd src/tests/
$ python -m unittest discover
$ python -m unittest discover
or
$ coverage run --rcfile=../../.coveragerc --source=.,.. -m unittest discover && coverage report -m
```
##
1 change: 0 additions & 1 deletion src/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

8 changes: 6 additions & 2 deletions src/tests/delete/expected_out.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
test:
foo:
h:
- check: ok
# comment2
- check: ok # comment3
ef: fefe
- ef: fefsegsegs
# comment4
- ef: fefsegsegs # comment5
- {}
# comment6
i: random
bar: 1
10 changes: 7 additions & 3 deletions src/tests/delete/file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
test:
foo:
h:
- check: ok
# comment2
- check: ok # comment3
ef: fefe
# comment4
- check: not_ok
ef: fefsegsegs
- fqffqzfq
ef: fefsegsegs # comment5
- check: not_ok # comment-nope
ef: nope # comment-nope
# comment6
i: random
bar: 1
36 changes: 35 additions & 1 deletion src/tests/merge/expected_out.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
#comment1
test:
foo: 2 #comment1
foo:
h: #comment1.0
# comment2
check: ok # comment3
ef: fefe
# new-comment
check2: not_ok
ef2: fefsegsegs # comment5
check3: not_ok # comment-ok
ef3: nope # comment-ok
# comment6
# comment7
i: # comment8
toto: random
bar: 3 #comment3
foo2:
h:
# comment2
- check: ok # comment3
ef: fefe
# comment4
- check: not_ok
ef: fefsegsegs
- check: not_ok
ef: nope # comment-ok
- check: okz # comment3
ef: fefed
- check: not_okay
ef: fefsegdzasegs # comment5
- check: not_ok # comment-ok
ef: nope # comment-ok
# comment6
i: randomd
foobar: 3 #comment3
# foo
# end
# end 2
18 changes: 15 additions & 3 deletions src/tests/merge/file1.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#comment1
test:
foo: 1 #comment1
bar: 1
foo:
h: #ok
check: ook
ef: fefedfr
# new-comment
check2: not_ok_
ef2: fefsegsegsf
check3: not_oke
ef3: nopee
i:
toto: random
# comment-bar
bar: 1
# end nope
# end nope
31 changes: 30 additions & 1 deletion src/tests/merge/file2.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
#comment1
test:
foo: 2
foo:
h: #comment1.0
# comment2
check: ok # comment3
ef: fefe
# comment4
check2: not_ok
ef2: fefsegsegs # comment5
check3: not_ok # comment-ok
ef3: nope # comment-ok
# comment6
# comment7
i: # comment8
toto: random
bar: 1
foo2:
h:
# comment2
- check: ok # comment3
ef: fefe
# comment4
- check: not_ok
ef: fefsegsegs
- check: not_ok
ef: nope # comment-ok
i: random
# foo
# end
# end 2
14 changes: 13 additions & 1 deletion src/tests/merge/file3.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
test:
bar: 3 #comment3
foobar: 3 #comment3
foobar: 3 #comment3
foo2:
h:
# comment2
- check: okz # comment3
ef: fefed
# comment4
- check: not_okay
ef: fefsegdzasegs # comment5
- check: not_ok # comment-ok
ef: nope # comment-ok
# comment6
i: randomd

0 comments on commit 1c28c88

Please sign in to comment.