Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Fixing errors if no secrets repo exists when running apply #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions templates/starter-with-secrets/apps/aarch64-darwin/apply
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ insert_secrets_input() {
# Define file path
FILE_PATH="flake.nix"

# Check if the file exists
if [[ ! -f "$FILE_PATH" ]]; then
_print "${RED}File $FILE_PATH does not exist. Aborting insert_secrets_input...${NC}"
return
fi

# Backup the original file
cp "$FILE_PATH" "${FILE_PATH}.bak"

# Temporary file for the text to insert
TEMP_FILE="temp_insert.txt"

# Write the formatted text to the temporary file
cat > "$TEMP_FILE" << 'EOF'
cat > "$TEMP_FILE" << 'EOF'
secrets = {
url = "git+ssh://[email protected]/%GITHUB_USER%/%GITHUB_SECRETS_REPO%.git";
flake = false;
Expand All @@ -69,22 +75,34 @@ EOF

# Check if the 'secrets' block already exists
if grep -q 'url = "git+ssh://[email protected]/%GITHUB_USER%/%GITHUB_SECRETS_REPO%.git"' "$FILE_PATH"; then
echo "The 'secrets' block already exists in the file."
rm "$TEMP_FILE"
rm "${FILE_PATH}.bak"
exit 0
echo "The 'secrets' block already exists in the file."
rm "$TEMP_FILE"
rm "${FILE_PATH}.bak"
return
fi

# Find the start and end line numbers of the 'disko' block
START_LINE=$(grep -n 'disko = {' "$FILE_PATH" | head -n 1 | cut -d: -f1)
if [[ -z "$START_LINE" ]]; then
_print "${RED}Could not find 'disko = {' block. Aborting insert_secrets_input...${NC}"
rm "$TEMP_FILE"
return
fi

END_LINE=$(tail -n +$START_LINE "$FILE_PATH" | grep -n '};' | head -n 1 | cut -d: -f1)
if [[ -z "$END_LINE" ]]; then
_print "${RED}Could not find the end of 'disko' block. Aborting insert_secrets_input...${NC}"
rm "$TEMP_FILE"
return
fi

END_LINE=$((START_LINE + END_LINE - 1))

# Create a new file with the insertion
{
sed -n "1,${END_LINE}p" "$FILE_PATH"
cat "$TEMP_FILE"
sed -n "$((END_LINE + 1)),\$p" "$FILE_PATH"
sed -n "1,${END_LINE}p" "$FILE_PATH"
cat "$TEMP_FILE"
sed -n "$((END_LINE + 1)),\$p" "$FILE_PATH"
} > "${FILE_PATH}.new"

# Replace the original file with the new file
Expand All @@ -95,20 +113,6 @@ EOF
rm "${FILE_PATH}.bak"
}

ask_for_star() {
_print "${YELLOW}Would you like to support my work by starring my GitHub repo? yes/no [yes]: ${NC}"
local response
read -r response
response=${response:-yes} # Set default response to 'yes' if input is empty
if [[ "$response" =~ ^[Yy](es)?$ ]] || [[ -z "$response" ]]; then
if [[ "$OS" == "Darwin" ]]; then
open "https://github.com/dustinlyons/nixos-config"
else
xdg-open "https://github.com/dustinlyons/nixos-config"
fi
fi
}

ask_for_star

# Fetch username from the system
Expand Down Expand Up @@ -211,6 +215,11 @@ confirm_details
# Function to replace tokens in each file
replace_tokens() {
local file="$1"
if [[ ! -f "$file" ]]; then
_print "${RED}File $file does not exist. Skipping...${NC}"
return
fi

if [[ $(basename $1) != "apply" ]]; then
if [[ "$OS" == "Darwin" ]]; then
# macOS
Expand Down