Skip to content

Instantly share code, notes, and snippets.

@nirgeier
Created April 24, 2025 10:25
Show Gist options
  • Save nirgeier/16ba458d3f458bd70959cefdba1564cb to your computer and use it in GitHub Desktop.
Save nirgeier/16ba458d3f458bd70959cefdba1564cb to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to generate ConfigMap definitions for files in a specified directory
# Check if a directory path was provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <source_directory> <configmap_name>"
echo "Example: $0 ./config-files my-app-config"
exit 1
fi
# Assign arguments to variables
SOURCE_DIR=$1
CONFIGMAP_NAME=$2
# Check if the directory exists
if [ ! -d "$SOURCE_DIR" ]; then
echo "Error: Directory '$SOURCE_DIR' does not exist"
exit 1
fi
# Count files in the directory
FILE_COUNT=$(find "$SOURCE_DIR" -type f | wc -l)
if [ $FILE_COUNT -eq 0 ]; then
echo "Error: No files found in '$SOURCE_DIR'"
exit 1
fi
# Start creating the ConfigMap YAML file
echo "Creating ConfigMap for files in '$SOURCE_DIR'..."
# Generate ConfigMap header
cat > "${CONFIGMAP_NAME}.yaml" << EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: ${CONFIGMAP_NAME}
data:
EOF
# Loop through each file in the directory
find "$SOURCE_DIR" -type f | sort | while read -r file; do
filename=$(basename "$file")
echo " Processing file: $filename"
# Add the file content to the ConfigMap
echo " ${filename}: |" >> "${CONFIGMAP_NAME}.yaml"
# Add each line of the file with proper indentation (2 spaces for YAML)
while IFS= read -r line; do
echo " $line" >> "${CONFIGMAP_NAME}.yaml"
done < "$file"
# Add an empty line for readability
echo "" >> "${CONFIGMAP_NAME}.yaml"
done
echo "ConfigMap has been generated as '${CONFIGMAP_NAME}.yaml'"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment