Split prerequisites check into three stages
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
#!/usr/bin/env zsh
|
||||
# vi: ft=zsh
|
||||
|
||||
function checkPrerequisites() {
|
||||
local -A cmds=(
|
||||
[docopts]='(with -f option supported)'
|
||||
function getQuestionsPrerequisites() {
|
||||
cmds=(
|
||||
[systemsetup]=''
|
||||
)
|
||||
requireRootPrivileges
|
||||
}
|
||||
|
||||
function getExecPrerequisites() {
|
||||
cmds=(
|
||||
[osascript]=''
|
||||
[scutil]=''
|
||||
[systemsetup]=''
|
||||
@@ -12,8 +18,6 @@ function checkPrerequisites() {
|
||||
[defaults]=''
|
||||
[launchctl]=''
|
||||
)
|
||||
test "`id -u`" -eq 0 || { lop -- -e 'This module requires root access. Please run as root.'; return 11 }
|
||||
checkCommands
|
||||
}
|
||||
|
||||
function getQuestions() {
|
||||
|
||||
@@ -187,20 +187,30 @@ function configure_system() {
|
||||
allowOnlyFileVaultUserToUnlock "${filevault_username}"
|
||||
}
|
||||
|
||||
function checkPrerequisites() {
|
||||
local -A cmds=(
|
||||
[docopts]='(with -f option supported)'
|
||||
function getHelpPrerequisites() {
|
||||
cmds=(
|
||||
[tr]=''
|
||||
[cut]=''
|
||||
[scutil]=''
|
||||
)
|
||||
addDocoptsToCmds
|
||||
}
|
||||
|
||||
function getQuestionsPrerequisites() {
|
||||
cmds=(
|
||||
[find]=''
|
||||
[dscl]=''
|
||||
[sysadminctl]=''
|
||||
)
|
||||
}
|
||||
|
||||
function getExecPrerequisites() {
|
||||
cmds=(
|
||||
[cut]=''
|
||||
[fdesetup]=''
|
||||
[base64]=''
|
||||
[dsimport]=''
|
||||
[sysadminctl]=''
|
||||
[scutil]=''
|
||||
)
|
||||
test "`id -u`" -eq 0 || { lop -- -e 'This module requires root access. Please run as root.'; return 11 }
|
||||
checkCommands
|
||||
requireRootPrivileges
|
||||
}
|
||||
|
||||
function getQuestions() {
|
||||
|
||||
@@ -246,9 +246,8 @@ function configure_system() {
|
||||
popd -q
|
||||
}
|
||||
|
||||
function checkPrerequisites() {
|
||||
local -A cmds=(
|
||||
[docopts]='(with -f option supported)'
|
||||
function getExecPrerequisites() {
|
||||
cmds=(
|
||||
[dscl]=''
|
||||
[dseditgroup]=''
|
||||
[chown]=''
|
||||
@@ -259,8 +258,7 @@ function checkPrerequisites() {
|
||||
[awk]=''
|
||||
[sysadminctl]=''
|
||||
)
|
||||
test "`id -u`" -eq 0 || { lop -- -e 'This module requires root access. Please run as root.'; return 11 }
|
||||
checkCommands
|
||||
requireRootPrivileges
|
||||
}
|
||||
|
||||
function getDefaultHomebrewUsername() {
|
||||
|
||||
@@ -94,14 +94,11 @@ function configure_system() {
|
||||
popd -q
|
||||
}
|
||||
|
||||
function checkPrerequisites() {
|
||||
local -A cmds=(
|
||||
[docopts]='(with -f option supported)'
|
||||
)
|
||||
local -A execCmds=(
|
||||
function getExecPrerequisites() {
|
||||
cmds=(
|
||||
[brew]=''
|
||||
)
|
||||
test "`id -u`" -eq 0 || { lop -- -e 'This module requires root access. Please run as root.'; return 11 }
|
||||
id -nG | grep admin >&! /dev/null || { lop -- -e 'This module requires the user to be in admin group. Please run again as either root or an admin user.'; return 11 }
|
||||
checkCommands
|
||||
}
|
||||
|
||||
|
||||
@@ -29,17 +29,46 @@ function getModuleAnswerByKeyRegEx() {
|
||||
|
||||
function checkCommands() {
|
||||
local cmd
|
||||
local varName=$1
|
||||
for cmd in ${(Pk)varName}; do
|
||||
for cmd in ${(k)cmds}; do
|
||||
if ! which "${cmd}" >&! /dev/null; then
|
||||
local comment=''
|
||||
[ -n "${${(P)varName}[$cmd]}" ] && comment=" ${${(P)varName}[$cmd]}"
|
||||
[ -n "${cmds[$cmd]}" ] && comment=" ${cmds[$cmd]}"
|
||||
lop -- -e "This module needs ${cmd}${comment} to work."
|
||||
return 11
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
function checkHelpPrerequisites() {
|
||||
local -A cmds
|
||||
getHelpPrerequisites || return
|
||||
checkCommands
|
||||
}
|
||||
|
||||
function addDocoptsToCmds() {
|
||||
cmds+=(docopts '(with -f option supported)')
|
||||
}
|
||||
|
||||
function requireRootPrivileges() {
|
||||
test "`id -u`" -eq 0 || { lop -- -e 'This module requires root access. Please run as root.'; return 11 }
|
||||
}
|
||||
|
||||
whence getHelpPrerequisites >&! /dev/null || function $_() {
|
||||
addDocoptsToCmds
|
||||
}
|
||||
|
||||
function checkQuestionsPrerequisites() {
|
||||
local -A cmds
|
||||
getQuestionsPrerequisites || return
|
||||
checkCommands
|
||||
}
|
||||
|
||||
function checkExecPrerequisites() {
|
||||
local -A cmds
|
||||
getExecPrerequisites || return
|
||||
checkCommands
|
||||
}
|
||||
|
||||
function showQuestions() {
|
||||
local questions=()
|
||||
getQuestions
|
||||
@@ -52,11 +81,18 @@ function module_main() {
|
||||
local cmdName=${1:t}
|
||||
shift
|
||||
autoloadZShLib || return
|
||||
checkPrerequisites cmds || return
|
||||
checkHelpPrerequisites || return
|
||||
configureLogging
|
||||
eval "`getUsage $cmdName | docopts -f -V - -h - : "$@"`"
|
||||
[ $# -lt 1 ] && return
|
||||
checkQuestionsPrerequisites || return
|
||||
[ "${show_questions}" = true ] && { showQuestions; return }
|
||||
checkPrerequisites execCmds || return
|
||||
checkExecPrerequisites || return
|
||||
configure_system
|
||||
}
|
||||
|
||||
function {
|
||||
local name
|
||||
for name in getQuestionsPrerequisites getExecPrerequisites getQuestions getUsage; do
|
||||
whence ${name} >&! /dev/null || function $_() {}
|
||||
done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user