irir

command module
v0.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 19 Imported by: 0

README

irir

irir CI irir report card

irir is a command line tool that provides a filter to add colors for text lines generically from a YAML configuration file easily.

Usage

This is a log file I want to add colors.

$ cat example.log
2023/05/19 23:56:54 [info] GET /some/resource 200 0.001
2023/05/19 23:56:55 [warn] GET /some/resource 200 0.001
2023/05/19 23:56:56 [error] GET /some/resource 200 0.001
2023/05/19 23:56:57 [info] GET /some/resource 200 0.001

then, below is a coloring rule file for log in YAML.

$ cat ~/.config/irir/irir_rule.yaml
---
log:
- type: match
  match: [info]
  color: cyan
  target: word
- type: match
  match: [warn]
  color: yellow
  target: word
- type: match
  match: [error]
  color: bg_red
  target: line

Then, you can filter the log file by irir with log rule.

$ cat example.log | irir log

colored log file

Yas!

Rule YAML

irir loads rules from YAML file. The rule file locates on your config directory of XDG Base Directory. File name should be irir_rule.yaml.

You can start editing irir_rule.yaml by a command irir --edit-config except on Windows.

You can see the location of irir_rule.yaml by a command irir --dump-config-path.

Here is the JSON Schema file to support writing irir_rule.yaml.

irir_rule.yaml

First key log is rule name that is specified in command line. You can name it as you like.

log:
- type: match
  match: [info]
  color: cyan
  target: word
- type: match
  match: [warn]
  color: yellow
  target: line
- type: match
  match: [error]
  color: bg_red
  target: line

Above rules have 3 ways to color as list.

Each rule to color
- type: match
  match: [info]
  color: cyan
  target: word
  • type: This specifies how to match. It should be match, prefix, suffix or regexp. If target value is word, then you can use only match or regexp.
  • match: This is a string or a regexp string to match.
  • color: specific color name. See the palette
  • target: This specifies a scope of coloring. It should be word or line.
The case of regexp type

If type is regexp, then there are special way to set regexp for coloring words.

Simple regexp case

The condition of regexp is Ba.. It will match 2 places with the line Foo Bar Baz.

$ cat example_file.txt
Foo Bar Baz

$ cat ~/.config/irir/irir_rule.yaml
re:
- type: regexp
  match: Ba.
  color: red
  target: word

Filter above example_file.txt like below:

$ cat example_file.txt | irir re

Output like this.

colored words by regexp

Special chars in YAML for regexp

As for YAML spec, if you write backslash \ in string value, then you should enclose string value by single-quote like below:

- type: regexp
  match: '\w+\.go'

If you enclose regexp with backslash by double-quote, then you should escape backslash by backslash:

- type: regexp
  match: "\\w+\\.go"

This is bit confusing. Single-quoted regexp is easier.

Another example

To add colors for go test result by gotest rule.

gotest:
- type: prefix
  match: "--- PASS"
  color: green
  target: line
- type: prefix
  match: "ok"
  color: green
  target: line
- type: prefix
  match: "PASS"
  color: green
  target: line
- type: prefix
  match: "--- FAIL"
  color: red
  target: line
- type: prefix
  match: "FAIL"
  color: red
  target: line
- type: prefix
  match: "--- SKIP"
  color: dark_yellow
  target: line
- type: match
  match: "=== RUN"
  color: gray
  target: line
- target: line
  type: match
  match: "=== CONT"
  color: gray
- type: match
  match: "=== PAUSE"
  color: gray
  target: line
- type: match
  match: "[no tests to run]"
  color: yellow
  target: word
- type: match
  match: "[no test files]"
  color: yellow
  target: word
- type: regexp
  match: '[^\/]+\.go:\d+'
  color: cyan
  target: word

This is also helpful on wrapped go test through make in a project.

$ make test | irir gotest

colored test result

Default rule

You can specify default rule by ENV:IRIR_DEFAULT_RULE. Then you can omit rule argument in command line.

export IRIR_DEFAULT_RULE=gotest
go test -v ./... | irir

Full options

irir --help

Usage: cat example.log | irir RULE_ID
Options:
      --dump-colors        Dump color palette
      --dump-config-path   Dump config file path
      --dump-rule          Dump specified rule
      --dump-rules         Show rules from config file
      --dump-schema        Dump JSON Schema to validate the rule YAML config file
      --edit-config        Invoke $EDITOR (or vi) to edit config YAML file
  -h, --help               Show help (This message) and exit
  -v, --version            Show version and build info and exit

Wrap Command Feature (Experimental)

NOTE: Don't execute a command from outside you don't handle. Just invoke only your own commands.

Below command line will color an output from some-command by irir rule.

$ iriri rule -- some-command

It's as same as below.

$ some-command | irir rule

If you often use irir, you can set alias with wrap command feature like below.

$ alias some-command="iriri rule -- some-command"

Then you can avoid writing | irir rule on each time.

TIPS: Color for Github Actions

Github Actions doesn't have TTY. If you want to use irir in Github Actions, Then you should add shell: 'script -q -e -c "bash {0}"' line like below. This is an example how to color outputs of go test in Github Actions.

- name: Install irir
  run: |
    go install github.com/bayashi/irir@latest
    mkdir -p $HOME/.config/irir
    curl -L https://raw.githubusercontent.com/bayashi/irir/1f6a6a0e4f20fba489eaa1047aea026dd0f056f3/configs/gotest.yaml > $HOME/.config/irir/irir_rule.yaml

- name: Run tests
  shell: 'script -q -e -c "bash {0}"'
  run: go test -v ./... | irir

Installation

homebrew install

If you are using Mac:

brew tap bayashi/tap
brew install bayashi/tap/irir
binary install

Download binary from here: https://github.com/bayashi/irir/releases

go install

If you have golang environment:

go install github.com/bayashi/irir@latest

License

MIT License

Author

Dai Okabayashi: https://github.com/bayashi

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL