diff --git a/scripts/sync_fb_cookbooks.sh b/scripts/sync_fb_cookbooks.sh index 2a53d619..7bc8124c 100755 --- a/scripts/sync_fb_cookbooks.sh +++ b/scripts/sync_fb_cookbooks.sh @@ -21,44 +21,50 @@ set -u -# constants -CONF="$HOME/.config/sync_fb_cookbooks.conf" - # defaults -upstreamdir="$HOME/src/chef-cookbooks" -internaldir="$HOME/src/chef" +config="$HOME/.config/sync_fb_cookbooks.conf" +default_upstreamdir="$HOME/src/chef-cookbooks" +default_internaldir="$HOME/src/chef" # methods error() { - echo "ERROR: $*" + echo "ERROR: $*" } warn() { - echo "WARNING: $*" + echo "WARNING: $*" +} + +info() { + echo "INFO: $*" } die() { - error "$@" - exit 1 + error "$@" + exit 1 } ourhelp() { - cat <] OPTIONS -c Compare just + -C + Path to config file + Default: $config + -d - Diff mode. + Diff mode -h - This. + This -i - Where your clone of the INTERNAL repo is. - Default: $internaldir + Where your clone of the INTERNAL repo is + Default: $default_internaldir -p Push changes internal -> upstream @@ -67,11 +73,11 @@ OPTIONS Sync changes upstream -> internal -u - Where your clone of the UPSTREAM repo is. - Default: $internaldir + Where your clone of the UPSTREAM repo is + Default: $default_internaldir CONFIG FILE -You can also tell this script where your repos are by creating $CONF +You can also tell this script where your repos are by creating $config in shell format and defining 'upstreamdir' and 'internaldir', like so: upstreamdir="\$HOME/mycheckouts/chef-cookbooks" @@ -94,75 +100,102 @@ GENERAL USAGE # OR.... push out internal changes $ $0 -c fb_sysfs -p - EOF } -# people put repos in different places, don't make them pass in -# -u and -i every time. -if [ -e "$CONF" ]; then - # shellcheck disable=SC1090 - source "$CONF" || die "Configuration file $CONF malformed" -fi - mode='' cookbook='' -while getopts c:dhi:psu: opt; do - case $opt in - c) - cookbook="$OPTARG" - ;; - d) - mode='diff' - ;; - h) - ourhelp - exit - ;; - i) - internaldir="$OPTARG" - ;; - p) - mode='push' - ;; - s) - mode='sync' - ;; - u) - upstreamdir="$OPTARG" - ;; - ?) - ourhelp - exit 1 - ;; - esac +while getopts 'c:C:dhi:psu:' opt; do + case $opt in + c) + cookbook="$OPTARG" + ;; + C) + config="$OPTARG" + ;; + d) + mode='diff' + ;; + h) + ourhelp + exit + ;; + i) + internaldir="$OPTARG" + ;; + p) + mode='push' + ;; + s) + mode='sync' + ;; + u) + upstreamdir="$OPTARG" + ;; + ?) + ourhelp + exit 1 + ;; + esac done +# save what was passed in either through env +# or command-line... +save_internaldir="" +save_upstreamdir="" +if [ -n "$internaldir" ]; then + save_internaldir="$internaldir" +fi +if [ -n "$upstreamdir" ]; then + save_upstreamdir="$upstreamdir" +fi + +# initialize with defaults +internaldir="$default_internaldir" +upstreamdir="$default_upstreamdir" + +# now read the config file, if we have one +if [ -e "$config" ]; then + # shellcheck disable=SC1090 + info "Loading config from $config" + source "$config" || die "Configuration file $config malformed" +fi + +# Now merge in passed-in config +if [ -n "$save_internaldir" ]; then + internaldir="$save_internaldir" +fi +if [ -n "$save_upstreamdir" ]; then + upstreamdir="$save_upstreamdir" +fi + +info "Using upstream: $upstreamdir | internal: $internaldir" + if [ -z "$mode" ]; then - if [ -n "$cookbook" ]; then - mode='diff' - else - mode='status' - fi + if [ -n "$cookbook" ]; then + mode='diff' + else + mode='status' + fi fi cd "$internaldir/cookbooks" || die "where am I?" [ -z "$cookbook" ] && cookbook="$(ls -d fb_*)" for cb in $cookbook; do - ours="$cb/" - upstream="$upstreamdir/cookbooks/$cb/" - if [ "$cb" = 'fb_init' ]; then - upstream="$upstreamdir/cookbooks/fb_init_sample/" - fi - if [ "$mode" = 'status' ]; then - diff -Nru "$ours" "$upstream" &>/dev/null || echo "$cb does not match" - elif [ "$mode" = 'diff' ]; then - diff -Nru "$ours" "$upstream" - elif [ "$mode" = 'push' ]; then - rsync -avz "$ours" "$upstream" - elif [ "$mode" = 'sync' ]; then - rsync -avz "$upstream" "$ours" - else - die "wut? wut mode is '$mode'" - fi + ours="$cb/" + upstream="$upstreamdir/cookbooks/$cb/" + if [ "$cb" = 'fb_init' ]; then + upstream="$upstreamdir/cookbooks/fb_init_sample/" + fi + if [ "$mode" = 'status' ]; then + diff -Nru "$ours" "$upstream" &>/dev/null || echo "$cb does not match" + elif [ "$mode" = 'diff' ]; then + diff -Nru "$ours" "$upstream" + elif [ "$mode" = 'push' ]; then + rsync -avz "$ours" "$upstream" + elif [ "$mode" = 'sync' ]; then + rsync -avz "$upstream" "$ours" + else + die "wut? wut mode is '$mode'" + fi done