Giter VIP home page Giter VIP logo

cytopia / docker-file-lint Goto Github PK

View Code? Open in Web Editor NEW
13.0 4.0 4.0 90 KB

Alpine-based Docker image to perform generic file checks on your source code in order to improve consistency within your repository (e.g. for easy usage in CI).

License: MIT License

Makefile 13.39% Shell 84.51% HTML 0.52% CSS 0.14% JavaScript 0.10% Perl 0.47% PHP 0.25% Python 0.30% Ruby 0.18% SCSS 0.14%
alpine multistage multistage-docker docker dockerfile docker-image docker-container ci continuous-integration lint

docker-file-lint's Introduction

Docker image for file-lint

Tag License

lint build nightly

All #awesome-ci Docker images

ansible-lint ansible awesome-ci bandit black checkmake eslint file-lint gofmt goimports golint jsonlint kubeval linkcheck mypy php-cs-fixer phpcbf phpcs phplint pycodestyle pydocstyle pylint terraform-docs terragrunt-fmt terragrunt yamlfmt yamllint

View Dockerfiles on GitHub.

Available Architectures: amd64, i386, arm64, arm/v7, arm/v6

Tiny Alpine-based Docker image for the very basics of CI against your code files based one awesome-ci[1].

[1] Original project: https://github.com/topics/awesome-ci

🐳 Available Docker image versions

Docker

Rolling releaess

The following Docker image tags are rolling releases and are built and updated every night.

nightly

Docker Tag Git Ref file-lint Flavour Available Architectures
latest master latest default amd64, i386, arm64, arm/v7, arm/v6
alpine master latest Alpine amd64, i386, arm64, arm/v7, arm/v6

Point in time releases

The following Docker image tags are built once and can be used for reproducible builds. Its version never changes so you will have to update tags in your pipelines from time to time in order to stay up-to-date.

build

Docker Tag Git Ref file-lint Flavour Available Architectures
latest-<tag> tag: <tag> latest default amd64, i386, arm64, arm/v7, arm/v6
alpine-latest-<tag> tag: <tag> latest Alpine amd64, i386, arm64, arm/v7, arm/v6

Where <tag> refers to the chosen git tag from this repository.

⭐ Features

  • dry run (which shows all piped unix command voodoo for learning)
  • project based configuration file (awesome-ci.conf)
  • check for empty files
  • check for files with carriage returns (\r)
  • check for files with windows newlines (\r\n)
  • check for files with nullbyte characters (\x00)
  • check for trailing newlines at eof (exactly one or multiple)
  • check for trailing white space
  • ensure files are utf8 encoded
  • ensure files do not contain utf8 bom (byte order mark: U+FEFF)
  • allows for automatic fixing (most commands)
  • allows for find-grained control
    • check files by specific extension(s) only
    • check files by specific shebang only
    • check binary or text-files only

🔧 Tools

Type Tool Fixable Description
File file-cr Scan files and check if they contain CR (Carriage Return only).
File file-crlf Scan files and check if they contain CRLF (Windows Line Feeds).
File file-empty Scan files and check if they are empty (0 bytes).
File file-nullbyte Scan files and check if they contain a null-byte character (\x00).
File file-trailing-newline Scan files and check if they contain a trailing newline.
File file-trailing-single-newline Scan files and check if they contain exactly one trailing newline.
File file-trailing-space Scan files and check if they contain trailing whitespaces.
File file-utf8 Scan files and check if they have a non UTF-8 encoding.
File file-utf8-bom Scan files and check if they contain BOM (Byte Order Mark): U+FEFF.
Git git-conflicts Scan files and check if they contain git conflicts.

Tools extracted from https://github.com/cytopia/awesome-ci

📂 Docker mounts

The working directory inside the Docker container is /data/ and should be mounted locally.

💻 Usage

General

$ docker run --rm -v $(pwd):/data cytopia/file-lint

################################################################################
#                              cytopia/file-lint                               #
#                                 (awesome-ci)                                 #
################################################################################
#                                                                              #
#                                                                              #
# Usage:                                                                       #
# -----------------------------------------------------------------------------#
# docker run --rm cytopia/file-lint --help                                     #
# docker run --rm cytopia/file-lint <tool> --help                              #
# docker run --rm cytopia/file-lint <tool> --info                              #
# docker run --rm cytopia/file-lint <tool> --version                           #
#                                                                              #
#                                                                              #
# Available tools:                                                             #
# -----------------------------------------------------------------------------#
# file-empty                    Scans if files are empty                       #
# file-cr                       Scans if files contain carriage returns (\r)   #
# file-crlf                     Scans if files contain win line feeds (\r\n)   #
# file-nullbyte                 Scans if files contain nullbyte chars (\x00)   #
# file-trailing-newline         Scans if files contain trailing newline(s)     #
# file-trailing-single-newline  Scans if files contain single trailing newline #
# file-trailing-space           Scans if files contain trailing whitespace     #
# file-utf8                     Scans if files are utf8 encoded                #
# file-utf8-bom                 Scans if files contain byte order mark         #
# git-conflicts                 Scans if files contain git conflicts           #
#                                                                              #
#                                                                              #
# Example:                                                                     #
# -----------------------------------------------------------------------------#
# docker run --rm -v $(pwd):/data cytopia/file-lint \                          #
#     lf-crlf --ignore ".git/,.github/" --path .                               #
#                                                                              #
################################################################################

Tool usage

The following help screen is taken from file-crlf. All other tools have the exact same functionality.

$ docker run --rm cytopia/file-lint file-crlf --help

Usage: file-crlf [--text] [--size] [--shebang <ARG>] [--extension "tpl,htm,html,php,..."] [--ignore "dir1,dir2"] [--config "conf"] [--confpre "FILE_CRLF_"] [--fix] [--verbose] [--debug] [--dry] [--list] --path <DIR>
       file-crlf --info
       file-crlf --help
       file-crlf --version

Scans recursively for files containing CRLF (Windows Line Feeds).
Will return 1 on occurance, otherwise 0.

Required arguments:

  --path <ARG>       Specify directory where to scan.


Optional run arguments:
  --fix              Fixable :-)
                     Fix the problems for the specified files.
                     Note, all other options below also apply

  --text             Limit search to text files only (non-binary).
                     Can be narrowed further with '--extension'

  --size             Limit search to files which are not empty (bigger than 0 bytes).

  --shebang <ARG>    Only find files (shell scripts) with this specific shebang.
                     It is useful to combine this with --text and --size for faster searches.
                     Use with --dry to see how this search command is generated.
                     Example:
                         --shebang bash
                         --shebang php
                         --shebang sh

  --extension <ARG>  Only find files matching those extensions.
                     Comma separated list of file extensions.
                     Only find files matching those extensions.
                     Defaults to all files if not specified or empty.
                     Example:
                         --extension "html,php,inc"
                         --extension php

  --ignore <ARG>     Comma separated list of ignore paths.
                     Directories must be specified from the starting location of --path.
                     Example:
                          ignore 'foor/bar' folder inside '/var/www' path:
                         --path /var/www --ignore foo/bar

  --config <ARG>     Load configuration file.
                     File must contain the following directives:
                         FILE_CRLF_EXTENSION="" # comma separated
                         FILE_CRLF_IGNORE=""    # comma separated
                         FILE_CRLF_TEXT=0|1     # 0 or 1
                     Note that cmd arguments take precedence over
                     config file settings.

  --confpre <ARG>    Set custom configuration directive prefix.
                     Current default ist: 'FILE_CRLF_'.
                     This is useful, when you want to define different defaults
                     per check via configuration file.

  --verbose          Be verbose and print commands and files being checked.


  --debug            Print system messages.


Optional training arguments:
  --dry              Don't do anything, just display the commands.

  --list             Instead of searching inside the files, just display the filenames
                     that would be found by --path, --extension and --ignore


System arguments:
  --info             Show versions of required commands (useful for bugreports).
  --help             Show help screen.
  --version          Show version information.


file-crlf is part of the awesome-ci collection.
https://github.com/cytopia/awesome-ci

Configuration file

You can also add a configuration file named awesome-ci.conf to your project to configure it to your likings.

#
# Awesome-ci configuration file
#
# Each tool will have its own config section
# which all behave in the same way:
#
#
# 1. File extensions
# ------------------
# Comma separated list of file extensions
# to narrow down the files to check.
#     <TOOL_NAME>_EXTENSION=""
#     <TOOL_NAME>_EXTENSION="tpl,html"
#
# 2. Ignored paths
# ----------------
# Comma separated list of file paths
# to narrow down the files to check.
# Note that those paths must start at the
# path where --path starts.
#     <TOOL_NAME>_IGNORE=""
#     <TOOL_NAME>_IGNORE="tmp/log,tmp/run"
#
# 3. Text files
# -------------
# 0 or 1 to specify whether to work on text files
# only.
#     <TOOL_NAME>_TEXT=0
#     <TOOL_NAME>_TEXT=1




