Skip to main content

an extendible dispatcher to lint/format code, based on rifle

Project description

rifleman

PyPi version Python 3.6|3.7|3.8 PRs Welcome

An extendible dispatcher to lint/format code, based on rifle

This lets you run one command over lots of files/directories which could be in different languages - it classifies them by inspecting the mime type, extension, name or shebang, and then runs a command on those files.

This heavily simplifies and modifies the rifle config file format; including a condition to help check the 'shebang' value for scripts.

See config/format.conf for the default configuration file, I recommend you customize it to include the commands you use.

By default, I've included lots of the formaters/linters I use, an excerpt:

# html
ext x?html?, has prettier = prettier -w "$@"
# web technologies, handled by prettier
ext vue|yaml|json|graphql|tsx?|jsx?|s?css|less|md, has prettier = prettier -w "$@"

# golang
ext go, has go = go fmt "$@"

# python
ext py, has black = black "$@"
mime text/x-script.python, has black = black "$@"
shebang python(2|3)?, has black = black "$@"

# shell script
shebang zsh|bash, has shfmt = shfmt -w "$@"
shebang \/bin\/sh, has shfmt = shfmt -w "$@"
ext sh|(ba|z)sh, has shfmt = shfmt -w "$@"
mime text/x-shellscript, has shfmt = shfmt -w "$@"

Used emacs-format-all-the-code as reference.

Feel free to PR additional formatters!

Installation

Requires python3.6+

To install with pip, run:

pip install rifleman

Usage

Usage: rifleman [-] [-ljpcah] [files]...

Pass '-' to read filenames from STDIN, separated by newlines

Options:
  -h, --help      show this help message and exit
  -l              list actions for files
  -j              list actions for files as JSON
  -p              prompt before running each command
  -c CONFIG_FILE  read config from specified file instead of default
  -a ACTION       name of configuration file in config directory to use
                  (lint|format)

This doesn't offer a way to discover/search for files, because so many tools already exist to do that.

With shell globbing:

rifleman *.md ./project/*.py

To run this against all files in a git-tracked directory:

git ls-files | rifleman -

You can find (with the -exec flag), or the friendlier fd, to run against all files in the directory recursively:

fd -X rifleman

The -j and -l flags print what commands which would be used on each file instead of running the command.

The -c and -a files are used to determine which config file to use, completely altering the functionality of this.

By default, it uses the format.conf file in the ${XDG_CONFIG_HOME:-${HOME}/.config}/rifleman directory. -a is a shorthand; specifying -a lint looks for a file in the configuration directory called lint.conf

When this is first run, it will try to download the configuration files into the corresponding directories.

Example

$ rifleman *.md $(fd \.py$)
Running: /bin/sh -c set -- 'README.md'; prettier -w "$@"
README.md 88ms
Running: /bin/sh -c set -- 'rifleman/__init__.py' 'rifleman/__main__.py' 'setup.py' 'tests/test_rifleman.py'; black "$@"
All done!  🍰 4 files left unchanged.

Tests

git clone 'https://github.com/seanbreckenridge/rifleman'
cd ./rifleman
pip install '.[testing]'
mypy ./rifleman
pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rifleman-0.1.4.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rifleman-0.1.4-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file rifleman-0.1.4.tar.gz.

File metadata

  • Download URL: rifleman-0.1.4.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.6

File hashes

Hashes for rifleman-0.1.4.tar.gz
Algorithm Hash digest
SHA256 4a6b4423c1734057c2a95b33a596c26b9a36a604fc5cef28382e005cc3c17423
MD5 01029527935aaed703f51dd41f5aa9ef
BLAKE2b-256 c6297d1aadfb44e460c48b21c8f0ae27079e32335d3cb2d8bf00d99c624f96df

See more details on using hashes here.

File details

Details for the file rifleman-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: rifleman-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.6

File hashes

Hashes for rifleman-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bddb30c9749e14d2452205be751bd73d1fe4dba24baa8a6d56e09a1bf82ea37e
MD5 78e50be78b82953e1a87261664df6bb0
BLAKE2b-256 997d7ec96b53794c6369863272d2bcedfc25f1c9191a51ae6a266ea932335516

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page