@shinyaz

Target specific sections with --force in dotfiles install.sh

1 min read

My dotfiles install.sh had a --force flag, but it overwrote everything — including files like AWS config where the repo holds a template and real values only exist locally. One careless --force and the actual credentials config gets replaced with a blank template.

The fix was adding a section name as a third argument to copy_file.

should_force() {
  local section="$1"
  [[ "$FORCE_ALL" == true ]] || [[ " ${FORCE_TARGETS[*]} " == *" $section "* ]]
}
 
copy_file() {
  local src="$1" dest="$2" section="${3:-}"
  if [[ -f "$dest" ]] && ! should_force "$section"; then
    warn "Skipped (already exists): $dest"
    return 1
  fi
  cp "$src" "$dest"
}
 
# Tag each call with its section name
copy_file "$DOTFILES/zsh/zshrc"  "$ZDOTDIR/.zshrc"            zsh || true
copy_file "$DOTFILES/aws/config" "$XDG_CONFIG_HOME/aws/config" aws || true

Now ./install.sh --force zsh git only overwrites zsh and git sections, leaving aws untouched. A bare --force with no arguments still overwrites everything. It's just one extra argument to copy_file, so no breaking changes.

Share this post

Shinya Tahara

Shinya Tahara

Solutions Architect @ AWS

I'm a Solutions Architect at AWS, providing technical guidance primarily to financial industry customers. I share learnings about cloud architecture and AI/ML on this site.The views and opinions expressed on this site are my own and do not represent the official positions of my employer.