Skip to content

Commit

Permalink
Add functions to process-files, add apps-home
Browse files Browse the repository at this point in the history
These new process-files functions will likely find their way into
go-script-bash as well.
  • Loading branch information
mbland committed Aug 24, 2017
1 parent 0b2edcf commit aca00dd
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 18 deletions.
19 changes: 3 additions & 16 deletions scripts/install.d/languages
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,10 @@
# RUBY_VERSION: Version of Ruby to install
# RBENV_VERSION: Tag or commit hash of rbenv version to install

_languages() {
local app_sys_root="$1"
local language

if [ ! -d $app_sys_root ]; then
mkdir -p $app_sys_root
fi
. "$_GO_USE_MODULES" 'apps-home'

if [ ! -d /etc/profile.d ]; then
mkdir /etc/profile.d
cat >>/etc/profile <<'END_PROFILE'
for script in /etc/profile.d/*.sh
do
. $script
done
END_PROFILE
fi
_languages() {
create_apps_home

@go install languages "go" "$GO_VERSION" "$GVM_VERSION"
@go install languages "node" "$NODEJS_VERSION" "$NVM_VERSION"
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.d/languages.d/go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# Will install gvm in `<app-sys-root>/gvm` and add `/etc/profile.d/gvm.sh`.

declare -r _GVM_PROFILE='/etc/profile.d/gvm.sh'
declare -r _GVM_PROFILE="$APPS_HOME/etc/profile.d/gvm.sh"
declare -r _GVM_INSTALLER_URL='https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer'

_install_gvm() {
Expand Down
28 changes: 28 additions & 0 deletions scripts/lib/apps-home
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env bash
#
# Utilities to manage the APPS_HOME directory structure
#
# Exports:
# create_apps_home
# Creates the APPS_HOME directory structure

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

export APPS_HOME_DIRS=(
"$APPS_HOME/etc/profile.d"
)
export APPS_HOME_DIR_PERMISSIONS="${APPS_HOME_DIR_PERMISSIONS:-755}"
export APPS_HOME_FILE_PERMISSIONS="${APPS_HOME_FILE_PERMISSIONS:-644}"

if [[ -z "$APPS_HOME" ]]; then
@go.log FATAL "APPS_HOME not defined; set it in settings.bash"
fi

# Creates the APPS_HOME directory structure
create_apps_home() {
local dir

for dir in "${APPS_HOME_DIRS[@]}"; do
create_dirs_with_permissions "$dir" "$APPS_HOME_DIR_PERMISSIONS"
done
}
79 changes: 79 additions & 0 deletions scripts/lib/process-files
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
#
# process_user_bin_scripts
# Perform an operation on all user-bin script paths
#
# process_path_forward
# Processes a file path from the first component to the last
#
# create_dir_with_permissions
# Creates a directory with the specified permissions
#
# create_dirs_with_permissions
# Creates a directory and its parents with the specified permissions

# Perform an operation on each path that is a regular file
#
Expand Down Expand Up @@ -48,3 +57,73 @@ process_user_bin_scripts() {
fi
process_files "$1" user-bin/{common,$PLATFORM_ID}/*
}

# Processes a file path from the first component to the last
#
# The first call to `operation` receives the first component of the path as its
# path argument. Each successive call to `operation` receives the previous path
# plus its child component. The processing is terminated when `operation`
# returns a nonzero value.
#
# Arguments:
# operation: Name of the function taking a file path as an argument
# ${@:1}: List of paths to begin examining
process_path_forward() {
local operation="$1"
local oldIFS="$IFS"
local IFS='/'
local components=($2)
local component
local current_path

IFS="$oldIFS"

for component in "${components[@]}"; do
current_path+="$component/"
if ! "$operation" "$current_path"; then
break
fi
done
}

# Creates a directory with the specified permissions
#
# If a directory already exists, this function does not update its permissions.
#
# Globals:
# permissions: May be defined as an alternative to passing as a parameter
#
# Arguments:
# dir: The path of the directory to create
# permissions: The permissions to set on the directory, if created
create_dir_with_permissions() {
local dir="$1"
local permissions="${2:-$permissions}"

if [[ -z "$dir" ]]; then
@go.log FATAL "Directory argument not specified"
elif [[ -z "$permissions" ]]; then
@go.log FATAL "Permissions not specified"
fi

if [[ ! -d "$dir" ]]; then
@go.log INFO "Creating $dir"

if ! mkdir "$dir"; then
@go.log FATAL "Could not create $dir"
elif ! chmod "$permissions" "$dir"; then
@go.log FATAL "Could not set permissions for $dir"
fi
fi
}

# Creates a directory and its parents with the specified permissions
#
# If a directory already exists, this function does not update its permissions.
#
# Arguments:
# 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"
}
2 changes: 1 addition & 1 deletion settings.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Root directory for language manager installs.
declare -r APPS_ROOT='/usr/local/mbland'
declare -r APPS_HOME='/usr/local/mbland'

# List of language for which to install language managers.
declare -r INSTALL_LANGUAGES=('go')
Expand Down

0 comments on commit aca00dd

Please sign in to comment.