diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0793df8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python +MAINTAINER think@hotmail.de + +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 diff --git a/README.md b/README.md index a395c55..3c8ca2a 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/Rakefile b/Rakefile index fa09c2e..edf6302 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/bin/compose_format b/bin/compose_format index 9894fa5..093638e 100755 --- a/bin/compose_format +++ b/bin/compose_format @@ -5,6 +5,7 @@ from compose_format import ComposeFormat if __name__ == '__main__': import argparse + import sys parser = argparse.ArgumentParser() parser.add_argument( @@ -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): diff --git a/compose_format/__init__.py b/compose_format/__init__.py index 1e0362a..4f2451a 100755 --- a/compose_format/__init__.py +++ b/compose_format/__init__.py @@ -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): @@ -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): diff --git a/setup.py b/setup.py index ef69d5e..96bfed1 100644 --- a/setup.py +++ b/setup.py @@ -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',