Skip to content

Commit

Permalink
Merge pull request #1 from cytopia/release-0.1
Browse files Browse the repository at this point in the history
Initial release
  • Loading branch information
cytopia authored Jul 28, 2019
2 parents 4c52a65 + 252c3c8 commit 70758e4
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---

###
### Enable sudo (required for docker service)
###
sudo: required


###
### Language
###
language: minimal


###
### Add services
###
services:
- docker


###
### Build Matrix
###
env:
matrix:
- VERSION=latest


###
### Install requirements
###
install:
- retry() {
for ((n=0; n<10; n++)); do
echo "[${n}] ${*}";
if eval "${*}"; then
return 0;
fi;
done;
return 1;
}


###
### Check generation changes, build and test
###
before_script:
- retry make lint
- retry make build TAG=${VERSION}
- retry make test TAG=${VERSION}


###
### Push to Dockerhub
###
script:
# Push to docker hub on success
- if [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then
while ! make login USER="${DOCKER_USERNAME}" PASS="${DOCKER_PASSWORD}"; do sleep 1; done;
if [ -n "${TRAVIS_TAG}" ]; then
while ! make push TAG="${VERSION}-${TRAVIS_TAG}"; do sleep 1; done;
elif [ "${TRAVIS_BRANCH}" == "master" ]; then
while ! make push TAG=${VERSION}; do sleep 1; done;
elif [[ ${TRAVIS_BRANCH} =~ ^(release-[.0-9]+)$ ]]; then
while ! make push TAG="${VERSION}-${TRAVIS_BRANCH}"; do sleep 1; done;
else
echo "Skipping branch ${TRAVIS_BRANCH}";
fi
else
echo "Skipping push on PR";
fi
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM alpine:3.9
LABEL \
maintainer="cytopia <[email protected]>" \
repo="https://github.com/cytopia/docker-yamlfmt"

ARG VERSION=latest
RUN set -x \
&& apk add --no-cache \
bash \
python3 \
&& if [ "${VERSION}" = "latest" ]; then \
pip3 install --no-cache-dir --no-compile yamlfmt; \
else \
pip3 install --no-cache-dir --no-compile yamlfmt==${VERSION}; \
fi \
&& find /usr/lib/ -name '__pycache__' -print0 | xargs -0 -n1 rm -rf \
&& find /usr/lib/ -name '*.pyc' -print0 | xargs -0 -n1 rm -rf

COPY ./data/docker-entrypoint.sh /docker-entrypoint.sh
WORKDIR /data
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["--help"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 cytopia <https://github.com/cytopia>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
88 changes: 88 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
ifneq (,)
.error This Makefile requires GNU Make.
endif

.PHONY: build rebuild lint test _test-run-ok _test-run-fail tag pull login push enter

CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

DIR = .
FILE = Dockerfile
IMAGE = cytopia/yamlfmt
TAG = latest

build:
docker build --build-arg VERSION=$(TAG) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR)

rebuild: pull
docker build --no-cache --build-arg VERSION=$(TAG) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR)

lint:
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-cr --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-crlf --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-single-newline --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-space --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8 --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8-bom --text --ignore '.git/,.github/,tests/' --path .

test:
@$(MAKE) --no-print-directory _test-run-ok
@$(MAKE) --no-print-directory _test-run-fail

_test-run-ok:
@echo "------------------------------------------------------------"
@echo "- Testing valid yaml files"
@echo "------------------------------------------------------------"
@if ! docker run --rm -v $(CURRENT_DIR)/tests/ok:/data $(IMAGE) ok1.yml ; then \
echo "Failed"; \
exit 1; \
fi; \
if ! docker run --rm -v $(CURRENT_DIR)/tests/ok:/data $(IMAGE) ok2.yml ; then \
echo "Failed"; \
exit 1; \
fi; \
if ! docker run --rm -v $(CURRENT_DIR)/tests/ok:/data $(IMAGE) '*.yml' ; then \
echo "Failed"; \
exit 1; \
fi; \
echo "Success";

_test-run-fail:
@echo "------------------------------------------------------------"
@echo "- Testing invalid yaml files"
@echo "------------------------------------------------------------"
@if docker run --rm -v $(CURRENT_DIR)/tests/fail:/data $(IMAGE) fail1.yml ; then \
echo "Failed"; \
exit 1; \
fi; \
if docker run --rm -v $(CURRENT_DIR)/tests/fail:/data $(IMAGE) fail2.yml ; then \
echo "Failed"; \
exit 1; \
fi; \
if docker run --rm -v $(CURRENT_DIR)/tests/fail:/data $(IMAGE) notexisting.yml ; then \
echo "Failed"; \
exit 1; \
fi; \
if docker run --rm -v $(CURRENT_DIR)/tests/fail:/data $(IMAGE) '*.yml' ; then \
echo "Failed"; \
exit 1; \
fi; \
echo "Success";

tag:
docker tag $(IMAGE) $(IMAGE):$(TAG)

