Skip to content

Commit

Permalink
support for docker
Browse files Browse the repository at this point in the history
  • Loading branch information
lindt committed Mar 13, 2016
1 parent a68b0dd commit 15c5929
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python
MAINTAINER [email protected]

RUN pip install pyaml

ADD bin /bin
COPY compose_format /usr/local/lib/python3.5/site-packages/compose_format
ADD features /
ADD Dockerfile /
ADD README.md /

RUN chmod +x /bin/compose_format

ENTRYPOINT python3 /bin/compose_format
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# compose_format
Format docker compose files.

Format docker-compose files.

Note that this small utility is just valid until docker-compose has itself a format functionality.
Currently docker-compose just support the "config" switch. Which joins multiple compose files and print them in a machine readable form.
Expand All @@ -11,10 +12,16 @@ Currently docker-compose just support the "config" switch. Which joins multiple
Install it via:
`pip3 install compose_format`

After that use it like

`compose_format compose-format.yml`
this will print the formatted compose file to stdout.
To let it replace the compose file add `--replace`.

### Via Docker

After that use it like:
`echo "docker-compose.yml" > docker run -t compose_format`
Use it like:
`cat docker-compose.yml | docker run -i funkwerk/compose_format`

## Features
- Support for Version 2 and Version 1.
Expand Down
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ end
task :generate do
sh 'pandoc --from=markdown --to=rst --output=README.rst README.md'
end

task :docker do
sh 'docker build --tag=funkwerk/compose_format .'
end
14 changes: 10 additions & 4 deletions bin/compose_format
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from compose_format import ComposeFormat
if __name__ == '__main__':

import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument(
Expand All @@ -15,12 +16,17 @@ if __name__ == '__main__':
help='ignore changes for return code', default=True)
parser.add_argument('files', nargs=argparse.REMAINDER)
args = parser.parse_args()
formatter = ComposeFormat()

if len(args.files) == 0:
import sys
print('specify at least one file')
sys.exit(1)
assert args.replace is False, 'replace makes no sense when reading from stdin'

formatter = ComposeFormat()
data = sys.stdin.read()
formatted = formatter.format_string(data)
print(formatted)
if not args.ignore_changes:
if data != formatted:
sys.exit(1)

for path in args.files:
if not formatter.format(path, args.replace):
Expand Down
19 changes: 11 additions & 8 deletions compose_format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def format(self, path, replace=False):
with open(path, 'r') as file:
data = file.read()
original = data
formatted = self.format_string(data, replace=replace)

if replace:
with open(path, 'w') as file:
file.write(formatted)
else:
print(formatted)
return original == formatted

def format_string(self, data, replace=False):
data = self.reorder(load(data))

def is_legacy_version(data):
Expand All @@ -35,14 +45,7 @@ def is_legacy_version(data):
vspacing = [1, 0] if is_legacy_version(data) else [0, 1, 0]

formatted = pyaml.dump(data, vspacing=vspacing, indent=2, width=120, string_val_style='plain')
formatted = formatted.strip() + '\n'

if replace:
with open(path, 'w') as file:
file.write(formatted)
else:
print(formatted)
return original == formatted
return formatted.strip() + '\n'

@staticmethod
def reorder(data):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def readme():

setup(
name='compose_format',
version='0.1.3',
description='format docker compose files',
version='0.1.4',
description='format docker-compose files',
long_description=readme(),
url='http://github.com/funkwerk/compose_format',
author='Stefan Rohe',
Expand Down

0 comments on commit 15c5929

Please sign in to comment.