#
# File checkers
#
# file-cr
FILE_CR_EXTENSION=""
FILE_CR_IGNORE=".git,*.svn"
FILE_CR_TEXT=1
FILE_CR_SIZE=1

# file-crlf
FILE_CRLF_EXTENSION=""
FILE_CRLF_IGNORE=".git,*.svn"
FILE_CRLF_TEXT=1
FILE_CRLF_SIZE=1

# file-empty
FILE_EMPTY_EXTENSION=""
FILE_EMPTY_IGNORE=".git,*.svn"
FILE_EMPTY_TEXT=0
FILE_EMPTY_SIZE=0

# file-nullbyte
FILE_NULLBYTE_EXTENSION=""
FILE_NULLBYTE_IGNORE=".git,*.svn,*.pyc"
FILE_NULLBYTE_TEXT=1
FILE_NULLBYTE_SIZE=1

# file-trailing-newline
FILE_TRAILING_NEWLINE_EXTENSION=""
FILE_TRAILING_NEWLINE_IGNORE=".git,*.svn"
FILE_TRAILING_NEWLINE_TEXT=1
FILE_TRAILING_NEWLINE_SIZE=1

# file-trailing-single-newline
FILE_TRAILING_SINGLE_NEWLINE_EXTENSION=""
FILE_TRAILING_SINGLE_NEWLINE_IGNORE=".git,*.svn"
FILE_TRAILING_SINGLE_NEWLINE_TEXT=1
FILE_TRAILING_SINGLE_NEWLINE_SIZE=1

# file-trailing-space
FILE_TRAILING_SPACE_EXTENSION=""
FILE_TRAILING_SPACE_IGNORE=".git,*.svn"
FILE_TRAILING_SPACE_TEXT=1
FILE_TRAILING_SPACE_SIZE=1

# file-utf8
FILE_UTF8_EXTENSION=""
FILE_UTF8_IGNORE=".git,*.svn"
FILE_UTF8_TEXT=1
FILE_UTF8_SIZE=1

# file-utf8-bom
FILE_UTF8_BOM_EXTENSION=""
FILE_UTF8_BOM_IGNORE=".git,*.svn"
FILE_UTF8_BOM_TEXT=1
FILE_UTF8_BOM_SIZE=1

# git-conflicts
GIT_CONFLICTS_EXTENSION=""
GIT_CONFLICTS_IGNORE=".git,*.svn"
GIT_CONFLICTS_TEXT=1
GIT_CONFLICTS_SIZE=1

Example Makefile

ifneq (,)
.error This Makefile requires GNU Make.
endif

.PHONY: lint _lint-cr _lint-crlf _lint-trailing-single-newline _lint-trailing-space _lint-utf8 _lint-utf8-bom _lint-git-conflicts

FL_VERSION      = latest
FL_IGNORE_PATHS = .git/,.github/

lint:
	@$(MAKE) --no-print-directory _lint-cr
	@$(MAKE) --no-print-directory _lint-crlf
	@$(MAKE) --no-print-directory _lint-trailing-single-newline
	@$(MAKE) --no-print-directory _lint-trailing-space
	@$(MAKE) --no-print-directory _lint-utf8
	@$(MAKE) --no-print-directory _lint-utf8-bom
    @$(MAKE) --no-print-directory _lint-git-conflicts

_lint-cr:
	@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '$(FL_IGNORE_PATHS)' --path .

_lint-crlf:
	@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '$(FL_IGNORE_PATHS)' --path .

_lint-trailing-single-newline:
	@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '$(FL_IGNORE_PATHS)' --path .

_lint-trailing-space:
	@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '$(FL_IGNORE_PATHS)' --path .

_lint-utf8:
	@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '$(FL_IGNORE_PATHS)' --path .

_lint-utf8-bom:
	@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '$(FL_IGNORE_PATHS)' --path .

_lint-git-conflicts:
	@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) git-conflicts --text --ignore '$(FL_IGNORE_PATHS)' --path .

🔄 Related #awesome-ci projects

Docker images

Save yourself from installing lot's of dependencies and pick a dockerized version of your favourite linter below for reproducible local or remote CI tests:

