Skip to content

Commit

Permalink
Rename lib/process-files to lib/walk
Browse files Browse the repository at this point in the history
This is in anticipation of moving some of these functions to
go-script-bash, after realizing that `walk` is a standard (and shorter!)
term for processing file and directory paths and structures.
  • Loading branch information
mbland committed Aug 24, 2017
1 parent aca00dd commit 91e015e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 35 deletions.
6 changes: 3 additions & 3 deletions scripts/diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

export DIFF_EDITOR="${DIFF_EDITOR:-vimdiff}"

. "$_GO_USE_MODULES" 'log' 'process-files'
. "$_GO_USE_MODULES" 'log' 'walk'

_diff_edit_if_different() {
if [[ ! -f "$1" ]]; then
Expand All @@ -26,8 +26,8 @@ _diff_user_bin_script() {
}

_diff() {
process_dotfiles _diff_dotfile
process_user_bin_scripts _diff_user_bin_script
walk_dotfiles _diff_dotfile
walk_user_bin_scripts _diff_user_bin_script
}

_diff "$@"
5 changes: 2 additions & 3 deletions scripts/install.d/dotfiles
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Installs dotfiles in the user's `HOME` directory

. "$_GO_USE_MODULES" 'copy'
. "$_GO_USE_MODULES" 'copy' 'walk'

_dotfiles_add_source_bash_dev() {
local candidates=(
Expand Down Expand Up @@ -35,8 +35,7 @@ _dotfiles() {
local dotfile

@go.log INFO Copying dotfiles into "$HOME"
. "$_GO_USE_MODULES" 'process-files'
process_dotfiles _dotfiles_copy_to_user_home_dir
walk_dotfiles _dotfiles_copy_to_user_home_dir
_dotfiles_add_source_bash_dev
}

Expand Down
5 changes: 2 additions & 3 deletions scripts/install.d/user-bin
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# These may be common or platform-specific scripts.

. "$_GO_USE_MODULES" 'platform' 'copy'
. "$_GO_USE_MODULES" 'platform' 'copy' 'walk'

install_user_bin_scripts_do_copy() {
copy_file_safely "$1" "${1%/*}/" "$HOME/bin" '700' '700'
Expand All @@ -14,8 +14,7 @@ install_user_bin_scripts() {
local script

@go.log INFO Copying user-bin scripts into "$HOME/bin"
. "$_GO_USE_MODULES" 'process-files'
process_user_bin_scripts install_user_bin_scripts_do_copy
walk_user_bin_scripts install_user_bin_scripts_do_copy
}

install_user_bin_scripts "$@"
2 changes: 1 addition & 1 deletion scripts/lib/apps-home
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# create_apps_home
# Creates the APPS_HOME directory structure

. "$_GO_USE_MODULES" 'log' 'process-files'
. "$_GO_USE_MODULES" 'log' 'walk'

export APPS_HOME_DIRS=(
"$APPS_HOME/etc/profile.d"
Expand Down
16 changes: 3 additions & 13 deletions scripts/lib/copy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# copy_file_safely
# Copy a file, creating directories if need be, only if it doesn't exist

. "$_GO_USE_MODULES" 'walk'

# Copy a file, creating directories if need be, only if it doesn't exist
#
# Arguments:
Expand All @@ -23,20 +25,8 @@ copy_file_safely() {
local dest="$dest_dir/${src#$src_prefix}"
local dest_parent="${dest_dir%/*}"

dest_dir="${dest%/*}"
printf '%s => %s\n' "$src" "$dest"

if [[ ! -d "$dest_dir" ]]; then
if ! mkdir -p "$dest_dir"; then
@go.log FATAL Failed to create "$dest_dir"
fi
while [[ "$dest_dir" != "$dest_parent" ]]; do
if ! chmod "$dir_perms" "$dest_dir"; then
@go.log FATAL Failed to set permissions on "$dest_dir"
fi
dest_dir="${dest_dir%/*}"
done
fi
create_dirs_with_permissions "$dest_dir" "$dir_perms"

if [[ ! -f "$dest" ]]; then
if ! cp "$src" "$dest"; then
Expand Down
24 changes: 12 additions & 12 deletions scripts/lib/process-files → scripts/lib/walk
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
# Functions for processing lists of dev setup files
#
# Exports:
# process_files
# walk_files
# Perform an operation on each path that is a regular file
#
# process_dotfiles
# walk_dotfiles
# Perform an operation on each dotfile path
#
# process_user_bin_scripts
# walk_user_bin_scripts
# Perform an operation on all user-bin script paths
#
# process_path_forward
# walk_path_forward
# Processes a file path from the first component to the last
#
# create_dir_with_permissions
Expand All @@ -28,13 +28,13 @@
# Arguments:
# operation: Name of the function taking a file path as an argument
# ${@:1}: List of paths to begin examining
process_files() {
walk_files() {
local f
for f in "${@:1}"; do
if [[ -f "$f" ]] && ! "$1" "$f"; then
return
elif [[ -d "$f" ]]; then
process_files "$1" "$f"/*
walk_files "$1" "$f"/*
fi
done
}
Expand All @@ -43,19 +43,19 @@ process_files() {
#
# Arguments:
# operation: Function taking a dotfile path as an argument
process_dotfiles() {
process_files "$1" dotfiles/.[A-Za-z0-9_-]*
walk_dotfiles() {
walk_files "$1" dotfiles/.[A-Za-z0-9_-]*
}

# Perform an operation on all user-bin script paths
#
# Arguments:
# operation: Function taking a user-bin script path as an argument
process_user_bin_scripts() {
walk_user_bin_scripts() {
if [[ -z "$PLATFORM_ID" ]]; then
. "$_GO_USE_MODULES" 'platform'
fi
process_files "$1" user-bin/{common,$PLATFORM_ID}/*
walk_files "$1" user-bin/{common,$PLATFORM_ID}/*
}

# Processes a file path from the first component to the last
Expand All @@ -68,7 +68,7 @@ process_user_bin_scripts() {
# Arguments:
# operation: Name of the function taking a file path as an argument
# ${@:1}: List of paths to begin examining
process_path_forward() {
walk_path_forward() {
local operation="$1"
local oldIFS="$IFS"
local IFS='/'
Expand Down Expand Up @@ -125,5 +125,5 @@ create_dir_with_permissions() {
# dir: The path of the directory to create
# permissions: The permissions to set on any created directory
create_dirs_with_permissions() {
permissions="$2" process_path_forward create_dir_with_permissions "$1"
permissions="$2" walk_path_forward create_dir_with_permissions "$1"
}

0 comments on commit 91e015e

Please sign in to comment.