Files
sh-goodies/rusty-checker.sh

71 lines
2.5 KiB
Bash
Executable File

#!/bin/sh
# Rusty checker: check if a FOSS project is written in Rust (by default)
# or another language of your selection
# Usage: rusty-checker.sh repo_url [lang]
# Currently only supports GitHub, GitLab and Codeberg
# More provider support may come if there's some public API
# Depends on grep, curl and jq
# Created by Luxferre in 2026, released into public domain
PROV=""
TGT=""
REPO="$1"
CHLANG=Rust
[ ! -z "$2" ] && CHLANG="$2"
echo "$REPO" | grep -qi "github.com" && PROV="gh"
echo "$REPO" | grep -qi "gitlab.com" && PROV="gl"
echo "$REPO" | grep -qi "codeberg.org" && PROV="cb"
# Remove trailing slash before processing
REPO="${REPO%/}"
temp="${REPO%.git}"
CORR_SCORE=0 # innocent until proven guilty
if [ "$PROV" = "gh" ]; then
TGT="${temp#*github.com/}"
ROOT_URL="https://api.github.com/repos/$TGT"
LANG_URL=""
elif [ "$PROV" = "gl" ]; then
TGT="${temp#*gitlab.com/}"
ROOT_URL="https://gitlab.com/api/v4/projects/${TGT/\//%2F}"
LANG_URL="${ROOT_URL}/languages"
elif [ "$PROV" = "cb" ]; then
TGT="${temp#*codeberg.org/}"
ROOT_URL="https://codeberg.org/api/v1/repos/$TGT"
LANG_URL=""
else
echo 'This provider is currently unsupported, sorry!'
exit 2
fi
repodata="$(curl -sSL $ROOT_URL)"
[ -z "$LANG_URL" ] && LANG_URL="$(echo $repodata | jq -r .languages_url)"
langdata="$(curl -sSL $LANG_URL)"
desc="$(echo $repodata | jq -r .description)"
topics=$(echo $repodata | jq -r .topics)
# mentioning the language in the description boosts the score a lot
echo "$desc" | grep -qi "${CHLANG}-" && CORR_SCORE=$((CORR_SCORE + 50))
echo "$desc" | grep -qi " ${CHLANG}" && CORR_SCORE=$((CORR_SCORE + 50))
# mentioning the language in the topics also boosts the score
echo "$topics" | grep -qi "$CHLANG" && CORR_SCORE=$((CORR_SCORE + 30))
# get the most popular language in the language list
mainlang="$(echo "$langdata" | jq -r 'to_entries | max_by(.value) | .key')"
# just using this language increases the score by 10
echo "$langdata" | grep -qi "$CHLANG" && CORR_SCORE=$((CORR_SCORE + 10))
# but if it is the main one, we further increase the score by 50
echo "$mainlang" | grep -qi "$CHLANG" && CORR_SCORE=$((CORR_SCORE + 50))
if ((CORR_SCORE >= 50)); then
echo "The project $TGT is most likely written in $CHLANG."
if ((CORR_SCORE >= 100)); then
echo 'The author is a definite fanatic of this language.'
fi
exit 0
else
echo "The project $TGT does not seem to be written in $CHLANG."
if ((CORR_SCORE > 0)); then
echo 'However, this language may be partially used there.'
fi
exit 1;
fi