Created
April 28, 2025 01:38
-
-
Save akhanf/df1376a2949dac476b054cc6b78dbd36 to your computer and use it in GitHub Desktop.
enhancement wrapper for osfcli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
shopt -s expand_aliases | |
alias osf='uvx --from osfclient osf' | |
CACHE_DIR=".osfcli.cache" | |
CACHE_FILE="${CACHE_DIR}/filelist.txt" | |
CACHE_TTL=3600 | |
echo $CACHE_FILE | |
get_project_id() { | |
if [[ ! -f .osfcli.config ]]; then | |
echo "[ERROR] No .osfcli file found. Run '$0 init' first." | |
exit 1 | |
fi | |
grep "^project =" .osfcli.config | awk '{print $3}' | |
} | |
get_cached_filelist() { | |
local force_refresh=$1 | |
local project | |
project=$(get_project_id) | |
mkdir -p "$CACHE_DIR" | |
if [[ "$force_refresh" == "true" || ! -f "$CACHE_FILE" || $(($(date +%s) - $(stat -c %Y "$CACHE_FILE"))) -gt $CACHE_TTL ]]; then | |
>&2 echo "[INFO] Refreshing file list for project $project..." | |
osf list > "$CACHE_FILE" | |
else | |
>&2 echo "[INFO] Using cached file list: $CACHE_FILE" | |
fi | |
echo "$CACHE_FILE" | |
} | |
subcommand_init() { | |
echo "[INFO] Initializing OSF project in this directory..." | |
osf init | |
echo "[INFO] Setting up local cache directory: $CACHE_DIR" | |
mkdir -p "$CACHE_DIR" | |
} | |
subcommand_ls() { | |
local refresh="false" | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--refresh) | |
refresh="true" | |
shift | |
;; | |
-*) | |
echo "Unknown ls option: $1" | |
exit 1 | |
;; | |
*) | |
echo "Unexpected argument: $1" | |
exit 1 | |
;; | |
esac | |
done | |
local filelist | |
filelist=$(get_cached_filelist "$refresh") | |
cat "$filelist" | |
} | |
subcommand_get() { | |
local dry_run=false | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--dry-run) | |
dry_run=true | |
shift | |
;; | |
-*) | |
echo "Unknown get option: $1" | |
exit 1 | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
if [[ $# -ne 1 ]]; then | |
echo "Usage: $0 get [--dry-run] GLOB_PATTERN" | |
exit 1 | |
fi | |
local pattern=$1 | |
local project | |
project=$(get_project_id) | |
local filelist | |
filelist=$(get_cached_filelist false) | |
matched_paths=$(python3 - "$pattern" "$filelist" <<EOF | |
import fnmatch | |
import sys | |
glob = sys.argv[1] | |
with open(sys.argv[2]) as f: | |
lines = [line.strip() for line in f if line.strip()] | |
matched = fnmatch.filter(lines, glob) | |
print("\n".join(matched)) | |
EOF | |
) | |
while IFS= read -r path; do | |
[ -z "$path" ] && continue | |
mkdir -p "$(dirname "$path")" | |
if $dry_run; then | |
echo "[DRY RUN] Would fetch: $project:$path -> $path" | |
else | |
echo "Fetching: $project:$path -> $path" | |
osf fetch "$project:$path" "$path" | |
fi | |
done <<< "$matched_paths" | |
} | |
# --- Main command dispatcher --- | |
if [[ $# -lt 1 ]]; then | |
echo "Usage: $0 <init|ls|get> [options]" | |
exit 1 | |
fi | |
cmd=$1 | |
shift | |
case "$cmd" in | |
init) subcommand_init "$@" ;; | |
ls) subcommand_ls "$@" ;; | |
get) subcommand_get "$@" ;; | |
*) | |
echo "Unknown command: $cmd" | |
exit 1 | |
;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment