Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ jobs:
contents: read
security-events: write
uses: fabasoad/reusable-workflows/.github/workflows/wf-security-sast.yml@main
with:
code-scanning: true
sca: true
1 change: 1 addition & 0 deletions .github/workflows/update-license.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: License

on: # yamllint disable-line rule:truthy
schedule:
# Every January 1st at 14:00 JST
- cron: "0 5 1 1 *"

jobs:
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
hooks:
- id: build
name: Build
entry: make build
entry: make install
language: system
pass_filenames: false
verbose: true
Expand All @@ -34,11 +34,11 @@ repos:
hooks:
- id: detect-secrets
- repo: https://github.com/gitleaks/gitleaks
rev: v8.22.1
rev: v8.23.2
hooks:
- id: gitleaks
- repo: https://github.com/fabasoad/pre-commit-grype
rev: v0.6.1
rev: v0.6.2
hooks:
- id: grype-dir
args:
Expand All @@ -47,7 +47,7 @@ repos:
stages: ["pre-push"]
# Markdown
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.43.0
rev: v0.44.0
hooks:
- id: markdownlint-fix
stages: ["pre-commit"]
Expand All @@ -66,7 +66,7 @@ repos:
stages: ["pre-push"]
# GitHub Actions
- repo: https://github.com/rhysd/actionlint
rev: v1.7.6
rev: v1.7.7
hooks:
- id: actionlint
args: ["-pyflakes="]
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: build clean test
.PHONY: install clean test

.DEFAULT_GOAL := build
.DEFAULT_GOAL := install

build: clean
@./scripts/build.sh
install: clean
@./scripts/install.sh

clean:
@./scripts/clean.sh
Expand Down
46 changes: 27 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@

## Table of Contents

