mirror of
https://github.com/ivuorinen/cheatsheet-tldr.git
synced 2026-01-26 03:24:04 +00:00
feat: Rewrote run.sh and consolidated files
This commit is contained in:
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# EditorConfig is awesome: https://EditorConfig.org
|
||||||
|
|
||||||
|
# top-most EditorConfig file
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
217
.github/run.sh
vendored
217
.github/run.sh
vendored
@@ -6,67 +6,192 @@
|
|||||||
# TLDR pages licensed under CC-BY-4.0
|
# TLDR pages licensed under CC-BY-4.0
|
||||||
# https://github.com/tldr-pages/tldr/blob/main/LICENSE.md
|
# https://github.com/tldr-pages/tldr/blob/main/LICENSE.md
|
||||||
|
|
||||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
# To run debug mode, run script like this: DEGUG=1 ./run.sh
|
||||||
|
DEBUG="${DEBUG:-0}"
|
||||||
|
# To run in verbose mode, run script like this: VERBOSE=1 ./run.sh
|
||||||
|
VERBOSE="${VERBOSE:-0}"
|
||||||
|
|
||||||
echo "Running: $DIR/run.sh"
|
# Function to print an error message and exit
|
||||||
echo "Working directory: $DIR"
|
# $1 - error message (string)
|
||||||
|
error_exit() {
|
||||||
|
echo "Error: $1" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
source "$DIR/shared.sh"
|
# Print an error message
|
||||||
|
# $1 - error message (string)
|
||||||
|
msg_err() {
|
||||||
|
echo "Error: $1" >&2
|
||||||
|
}
|
||||||
|
|
||||||
TLDR_GIT="https://github.com/tldr-pages/tldr.git"
|
# Print a warning message
|
||||||
TLDR_SOURCE="source: $TLDR_GIT"
|
# $1 - warning message (string)
|
||||||
TLDR_SYNTAX="syntax: markdown"
|
msg_warn() {
|
||||||
TLDR_TEMP_DIR="$(realpath "$DIR"/..)/__tldr-temp"
|
echo "Warning: $1" >&2
|
||||||
TLDR_CHEAT_DEST="$(realpath "$DIR"/../tldr)"
|
}
|
||||||
|
|
||||||
if [ -d "$TLDR_TEMP_DIR" ]; then
|
# Print an ok message
|
||||||
rm -rf "$TLDR_TEMP_DIR"
|
# $1 - ok message (string)
|
||||||
fi
|
msg_ok() {
|
||||||
|
echo "OK: $1" >&2
|
||||||
|
}
|
||||||
|
|
||||||
echo "TLDR_TEMP_DIR: $TLDR_TEMP_DIR"
|
# Debug message
|
||||||
echo "TLDR_CHEAT_DEST: $TLDR_CHEAT_DEST"
|
# $1 - debug message (string)
|
||||||
|
msg_debug() {
|
||||||
|
if [[ $DEBUG -eq 1 ]]; then
|
||||||
|
echo "DEBUG: $1" >&2
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
git clone \
|
# Return sha256sum for a file
|
||||||
--depth 1 \
|
# $1 - filename (string)
|
||||||
--single-branch \
|
get_sha256sum() {
|
||||||
-q "$TLDR_GIT" "$TLDR_TEMP_DIR" || exit 1
|
[[ $DEBUG -eq 1 ]] && msg_debug "Getting sha256sum for: $1"
|
||||||
|
local file="$1"
|
||||||
|
sha256sum "$file" | awk '{print $1}'
|
||||||
|
}
|
||||||
|
|
||||||
rm -rf "$TLDR_TEMP_DIR/pages.*"
|
# Check if a file is replaceable
|
||||||
|
# $1 - source file (string)
|
||||||
|
# $2 - destination file (string)
|
||||||
|
# Returns 1 when replaceable, 0 when not replaceable.
|
||||||
|
is_replaceable() {
|
||||||
|
local file1="$1"
|
||||||
|
local file2="$2"
|
||||||
|
local file1_hash
|
||||||
|
local file2_hash
|
||||||
|
|
||||||
for d in "$TLDR_TEMP_DIR"/pages/*; do
|
[[ $DEGUG -eq 1 ]] && msg_debug "Checking if files are replaceable: $file1, $file2"
|
||||||
DIRNAME=$(basename "$d")
|
|
||||||
# echo "-> $DIRNAME ($d)"
|
|
||||||
|
|
||||||
SECTION_DIR="${TLDR_CHEAT_DEST}/$DIRNAME"
|
if [[ ! -r "$file1" ]]; then
|
||||||
|
[[ $VERBOSE -eq 1 ]] && msg_err "File 1 ($file1) does not exist"
|
||||||
[ "$DIRNAME" = "common" ] && SECTION_DIR="${TLDR_CHEAT_DEST}"
|
return 0
|
||||||
|
|
||||||
TLDR_TAGS="tags: [tldr, $DIRNAME]"
|
|
||||||
|
|
||||||
if [ ! -d "$SECTION_DIR" ]; then
|
|
||||||
mkdir -p "$SECTION_DIR"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for FILE in "$d"/*.md; do
|
if [[ ! -r "$file2" ]]; then
|
||||||
BASENAME=$(basename "$FILE" .md)
|
[[ $VERBOSE -eq 1 ]] && msg_warn "File 2 ($file2) does not exist, replaceable"
|
||||||
# FILENAME="${BASENAME%%.*}"
|
return 1
|
||||||
# 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
|
fi
|
||||||
|
|
||||||
replaceable "$FILE" "$TLDR_FILE"
|
file1_hash=$(get_sha256sum "$file1")
|
||||||
|
file2_hash=$(get_sha256sum "$file2")
|
||||||
|
|
||||||
|
if [[ -z "$file1_hash" ]]; then
|
||||||
|
[[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 1 ($file1)"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z "$file2_hash" ]]; then
|
||||||
|
[[ $VERBOSE -eq 1 ]] && msg_warn "Could not get hash for file 2 ($file2), replaceable"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$file1_hash" == "$file2_hash" ]]; then
|
||||||
|
[[ $VERBOSE -eq 1 ]] && msg_ok "Files match, not replaceable: $file1"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
[[ $VERBOSE -eq 1 ]] && msg_warn "Files do not match ($file1_hash != $file2_hash), replaceable"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to clone the TLDR repository
|
||||||
|
# $1 - repository URL (string)
|
||||||
|
clone_repo() {
|
||||||
|
[[ $DEBUG -eq 1 ]] && msg_debug "Cloning repository: $1"
|
||||||
|
local repo_url="$1"
|
||||||
|
local dest_dir="$2"
|
||||||
|
git clone --depth 1 --single-branch -q "$repo_url" "$dest_dir" || error_exit "Failed to clone repository"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to update the markdown files
|
||||||
|
# $1 - source directory (string)
|
||||||
|
# $2 - destination directory (string)
|
||||||
|
# $3 - syntax (string)
|
||||||
|
# $4 - source (string)
|
||||||
|
update_markdown_files() {
|
||||||
|
[[ $DEBUG -eq 1 ]] && msg_debug "Updating markdown files"
|
||||||
|
|
||||||
|
local src_dir="$1"
|
||||||
|
local dest_dir="$2"
|
||||||
|
local syntax="$3"
|
||||||
|
local source="$4"
|
||||||
|
|
||||||
|
for d in "$src_dir"/pages/*; do
|
||||||
|
[[ $VERBOSE -eq 1 ]] && msg_debug "Processing directory: $d"
|
||||||
|
|
||||||
|
local dirname
|
||||||
|
dirname=$(basename "$d")
|
||||||
|
local section_dir="${dest_dir}/${dirname}"
|
||||||
|
[ "$dirname" = "common" ] && section_dir="${dest_dir}"
|
||||||
|
|
||||||
|
local tldr_tags="tags: [tldr, $dirname]"
|
||||||
|
|
||||||
|
if [ ! -d "$section_dir" ]; then
|
||||||
|
mkdir -p "$section_dir" || error_exit "Failed to create directory: $section_dir"
|
||||||
|
fi
|
||||||
|
|
||||||
|
for file in "$d"/*.md; do
|
||||||
|
[[ $DEBUG -eq 1 ]] && msg_debug "Processing file: $file"
|
||||||
|
local basename
|
||||||
|
basename=$(basename "$file" .md)
|
||||||
|
local tldr_file="${section_dir}/${basename}"
|
||||||
|
|
||||||
|
if [ -f "$file" ] && [ '---' != "$(head -1 <"$file")" ]; then
|
||||||
|
echo -e "---\n$syntax\n$tldr_tags\n$source\n---\n$(cat "$file")" >"$file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
is_replaceable "$file" "$tldr_file"
|
||||||
|
local override=$?
|
||||||
|
|
||||||
override=$?
|
|
||||||
if [ "$override" -ne 0 ]; then
|
if [ "$override" -ne 0 ]; then
|
||||||
cp "$FILE" "$TLDR_FILE" && echo "Updated: $TLDR_FILE"
|
# Remove full path from the file and set as relative path for message
|
||||||
|
local tldr_file_rel
|
||||||
|
tldr_file_rel="${tldr_file#$dest_dir/}"
|
||||||
|
|
||||||
|
cp "$file" "$tldr_file" && echo "Updated: $tldr_file_rel" ||
|
||||||
|
error_exit "Failed to copy file: $file to $tldr_file"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main script logic
|
||||||
|
main() {
|
||||||
|
local dir
|
||||||
|
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ||
|
||||||
|
error_exit "Failed to determine current directory"
|
||||||
|
|
||||||
|
msg_debug "Running: $dir/run.sh"
|
||||||
|
msg_debug "Working directory: $dir"
|
||||||
|
|
||||||
|
local tldr_git="https://github.com/tldr-pages/tldr.git"
|
||||||
|
local tldr_source="source: $tldr_git"
|
||||||
|
local tldr_syntax="syntax: markdown"
|
||||||
|
local tldr_temp_dir
|
||||||
|
tldr_temp_dir="$(realpath "$dir"/..)/__tldr-temp"
|
||||||
|
local tldr_cheat_dest
|
||||||
|
tldr_cheat_dest="$(realpath "$dir"/../tldr)"
|
||||||
|
|
||||||
|
if [ -d "$tldr_temp_dir" ]; then
|
||||||
|
rm -rf "$tldr_temp_dir" ||
|
||||||
|
error_exit "Failed to remove directory: $tldr_temp_dir"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
msg_debug "TLDR_TEMP_DIR: $tldr_temp_dir"
|
||||||
done
|
msg_debug "TLDR_CHEAT_DEST: $tldr_cheat_dest"
|
||||||
|
|
||||||
rm -rf "$TLDR_TEMP_DIR"
|
clone_repo "$tldr_git" "$tldr_temp_dir"
|
||||||
|
|
||||||
|
rm -rf "$tldr_temp_dir/pages.*" ||
|
||||||
|
error_exit "Failed to remove translations (pages.*) from $tldr_temp_dir"
|
||||||
|
|
||||||
|
update_markdown_files "$tldr_temp_dir" "$tldr_cheat_dest" "$tldr_syntax" "$tldr_source"
|
||||||
|
|
||||||
|
rm -rf "$tldr_temp_dir" ||
|
||||||
|
error_exit "Failed to remove directory: $tldr_temp_dir"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run the main function
|
||||||
|
main "$@"
|
||||||
|
|||||||
50
.github/shared.sh
vendored
50
.github/shared.sh
vendored
@@ -1,50 +0,0 @@
|
|||||||
#!/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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user