media scwrypts v5 refactor
This commit is contained in:
14
scwrypts/media/cloud/cloud.module.zsh
Normal file
14
scwrypts/media/cloud/cloud.module.zsh
Normal file
@ -0,0 +1,14 @@
|
||||
#
|
||||
# cloud media synchronization tools
|
||||
#
|
||||
|
||||
|
||||
# synchronize cloud media with configured targets
|
||||
use cloud/synchronize --group media
|
||||
|
||||
# synchronize cloud media with a specific target
|
||||
use cloud/synchronize-target --group media
|
||||
|
||||
|
||||
# common parsers
|
||||
use cloud/zshparse --group media
|
58
scwrypts/media/cloud/synchronize
Executable file
58
scwrypts/media/cloud/synchronize
Executable file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env zsh
|
||||
#####################################################################
|
||||
|
||||
use cloud --group media
|
||||
|
||||
#####################################################################
|
||||
|
||||
media.cloud.zshparse.actions.usage
|
||||
|
||||
USAGE__options+='
|
||||
--target <string> local/remote target to synchronize (optional)
|
||||
'
|
||||
|
||||
USAGE__description='
|
||||
synchronize local media with an S3 bucket; *-synchronize actions
|
||||
will perform a pull/push back-to-back in the indicated order
|
||||
'
|
||||
|
||||
#####################################################################
|
||||
|
||||
MAIN() {
|
||||
local \
|
||||
TARGET \
|
||||
ACTION \
|
||||
PARSERS=(
|
||||
media.cloud.zshparse.actions
|
||||
)
|
||||
|
||||
eval "$ZSHPARSEARGS"
|
||||
|
||||
##########################################
|
||||
|
||||
case ${TARGET} in
|
||||
( '' )
|
||||
media.cloud.synchronize --action "${ACTION}"
|
||||
;;
|
||||
( * )
|
||||
media.cloud.synchronize-target --action "${ACTION}" --target "${TARGET}"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
MAIN.parse() {
|
||||
# local TARGET
|
||||
local PARSED=0
|
||||
|
||||
case $1 in
|
||||
( --target )
|
||||
PARSED=2
|
||||
TARGET="$2"
|
||||
;;
|
||||
esac
|
||||
|
||||
return $PARSED
|
||||
}
|
||||
|
88
scwrypts/media/cloud/synchronize-target.module.zsh
Normal file
88
scwrypts/media/cloud/synchronize-target.module.zsh
Normal file
@ -0,0 +1,88 @@
|
||||
#####################################################################
|
||||
|
||||
REQUIRED_ENV+=(MEDIA_SYNC__S3_BUCKET)
|
||||
|
||||
use cloud/aws
|
||||
use cloud/zshparse/actions --group media
|
||||
|
||||
#####################################################################
|
||||
|
||||
${scwryptsmodule}() {
|
||||
local \
|
||||
TARGET TARGET_CLOUD TARGET_LOCAL \
|
||||
ACTION \
|
||||
PARSERS=(
|
||||
media.cloud.zshparse.actions
|
||||
)
|
||||
|
||||
eval "$ZSHPARSEARGS"
|
||||
|
||||
##########################################
|
||||
|
||||
local A B
|
||||
case $ACTION in
|
||||
( push ) A="$TARGET_LOCAL"; B="$TARGET_CLOUD" ;;
|
||||
( pull ) A="$TARGET_CLOUD"; B="$TARGET_LOCAL" ;;
|
||||
|
||||
( pull-first-synchronize )
|
||||
: \
|
||||
&& media.cloud.synchronize-target \
|
||||
--target "${TARGET}" \
|
||||
--action pull \
|
||||
&& media.cloud.synchronize-target \
|
||||
--target "${TARGET}" \
|
||||
--action push \
|
||||
&& return 0 \
|
||||
|| return 1 \
|
||||
;
|
||||
;;
|
||||
|
||||
( push-first-synchronize )
|
||||
: \
|
||||
&& media.cloud.synchronize-target \
|
||||
--target "${TARGET}" \
|
||||
--action push \
|
||||
&& media.cloud.synchronize-target \
|
||||
--target "${TARGET}" \
|
||||
--action pull \
|
||||
&& return 0 \
|
||||
|| return 1 \
|
||||
;
|
||||
;;
|
||||
esac
|
||||
|
||||
echo.status "${ACTION}ing ${TARGET}"
|
||||
cloud.aws s3 sync $A $B \
|
||||
&& echo.success "${TARGET} up-to-date" \
|
||||
|| { ERROR "unable to sync ${TARGET} (see above)"; return 1; }
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
${scwryptsmodule}.parse() {
|
||||
# local TARGET TARGET_CLOUD TARGET_LOCAL
|
||||
local PARSED=0
|
||||
|
||||
case $1 in
|
||||
( --target )
|
||||
PARSED=2
|
||||
TARGET="$2"
|
||||
;;
|
||||
esac
|
||||
|
||||
return $PARSED
|
||||
}
|
||||
|
||||
${scwryptsmodule}.parse.usage() {
|
||||
USAGE__options+="
|
||||
--target <string> local/remote target to synchronize
|
||||
"
|
||||
}
|
||||
|
||||
${scwryptsmodule}.parse.validate() {
|
||||
[ "${TARGET}" ] \
|
||||
|| ERROR 'must specify a target'
|
||||
|
||||
TARGET_LOCAL="${HOME}/${TARGET}"
|
||||
TARGET_CLOUD="s3://${MEDIA_SYNC__S3_BUCKET}/${TARGET}"
|
||||
}
|
43
scwrypts/media/cloud/synchronize.module.zsh
Normal file
43
scwrypts/media/cloud/synchronize.module.zsh
Normal file
@ -0,0 +1,43 @@
|
||||
#####################################################################
|
||||
|
||||
REQUIRED_ENV+=(MEDIA_SYNC__TARGETS)
|
||||
|
||||
use cloud/synchronize-target --group media
|
||||
use cloud/zshparse/actions --group media
|
||||
|
||||
#####################################################################
|
||||
|
||||
${scwryptsmodule}() {
|
||||
eval "$(USAGE.reset)"
|
||||
|
||||
local \
|
||||
ACTION \
|
||||
PARSERS=(
|
||||
media.cloud.zshparse.actions
|
||||
)
|
||||
|
||||
eval "$ZSHPARSEARGS"
|
||||
|
||||
##########################################
|
||||
|
||||
local TARGET_COUNT=${#MEDIA_SYNC__TARGETS[@]}
|
||||
local FAILED_COUNT=0
|
||||
|
||||
echo.status "starting media ${ACTION}"
|
||||
|
||||
local TARGET
|
||||
for TARGET in ${MEDIA_SYNC__TARGETS[@]}
|
||||
do
|
||||
media.cloud.synchronize-target \
|
||||
--action "${ACTION}" \
|
||||
--target "${TARGET}" \
|
||||
|| ((FAILED_COUNT+=1))
|
||||
done
|
||||
|
||||
[[ $FAILED_COUNT -eq 0 ]] \
|
||||
&& echo.success "successfully completed ${ACTION} for ${TARGET_COUNT} / ${TARGET_COUNT} target(s)" \
|
||||
|| ERROR "failed ${ACTION} for ${FAILED_COUNT} / ${TARGET_COUNT} target(s)" \
|
||||
;
|
||||
}
|
||||
|
||||
#####################################################################
|
37
scwrypts/media/cloud/zshparse/actions.module.zsh
Normal file
37
scwrypts/media/cloud/zshparse/actions.module.zsh
Normal file
@ -0,0 +1,37 @@
|
||||
${scwryptsmodule}() {
|
||||
# local ACTION
|
||||
local PARSED=0
|
||||
|
||||
case $1 in
|
||||
( --action )
|
||||
PARSED=2
|
||||
ACTION="$2"
|
||||
;;
|
||||
esac
|
||||
|
||||
return $PARSED
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
${scwryptsmodule}.usage() {
|
||||
USAGE__options+="
|
||||
--action <string> a media sync action:
|
||||
push
|
||||
pull
|
||||
push-first-synchronize
|
||||
pull-first-synchronize
|
||||
"
|
||||
}
|
||||
|
||||
${scwryptsmodule}.validate() {
|
||||
case "${ACTION}" in
|
||||
( push | pull | pull-first-synchronize | push-first-synchronize ) ;;
|
||||
( '' )
|
||||
ERROR 'must specify a media sync action'
|
||||
;;
|
||||
( * )
|
||||
ERROR "invalid media sync action '${ACTION}'"
|
||||
;;
|
||||
esac
|
||||
}
|
5
scwrypts/media/cloud/zshparse/zshparse.module.zsh
Normal file
5
scwrypts/media/cloud/zshparse/zshparse.module.zsh
Normal file
@ -0,0 +1,5 @@
|
||||
#
|
||||
# common parsers for cloud media synchronization
|
||||
#
|
||||
|
||||
use cloud/zshparse/actions --group media
|
Reference in New Issue
Block a user