/sync-files.bash Secret
Last active
June 7, 2024 14:10
This file contains 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
#!/bin/bash | |
set -e | |
touch /tmp/result.yaml | |
for repository in $(yq ".[].repository" /tmp/sync-files.yaml); do | |
yq ".[] | select(.repository == \"$repository\")" /tmp/sync-files.yaml > /tmp/repo-config.yaml | |
ref=$(yq ".ref" /tmp/repo-config.yaml) | |
git_options=() | |
if [ "$ref" != "" ]; then | |
git_options+=("-b $ref") | |
fi | |
rm -rf /tmp/repository | |
git clone --depth 1 "$repository" /tmp/repository ${git_options[@]} | |
for dest_file in $(yq ".files[].dest" /tmp/repo-config.yaml); do | |
yq ".files[] | select(.dest == \"$dest_file\")" /tmp/repo-config.yaml > /tmp/file-config.yaml | |
source_path=$(yq ".source" /tmp/file-config.yaml) | |
echo "source_path: $source_path" | |
dest_path=$(yq ".dest" /tmp/file-config.yaml) | |
echo "dest_path: $dest_path" | |
replace=$(yq ".replace" /tmp/file-config.yaml) | |
delete_orphaned=$(yq ".delete-orphaned" /tmp/file-config.yaml) | |
pre_commands=$(yq ".pre-commands" /tmp/file-config.yaml) | |
post_commands=$(yq ".post-commands" /tmp/file-config.yaml) | |
modified_source_path="/tmp/repository/$source_path" | |
pre_commands=$(echo "$pre_commands" | sed "s|{source}|$modified_source_path|g" | sed "s|{dest}|$dest_path|g") | |
post_commands=$(echo "$post_commands" | sed "s|{source}|$modified_source_path|g" | sed "s|{dest}|$dest_path|g") | |
[ -n "$pre_commands" ] && echo "pre_commands for $source_path: $pre_commands" | |
[ -n "$post_commands" ] && echo "post_commands for $source_path: $post_commands" | |
if [ -f "$modified_source_path" ]; then | |
if [ -f "$dest_path" ] && [ "$replace" != "true" ]; then | |
echo "Skip copying to $dest_path." | |
yq -i ".skipped += [\"$dest_path\"]" /tmp/result.yaml | |
continue | |
fi | |
eval "$pre_commands" | |
if ! [ -f "$dest_path" ]; then | |
echo "Newly copy $modified_source_path to $dest_path." | |
mkdir -p $(dirname "$dest_path") | |
cp "$modified_source_path" "$dest_path" | |
eval "$post_commands" | |
yq -i ".added += [\"$dest_path\"]" /tmp/result.yaml | |
elif ! diff "$modified_source_path" "$dest_path"; then | |
echo "Copy $modified_source_path to $dest_path." | |
cp "$modified_source_path" "$dest_path" | |
eval "$post_commands" | |
yq -i ".changed += [\"$dest_path\"]" /tmp/result.yaml | |
else | |
echo "$modified_source_path and $dest_path are the same." | |
yq -i ".not-changed += [\"$dest_path\"]" /tmp/result.yaml | |
fi | |
elif [ "$delete_orphaned" = "true" ]; then | |
if [ -f "$dest_path" ]; then | |
echo "Delete $dest_path." | |
rm "$dest_path" | |
yq -i ".deleted += [\"$dest_path\"]" /tmp/result.yaml | |
else | |
echo "$dest_path was not found." | |
yq -i ".not-found += [\"$dest_path\"]" /tmp/result.yaml | |
fi | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Old version: