Skip to content

Commit

Permalink
Merge pull request #16 from thecodingmachine/jindun
Browse files Browse the repository at this point in the history
deleting default version for normalize-docker-compose command
  • Loading branch information
Jindun SHAO committed Jul 6, 2018
2 parents 13491b1 + 4ec785d commit d5e292e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 39 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ $ yaml-tools delete PATH_TO_KEY -i INPUT [-o OUTPUT]
- **OUTPUT**: path to output yaml file (or sys.stdout by default).

### normalize-docker-compose
Normalize the input docker-compose file by adding the version (3.4 by default) and converting all key-value string
(e.g. 'foo=bar' or '80:8080') to key-value dicts inside the services' `ports` and `environment` fields,
Normalize the input docker-compose file by and converting all key-value string (e.g. 'foo=bar' or '80:8080')
to key-value dicts inside the services' `ports`, `labels` and `environment` fields,
and finally delete all duplicated volumes (**and its preceding comments**) for each services
```
$ yaml-tools normalize-docker-compose -i INPUT [-o OUTPUT] [--dc-version DC_VERSION]
$ yaml-tools normalize-docker-compose -i INPUT [-o OUTPUT]
```
- **INPUT**: path to input yaml file.
- **OUTPUT**: path to output yaml file (or sys.stdout by default).
- **DC_VERSION**: version of docker-compose to append if any found, `3.2` by default

### comment (/!\ EXPERIMENTAL)
Comments one item/block from the input yaml file and preserves the comments.
Expand Down
1 change: 0 additions & 1 deletion src/tests/normalize-docker-compose/expected_out.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ services:
read_only: false
volumes:
db_data:
version: '3.4'
17 changes: 0 additions & 17 deletions src/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,6 @@ def test_fail_comment_commented(self):


class TestNormalizeDockerComposeCommand(unittest.TestCase):
def test_normalize_random_file(self):
str = """#comment1
"definitely-not-a-docker-compose-file-lol"
#comment2"""
expected_str = """# #comment1
# "definitely-not-a-docker-compose-file-lol"
# #comment2
version: '3.4'
"""
out = yaml_tools.normalize_docker_compose(str, version='3.4')
expected_out = round_trip_load(expected_str, preserve_quotes=True)

yml = MyYAML()
out_str = yml.dump(out)
expected_out_str = yml.dump(expected_out)
self.assertEqual(out_str, expected_out_str)

def test_normalize_docker_compose_file(self):
file = './normalize-docker-compose/file.yml'
fo = './normalize-docker-compose/out.yml'
Expand Down
23 changes: 6 additions & 17 deletions src/yaml_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,34 +312,25 @@ def delete_duplicated_volumes(data):
return data


def normalize_docker_compose(content, version='3.4'):
def normalize_docker_compose(content):
"""
Comment the content if it's not a CommentedMap, add version if not found, convert all key-value string
(e.g. 'foo=bar' or '80:8080') to key-value dicts inside the services' `ports` and `environment` fields,
If content is a CommentedMap, convert all key-value string (e.g. 'foo=bar' or '80:8080')
to key-value dicts inside the services' `ports`, `labels` and `environment` fields,
and finally delete all duplicated volumes (and its preceding comments) for each services
"""
data = round_trip_load(content, preserve_quotes=True)
if isinstance(data, CommentedMap):
keys = [key.lower() for key in data.keys()]
if 'version' not in keys:
data['version'] = version
if 'services' in keys:
services = data['services']
for k in services:
if 'ports' in services[k] and isinstance(services[k]['ports'], CommentedSeq):
services[k]['ports'] = convert_commented_seq_to_dict(services[k]['ports'])
if 'environment' in services[k] and isinstance(services[k]['environment'], CommentedSeq):
services[k]['environment'] = convert_commented_seq_to_dict(services[k]['environment'])
if 'labels' in services[k] and isinstance(services[k]['labels'], CommentedSeq):
services[k]['labels'] = convert_commented_seq_to_dict(services[k]['labels'])
if 'environment' in services[k] and isinstance(services[k]['environment'], CommentedSeq):
services[k]['environment'] = convert_commented_seq_to_dict(services[k]['environment'])
delete_duplicated_volumes(data)
else:
start_mark = CommentMark(0)
comments = [CommentToken('# ' + line, start_mark, None) for line in content.splitlines()]
data = CommentedMap()
data.ca.comment = [None, comments]
data['version'] = version

return data


Expand Down Expand Up @@ -461,15 +452,13 @@ def normalize_docker_compose_command():
help='<Required> Path to the input yaml file', required=True)
parser.add_argument('-o', '--output', type=str,
help='Path to the output file, or stdout by default')
parser.add_argument('--dc-version', type=str,
help='Version of docker-compose', default='3.4')

args = parser.parse_args(sys.argv[2:])
input_file = open(args.input, 'r')
content = input_file.read()
input_file.close()

output_data = normalize_docker_compose(content, args.dc_version)
output_data = normalize_docker_compose(content)

output_file = open(args.output, 'w') if args.output else sys.stdout
round_trip_dump(output_data, output_file)
Expand Down

0 comments on commit d5e292e

Please sign in to comment.