pull:
@grep -E '^\s*FROM' Dockerfile \
| sed -e 's/^FROM//g' -e 's/[[:space:]]*as[[:space:]]*.*$$//g' \
| xargs -n1 docker pull;

login:
yes | docker login --username $(USER) --password $(PASS)

push:
@$(MAKE) tag TAG=$(TAG)
docker push $(IMAGE):$(TAG)

enter:
docker run --rm --name $(subst /,-,$(IMAGE)) -it --entrypoint=/bin/sh $(ARG) $(IMAGE):$(TAG)
158 changes: 158 additions & 0 deletions data/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/usr/bin/env bash

# Be strict
set -e
set -u
set -o pipefail


###
### Globals
###
ARG_WRITE=0 # yamlfmt arg for -w
ARG_IGNORE= # yamlfmt arg to ignore files found via glob
REG_GLOB='(\*.+)|(.+\*)|(.+\*.+)' # Regex pattern to identify valid glob supported by 'find'


###
### Show Usage
###
print_usage() {
>&2 echo "Usage: cytopia/yamlfmt [-h] [-w] [file|pattern]"
>&2 echo
>&2 echo "positional arguments:"
>&2 echo " file file to parse"
>&2 echo " pattern glob pattern for recursive scanning. (e.g.: *\\.yml)"
>&2 echo
>&2 echo "optional arguments:"
>&2 echo " -h, --help show this help message and exit"
>&2 echo " -w, --write write formatted outpout to (source) file instead of stdout"
>&2 echo " -i, --ignore comma separated list of paths to ignore when using glob pattern."
}


###
### Validate YAML file
###
### @param int write file (-w): 1: Yes and 0: No
### @param string Path to file.
### @return int Success (0: success, >0: Failure)
###
_yamlfmt() {
local write="${1}"
local file="${2}"
# shellcheck disable=SC2155
local temp="/tmp/$(basename "${file}")"
local ret=0

# Overwrite original file
if [ "${write}" -eq "1" ]; then
local cmd="yamlfmt -w ${file}"
echo "${cmd}"

if ! eval "${cmd}" > "${temp}"; then
ret=$(( ret + 1 ))
fi

# Diff file
else
local cmd="yamlfmt ${file}"
echo "${cmd}"

cp -f "${file}" "${temp}"
if ! eval "yamlfmt -w ${temp}"; then
ret=$(( ret + 1 ))
fi

# Only diff if file is not empty
if [ -s "${temp}" ]; then
if ! diff "${file}" "${temp}"; then
ret=$(( ret + 1 ))
fi
fi
fi

return "${ret}"
}


###
### Arguments appended?
###
if [ "${#}" -gt "0" ]; then

while [ "${#}" -gt "0" ]; do
case "${1}" in
# Show Help and exit
--help|-h)
print_usage
exit 0
;;
# Add yamlfmt argument to write to file
--write|-w)
shift
ARG_WRITE=1
;;
# Ignore glob patterh
-i)
shift
if [ "${#}" -lt "1" ]; then
>&2 echo "Error, -i requires an argument"
exit 1
fi
ARG_IGNORE="${1}"
shift
;;
# Anything else is handled here
*)
# Case 1/2: Its a file
if [ -f "${1}" ]; then
# Argument check
if [ "${#}" -gt "1" ]; then
>&2 echo "Error, you cannot specify arguments after the file position."
print_usage
exit 1
fi
_yamlfmt "${ARG_WRITE}" "${1}"
exit "${?}"
# Case 2/2: Its a glob
else
# Glob check
if ! echo "${1}" | grep -qE "${REG_GLOB}"; then
>&2 echo "Error, invalid file or wrong glob format. Allowed: '${REG_GLOB}'"
exit 1
fi
# Argument check
if [ "${#}" -gt "1" ]; then
>&2 echo "Error, you cannot specify arguments after the glob position."
print_usage
exit 1
fi

# Iterate over all files found by glob and jsonlint them
if [ -z "${ARG_IGNORE}" ]; then
find_cmd="find . -name \"${1}\" -type f -print0"
else
find_cmd="find . -not \( -path \"${ARG_IGNORE}\" \) -name \"${1}\" -type f -print0"
fi

echo "${find_cmd}"
ret=0
while IFS= read -rd '' file; do
if ! _yamlfmt "${ARG_WRITE}" "${file}"; then
ret=$(( ret + 1 ))
fi
done < <(eval "${find_cmd}")
exit "${ret}"
fi
;;
esac
done

###
### No arguments appended
###
else
print_usage
exit 0
fi
4 changes: 4 additions & 0 deletions tests/fail/fail1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
test:
hallo:
john: 1
Empty file added tests/fail/fail2.yml
Empty file.
25 changes: 25 additions & 0 deletions tests/ok/ok1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
invoice: 34843
date: 2001-01-23
bill-to:
given: Chris
family: Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to: id001
product:
- sku: BL394D
quantity: 4
description: Basketball
price: 450.0
- sku: BL4438H
quantity: 1
description: Super Hoop
price: 2392.0
tax: 251.42
total: 4443.52
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
Loading

0 comments on commit 70758e4

Please sign in to comment.