- [How it works?](#how-it-works)
- [Prerequisites](#prerequisites)
- [Hooks](#hooks)
- [snyk-code](#snyk-code)
- [snyk-container](#snyk-container)
- [snyk-iac](#snyk-iac)
- [snyk-log4shell](#snyk-log4shell)
- [snyk-test](#snyk-test)
- [Customization](#customization)
- [Description](#description)
- [Parameters](#parameters)
- [Snyk](#snyk)
- [pre-commit-snyk](#pre-commit-snyk)
- [Log level](#log-level)
- [Log color](#log-color)
- [Snyk version](#snyk-version)
- [Clean cache](#clean-cache)
- [Examples](#examples)
- [Snyk pre-commit hooks](#snyk-pre-commit-hooks)
- [Table of Contents](#table-of-contents)
- [How it works?](#how-it-works)
- [Prerequisites](#prerequisites)
- [Hooks](#hooks)
- [snyk-code](#snyk-code)
- [snyk-container](#snyk-container)
- [snyk-iac](#snyk-iac)
- [snyk-log4shell](#snyk-log4shell)
- [snyk-test](#snyk-test)
- [Customization](#customization)
- [Description](#description)
- [Parameters](#parameters)
- [Snyk](#snyk)
- [pre-commit-snyk](#pre-commit-snyk)
- [Log level](#log-level)
- [Log color](#log-color)
- [Snyk version](#snyk-version)
- [Clean cache](#clean-cache)
- [Examples](#examples)
- [Contributions](#contributions)

## How it works?

Expand All @@ -35,11 +38,12 @@ that will be removed after scanning is completed.

## Prerequisites

The following tools have to be available on a runner prior using this pre-commit
The following tools have to be available on a machine prior using this pre-commit
hook:

- [bash >=4.0](https://www.gnu.org/software/bash/)
- [curl](https://curl.se/)
- [jq](https://jqlang.github.io/jq/)

## Hooks

Expand Down Expand Up @@ -290,3 +294,7 @@ repos:
- --hook-args=--log-level debug
- --snyk-args=--detection-depth=1 --ignore-policy
```

## Contributions

![Alt](https://repobeats.axiom.co/api/embed/7ce583138987e93d1295be43056e647a1d2ede8e.svg "Repobeats analytics image")
2 changes: 1 addition & 1 deletion scripts/build.sh → scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ main() {
mkdir -p "${bpkg_packages_dir}"
lock_file="${ROOT_DIR}/.bpkg.lock"
while IFS= read -r line; do
PREFIX="${bpkg_packages_dir}" bpkg install "$line"
PREFIX="${bpkg_packages_dir}" bpkg install "${line}"
done < "${lock_file}"
}

Expand Down
134 changes: 99 additions & 35 deletions src/.bpkg/bin/fabasoad-log
Original file line number Diff line number Diff line change
@@ -1,17 +1,65 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

FABASOAD_LOG_CONFIG_OUTPUT_FORMAT_DEFAULT="text"
FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT="%Y-%m-%d %T"
FABASOAD_LOG_CONFIG_HEADER_DEFAULT="fabasoad-log"
FABASOAD_LOG_CONFIG_TEXT_COLOR_DEFAULT="true"
FABASOAD_LOG_CONFIG_TEXT_FORMAT_DEFAULT="[<header>] <time> level=<level> <message>"
FABASOAD_LOG_CONFIG_TEXT_FORMAT_DEFAULT="[<header>] <timestamp> level=<level> <message>"
FABASOAD_LOG_CONFIG_LOG_LEVEL_DEFAULT="info"

# Initialization

_get_from_config_or_default() {
jq -r --arg p "${1}" --arg d "${3}" 'try .config[$p] catch $d' "${2}"
}

_fabasoad_log_init() {
header="${FABASOAD_LOG_CONFIG_HEADER_DEFAULT}"
output_format="${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT_DEFAULT}"
text_format="${FABASOAD_LOG_CONFIG_TEXT_FORMAT_DEFAULT}"
date_format="${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}"
text_color="${FABASOAD_LOG_CONFIG_TEXT_COLOR_DEFAULT}"
log_level="${FABASOAD_LOG_CONFIG_LOG_LEVEL_DEFAULT}"

config_path="${1}"

if [ -f "${config_path}" ]; then
header=$(_get_from_config_or_default "header" "${config_path}" "${FABASOAD_LOG_CONFIG_HEADER_DEFAULT}")
tmp=$(_get_from_config_or_default "output-format" "${config_path}" "${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT_DEFAULT}")
case "${tmp}" in
json|xml|text)
output_format="${tmp}"
;;
esac
text_format=$(_get_from_config_or_default "text-format" "${config_path}" "${FABASOAD_LOG_CONFIG_TEXT_FORMAT_DEFAULT}")
date_format=$(_get_from_config_or_default "date-format" "${config_path}" "${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}")
tmp=$(_get_from_config_or_default "text-color" "${config_path}" "${FABASOAD_LOG_CONFIG_TEXT_COLOR_DEFAULT}")
case "${tmp}" in
true|false)
text_color="${tmp}"
;;
esac
tmp=$(_get_from_config_or_default "log-level" "${config_path}" "${FABASOAD_LOG_CONFIG_LOG_LEVEL_DEFAULT}")
case "${tmp}" in
error|warning|info|debug|off)
log_level="${tmp}"
;;
esac
fi

export FABASOAD_LOG_CONFIG_HEADER="${header:-${FABASOAD_LOG_CONFIG_HEADER_DEFAULT}}"
export FABASOAD_LOG_CONFIG_OUTPUT_FORMAT="${output_format:-${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT_DEFAULT}}"
export FABASOAD_LOG_CONFIG_TEXT_FORMAT="${text_format:-${FABASOAD_LOG_CONFIG_TEXT_FORMAT_DEFAULT}}"
export FABASOAD_LOG_CONFIG_DATE_FORMAT="${date_format:-${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}}"
export FABASOAD_LOG_CONFIG_TEXT_COLOR="${text_color:-${FABASOAD_LOG_CONFIG_TEXT_COLOR_DEFAULT}}"
export FABASOAD_LOG_CONFIG_LOG_LEVEL="${log_level:-${FABASOAD_LOG_CONFIG_LOG_LEVEL_DEFAULT}}"
}

# Text modifications

_fabasoad_wrap_text_with_color() {
log_line="$1"
level="$2"
log_line="${1}"
level="${2}"
if [ "${FABASOAD_LOG_CONFIG_TEXT_COLOR:-${FABASOAD_LOG_CONFIG_TEXT_COLOR_DEFAULT}}" = "true" ]; then
if [ "${level}" = "error" ]; then
printf "\033[91m${log_line}\033[0m"
Expand All @@ -29,9 +77,9 @@ _fabasoad_wrap_text_with_color() {

_fabasoad_wrap_text_with_bold() {
if [ "${FABASOAD_LOG_CONFIG_TEXT_COLOR:-${FABASOAD_LOG_CONFIG_TEXT_COLOR_DEFAULT}}" = "true" ]; then
echo "\033[1m$1\033[22m"
echo "\033[1m${1}\033[22m"
else
echo "$1"
echo "${1}"
fi
}

Expand Down Expand Up @@ -76,8 +124,8 @@ _fabasoad_is_printing_debug_ok() {
# Printing

_fabasoad_print_log() {
level="$1"
message="$2"
level="${1}"
message="${2}"

if [ "${level}" = "error" ] && [ "$(_fabasoad_is_printing_error_ok)" = "true" ]; then # error
printf "${message}\n" >&2
Expand All @@ -91,12 +139,12 @@ _fabasoad_print_log() {
}

_fabasoad_log_text() {
level="$1"
message="$2"
level="${1}"
message="${2}"

text_msg="${FABASOAD_LOG_CONFIG_TEXT_FORMAT:-${FABASOAD_LOG_CONFIG_TEXT_FORMAT_DEFAULT}}"
text_msg=${text_msg/<header>/${FABASOAD_LOG_CONFIG_HEADER:-${FABASOAD_LOG_CONFIG_HEADER_DEFAULT}}}
text_msg=${text_msg/<time>/$(date +"${FABASOAD_LOG_CONFIG_DATE_FORMAT:-${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}}")}
text_msg=${text_msg/<timestamp>/$(date +"${FABASOAD_LOG_CONFIG_DATE_FORMAT:-${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}}")}
text_msg=${text_msg/<level>/$(_fabasoad_wrap_text_with_bold "${level}")}
text_msg=${text_msg/<message>/${message}}

Expand All @@ -105,22 +153,27 @@ _fabasoad_log_text() {
}

_fabasoad_log_json() {
level="$1"
message="$2"

json_msg="{"
json_msg="${json_msg}\"timestamp\":\"$(date +"${FABASOAD_LOG_CONFIG_DATE_FORMAT:-${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}}")\","
json_msg="${json_msg}\"header\":\"${FABASOAD_LOG_CONFIG_HEADER:-${FABASOAD_LOG_CONFIG_HEADER_DEFAULT}}\","
json_msg="${json_msg}\"level\":\"${level}\","
json_msg="${json_msg}\"message\":\"${message}\""
json_msg="${json_msg}}"
timestamp="$(date +"${FABASOAD_LOG_CONFIG_DATE_FORMAT:-${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}}")"
header="${FABASOAD_LOG_CONFIG_HEADER:-${FABASOAD_LOG_CONFIG_HEADER_DEFAULT}}"

json_msg=$(jq -cn \
--arg timestamp "${timestamp}" \
--arg header "${header}" \
--arg level "${1}" \
--arg message "${2}" \
'{
timestamp: $timestamp,
header: $header,
level: $level,
message: $message
}')

_fabasoad_print_log "${level}" "${json_msg}"
}

_fabasoad_log_xml() {
level="$1"
message="$2"
level="${1}"
message="${2}"

xml_msg="<log>"
xml_msg="${xml_msg}<timestamp>$(date +"${FABASOAD_LOG_CONFIG_DATE_FORMAT:-${FABASOAD_LOG_CONFIG_DATE_FORMAT_DEFAULT}}")</timestamp>"
Expand All @@ -134,30 +187,41 @@ _fabasoad_log_xml() {

# Public functions

# Prints log line.
# Parameters:
# 1. (Required) Log level. Possible values: debug, info, warning, error, off.
# 2. (Required) Log message.
# 2. (Optional) Path to the config file.
#
# Usage example:
# fabasoad_log "error" "This is error message"
# fabasoad_log "debug" "This is debug message" "./config.json"
fabasoad_log() {
level="${1}"
message="${2}"
config_path="${3:-""}"

if [ -f "${config_path}" ]; then
log_init_lib="$(dirname $(realpath "${0}"))/fabasoad-log-init.sh"
if [ -f "${log_init_lib}" ]; then
. "${log_init_lib}"
fabasoad_log_init "${config_path}"
fi
_fabasoad_log_init "${config_path}"
fi

if [ "${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT:-${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT_DEFAULT}}" = "xml" ]; then
_fabasoad_log_xml "${level}" "${message}"
elif [ "${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT:-${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT_DEFAULT}}" = "json" ]; then
_fabasoad_log_json "${level}" "${message}"
else
_fabasoad_log_text "${level}" "${message}"
fi
case "${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT:-${FABASOAD_LOG_CONFIG_OUTPUT_FORMAT_DEFAULT}}" in
"xml")
log_func="_fabasoad_log_xml"
;;
"json")
log_func="_fabasoad_log_json"
;;
*)
log_func="_fabasoad_log_text"
;;
esac

${log_func} "${level}" "${message}"
}

# export
if [ ${BASH_SOURCE[0]} != $0 ]; then
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
export -f fabasoad_log
else
fabasoad_log "${@}"
Expand Down
Loading