GitHub DockerHub Type Description
awesome-ci aci-hub-img Basic Tools for git, file and static source code analysis
file-lint flint-hub-img Basic Baisc source code analysis
linkcheck linkcheck-hub-img Basic Search for URLs in files and validate their HTTP status code
ansible ansible-hub-img Ansible Multiple versions and flavours of Ansible
ansible-lint alint-hub-img Ansible Lint Ansible
gofmt gfmt-hub-img Go Format Go source code [1]
goimports gimp-hub-img Go Format Go source code [1]
golint glint-hub-img Go Lint Go code
eslint elint-hub-img Javascript Lint Javascript code
jsonlint jlint-hub-img JSON Lint JSON files [1]
kubeval kubeval-hub-img K8s Lint Kubernetes files
checkmake cm-hub-img Make Lint Makefiles
phpcbf pcbf-hub-img PHP PHP Code Beautifier and Fixer
phpcs pcs-hub-img PHP PHP Code Sniffer
phplint plint-hub-img PHP PHP Code Linter [1]
php-cs-fixer pcsf-hub-img PHP PHP Coding Standards Fixer
bandit bandit-hub-img Python A security linter from PyCQA
black black-hub-img Python The uncompromising Python code formatter
mypy mypy-hub-img Python Static source code analysis
pycodestyle pycs-hub-img Python Python style guide checker
pydocstyle pyds-hub-img Python Python docstyle checker
pylint pylint-hub-img Python Python source code, bug and quality checker
terraform-docs tfdocs-hub-img Terraform Terraform doc generator (TF 0.12 ready) [1]
terragrunt tg-hub-img Terraform Terragrunt and Terraform
terragrunt-fmt tgfmt-hub-img Terraform terraform fmt for Terragrunt files [1]
yamlfmt yfmt-hub-img Yaml Format Yaml files [1]
yamllint ylint-hub-img Yaml Lint Yaml files

[1] Uses a shell wrapper to add enhanced functionality not available by original project.

Makefiles

Visit cytopia/makefiles for dependency-less, seamless project integration and minimum required best-practice code linting for CI. The provided Makefiles will only require GNU Make and Docker itself removing the need to install anything else.

📄 License

MIT License

Copyright (c) 2019 cytopia

docker-file-lint's People

Contributors

cytopia avatar dependabot[bot] avatar kbabioch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

docker-file-lint's Issues

Tools - Support GNU Parallel

Hi, pretty neat set of scripts this project has.

One thing I've noticed is that on relatively large directories with a large number of files, these scripts can occasionally take a good amount of time.

It would be nice if the scripts could leverage GNU Parallel to be able to process multiple files at once.

Taking a script like:

[OK]  All files are UTF-8 encoded.
$ find . -type f    -print0 | xargs -0 -P 2 -n1 grep -Il '' | tr '\n' '\0' | xargs -0 -P 2 -n1  sh -c 'if [ -f "${1}" ]; then awk "/^\xEF\xBB\xBF/ {print FILENAME} {nextfile}" "$1" || true; fi' --

To:

[OK]  All files are UTF-8 encoded.
$ find . -type f    -print0 | parallel 'xargs -0 -P 2 -n1 grep -Il \'\' | tr \'\n\' \'\0\' | xargs -0 -P 2 -n1  sh -c \'if [ -f "${1}" ]; then awk "/^\xEF\xBB\xBF/ {print FILENAME} {nextfile}" "$1" || true; fi\' --'

I don't fully know if this has been something that has been considered, but just a nice thought.

Asterisk (*) added at end of ignore pattern, bug?

When defining an ignore pattern, e.g. FILE_TRAILING_NEWLINE_IGNORE=".git,*.svg,*.excalidraw", the resulting path will get an * added at the end. Example:

`find . -type f -not \( -path "./.git*" -o -path "./*.svg*" -o -path "./*.excalidraw*" \) ! -size 0 -print0 | xargs -0 -P 8 -n1 grep -Il '' | tr '\n' '\0' | xargs -0 -P 8 -n1 sh -c 'if [ -f "${1}" ]; then aci-trailing-newline -n "$1" || echo "$1: No trailing newline at EOF."; fi' --`

This seems like a bug, this makes it impossible to only match files with certain extensions without also matching files having that extension in the middle of the file name, e.g. somefile.excalidraw.custom_ending will also be ignored.

It seems like this is the code responsible:

#
# 'find' pattern for ignores/excludes
#
if [ "${MY_IGN}" != "" ]; then
EXCL_PATTERN="-not \( -path \"${MY_PATH}/${MY_IGN//,/*\" -o -path \"${MY_PATH}\/}*\" \)"
else
EXCL_PATTERN=""
fi

would it be possible to not add an asterisk at the end?

Description of repository should be refined

Currently the description of the repository says this:

Alpine-based Docker image for the very basics of CI against your code files for reproducible usage in CI

This is very abstract and redundant (contains the word CI multiple times). At least for me it is misleading and non-descriptive, so by only reading this description I have no idea what this repository is about.

Therefore I think it should be re-phrased.

My suggest (feel free to change it, though) is the following:

Alpine-based Docker image to perform generic file checks on your source code in order to improve consistency within your repository (e.g. for easy usage in CI).

file-nullbyte messes up terminal (when run against binary files?)

The file-nullbyte check seems to mess up terminals. I've checked it on multiple *nix machines (Linux, macOS) and the terminals are only showing "weird" characters and/or behave strangely, after the following command is run:

docker run --rm -v $(pwd):/data cytopia/file-lint file-nullbyte --path .

$(pwd) is the checked out repository, and contains some binary files inside the .git/ directory

The result looks something like this:

W7Om,ҧ)ӘG2
R2        iG:U!%{|=7h>r$rCzيZWbnڒ0[~`ٻ@|?B屙QlW`/UA=~F};aL_ٚ1xmIo@Mܬ
m99φiXn t?0LӚbΤy`QV(oWiƯk0vUpC6`yn]s_I+}R*g\7r3A@B{<lkuH5nY?$ebxjbBxiX9MyCgm5؈c$1qP&uQUF        xYÙx</JNI?6,D"(A(r)N<
                                                                            r$R%T-@&^(~%Yk175,c1omiwziìĿXC7;zgY6co<
                                                                                                                   fwSXSdme^Z(NЉ*lw]Tl3
                                                                                                                                       g<k..NL(K)hv9&5տlX9=-k
W3~?lwKzBwEP?Wipx|4q.YF1~K93e36ef28bb545f9c4650c7f13ce2f59b442180.pack:|* #*-2Ds*2E%к<  d_xږGՕYSOb9'YIz9sU_~^ @wYdrҴ:x/U5ӧHtYaYbNla|0m쇷k7XYpi1;
                          *h#!o<Ov6thOQ]&r5Sk$wɄ_N>VSh"ldRD@nt?|hk]fi7Li9(i؄TP
                                                                             k,ć|9g4}w0
                                                                                       nrua,
;r͎¨rvz֍Ki+d瑿AAeOV\cp?Z,<*f{woYvMSTytVnZ*                                                   F9W_̲D037웨5yi{:0~PgBӌĥ-]w˷z         ەG3L:#;@X*
                                         :i6bp?%XF?R    ѮCSa8[fOga1xmKs@ڣ"Hb}dOSWW35UNTE
./.git/objects/pack/pack-693e36ef28bb545f9c4650c7f13ce2f59b442180.pack:BrR1Ρ$AEgͤyLi(UXIDDAD!%5S          CAMV   !PCrOC<34EU'
qsɽM)epKũ                                                                                                                   U@ ⶮa`XŰ)xlZκjzΊ!,벾2Lo -OE[O.\z#6tXz`/Ѥbk8j^׳'k^؍fgOv
./.git/objects/pack/pack-693e36ef28bb545f9c4650c7f13ce2f59b442180.pack:c2ljxvPAMyFX
vIDu]E\S'v SAHSەB,%;3mSUU䡝<5s
                              f#G9duyu4}|JdS6-Ox_oT_6g
                                                      tcUN#M)oHڸUq
                                                                  %I~XDxSˮFQk#6e

                                                                                |}dUzUV]dƀ)b
./.git/objects/pack/pack-693e36ef28bb545f9c4650c7f13ce2f59b442180.pack:3
LkftbFPpHx54H}/pack/pack-693e36ef28bb545f9c4650c7f13ce2f59b442180.pack:-+z\gָ| y(Bdr\[k8'c9(yt;Umc

The terminal needs to be reset afterwards.

The example snippet for this check looks like this:

# file-crlf
FILE_NULLBYTE_EXTENSION=""
FILE_NULLBYTE_IGNORE=".git,*.svn,*.pyc"
FILE_NULLBYTE_TEXT=0
FILE_NULLBYTE_SIZE=1

This results in binary files being scanned.

Now I'm wondering:

  • Is it a good idea to run this check against binary files in the first place?
  • Why is it messing up the terminal? This should not happen, regardless of whether binary files are involved or not.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.