From 4e88a1b42ff7b05308c743062584f0275b96ed29 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 21 Feb 2024 13:18:38 +0200 Subject: [PATCH] Initial commit --- .github/LICENSE.md | 21 +++++++++ .github/README.md | 36 +++++++++++++++ .github/renovate.json | 4 ++ .github/run.sh | 72 ++++++++++++++++++++++++++++++ .github/shared.sh | 50 +++++++++++++++++++++ .github/workflows/cheatsheets.yaml | 34 ++++++++++++++ .gitignore | 1 + 7 files changed, 218 insertions(+) create mode 100644 .github/LICENSE.md create mode 100644 .github/README.md create mode 100644 .github/renovate.json create mode 100755 .github/run.sh create mode 100755 .github/shared.sh create mode 100644 .github/workflows/cheatsheets.yaml create mode 100644 .gitignore diff --git a/.github/LICENSE.md b/.github/LICENSE.md new file mode 100644 index 00000000..bffa737e --- /dev/null +++ b/.github/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Ismo Vuorinen + +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. diff --git a/.github/README.md b/.github/README.md new file mode 100644 index 00000000..3e759970 --- /dev/null +++ b/.github/README.md @@ -0,0 +1,36 @@ +# ivuorinen/cheatsheet-tldr + +This repository is a collection of [cheat/cheat](https://github.com/cheat/cheat) +compatible cheatsheets derived from [tldr](https://github.com/tldr-pages/tldr) pages. + +## Installation + +Add the cheatsheets to your `cheat` installation by cloning this repository to +`~/.cheat/cheatsheets/tldr`: + +```sh +git clone https://github.com/ivuorinen/cheatsheet-tldr.git ~/.cheat/cheatsheets/tldr +``` + +Configure your `cheat.yaml` to include the cheatsheets: + +```yaml + - name: tldr + path: ~/.cheat/cheatsheets/tldr + tags: [tldr] + readonly: true +``` + +## Usage + +Use `cheat` as you normally would, but include the `tldr` tag to filter the +cheatsheets: + +```sh +cheat --tags tldr +``` + +## License + +Files under `.github/` are licensed under the [MIT License](LICENSE.md). +TLDR pages licensed under [CC-BY-4.0](https://github.com/tldr-pages/tldr/blob/main/LICENSE.md). diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 00000000..5b9cda39 --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,4 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": ["github>ivuorinen/.github:renovate-config"] +} diff --git a/.github/run.sh b/.github/run.sh new file mode 100755 index 00000000..e442de94 --- /dev/null +++ b/.github/run.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# Create cheat/cheat compatible versions of the english tldr pages +# Copyright (c) 2024 Ismo Vuorinen +# Script licensed under the MIT License +# https://github.com/ivuorinen/cheatsheet-tldr/blob/main/.github/LICENSE.md +# TLDR pages licensed under CC-BY-4.0 +# https://github.com/tldr-pages/tldr/blob/main/LICENSE.md + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +echo "Running: $DIR/run.sh" +echo "Working directory: $DIR" + +source "$DIR/shared.sh" + +TLDR_GIT="https://github.com/tldr-pages/tldr.git" +TLDR_SOURCE="source: $TLDR_GIT" +TLDR_SYNTAX="syntax: markdown" +TLDR_TEMP_DIR="$(realpath "$DIR"/..)/__tldr-temp" +TLDR_CHEAT_DEST="$(realpath "$DIR"/../)" + +if [ -d "$TLDR_TEMP_DIR" ]; then + rm -rf "$TLDR_TEMP_DIR" +fi + +echo "TLDR_TEMP_DIR: $TLDR_TEMP_DIR" +echo "TLDR_CHEAT_DEST: $TLDR_CHEAT_DEST" + +git clone \ + --depth 1 \ + --single-branch \ + -q "$TLDR_GIT" "$TLDR_TEMP_DIR" || exit 1 + +rm -rf "$TLDR_TEMP_DIR/pages.*" + +for d in "$TLDR_TEMP_DIR"/pages/*; do + DIRNAME=$(basename "$d") + # echo "-> $DIRNAME ($d)" + + SECTION_DIR="${TLDR_CHEAT_DEST}/$DIRNAME" + + [ "$DIRNAME" = "common" ] && SECTION_DIR="${TLDR_CHEAT_DEST}" + + TLDR_TAGS="tags: [tldr, $DIRNAME]" + + if [ ! -d "$SECTION_DIR" ]; then + mkdir -p "$SECTION_DIR" + fi + + for FILE in "$d"/*.md; do + BASENAME=$(basename "$FILE" .md) + # FILENAME="${BASENAME%%.*}" + # echo "-> $FILE = $FILENAME" + TLDR_FILE="$SECTION_DIR/${BASENAME}" + # echo "-> dest: $TLDR_FILE" + + # Update the original file for making the replaceable value comparable + if [ -f "$FILE" ] && [ '---' != "$(head -1 < "$FILE")" ]; then + echo -e "---\n$TLDR_SYNTAX\n$TLDR_TAGS\n$TLDR_SOURCE\n---\n$(cat "$FILE")" > "$FILE" + fi + + replaceable "$FILE" "$TLDR_FILE" + + override=$? + if [ "$override" -ne 0 ]; then + cp "$FILE" "$TLDR_FILE" && echo "Updated: $TLDR_FILE" + fi + + done +done + +rm -rf "$TLDR_TEMP_DIR" diff --git a/.github/shared.sh b/.github/shared.sh new file mode 100755 index 00000000..6f8cb276 --- /dev/null +++ b/.github/shared.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +# return sha256sum for file +# $1 - filename (string) +function get_sha256sum() +{ + sha256sum "$1" | head -c 64 +} + +# Replaceable file +# +# $1 - filename (string) +# $2 - filename (string) +# +# Returns 1 when replaceable, 0 when not replaceable. +function replaceable() +{ + FILE1="$1" + FILE2="$2" + + [[ ! -r "$FILE1" ]] && { + [[ $VERBOSE -eq 1 ]] && msg_err "File 1 ($FILE1) does not exist" + return 0 + } + [[ ! -r "$FILE2" ]] && { + [[ $VERBOSE -eq 1 ]] && msg_err "File 2 ($FILE2) does not exist, replaceable" + return 1 + } + + FILE1_HASH=$(get_sha256sum "$FILE1") + FILE2_HASH=$(get_sha256sum "$FILE2") + + [[ $FILE1_HASH = "" ]] && { + [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 1 ($FILE1)" + return 0 + } + [[ $FILE2_HASH = "" ]] && { + [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 2 ($FILE2), replaceable" + return 1 + } + + [[ "$FILE1_HASH" == "$FILE2_HASH" ]] && { + [[ $VERBOSE -eq 1 ]] && msg_ok "Files match, not replaceable: $FILE1" + return 0 + } + + [[ $VERBOSE -eq 1 ]] && msg_warn "Files do not match ($FILE1_HASH != $FILE2_HASH), replaceable" + + return 1 +} diff --git a/.github/workflows/cheatsheets.yaml b/.github/workflows/cheatsheets.yaml new file mode 100644 index 00000000..77bcded7 --- /dev/null +++ b/.github/workflows/cheatsheets.yaml @@ -0,0 +1,34 @@ +--- +on: + workflow_dispatch: + schedule: + - cron: '0 0 * * *' + push: + branches: [main] + pull_request: + branches: [main] + +permissions: + contents: write + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Setup git config + run: | + git config --global user.name "${{ github.actor }}" + git config --global user.email "${{ github.actor }}@users.noreply.github.com" + - name: Build + run: | + echo "Building cheatsheets" + bash .github/run.sh + echo "Done" + - name: Commit and push changes + run: | + git add . + git commit -m "Update cheatsheets" + git push diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f16ff29b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__tldr-temp