From 376c915b10c9ef4f80b8e895e7dbf8dcbc6167f3 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Mon, 12 Nov 2018 15:48:24 +0100 Subject: [PATCH] new: added ``cached_wget`` utility function to compose intended for charms to use. --- bin/compose | 18 +++++++++ test/wget | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100755 test/wget diff --git a/bin/compose b/bin/compose index efb1517..4ac1cfc 100755 --- a/bin/compose +++ b/bin/compose @@ -2601,6 +2601,24 @@ EOF echo "}" } +cached_wget() { + local cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \ + url="$1" + if [ -e "$cache_file" ]; then + cat "$cache_file" + touch "$cache_file" + return 0 + fi + wget -O- "${url}" | + tee "$cache_file" + + if [ "${PIPESTATUS[0]}" != 0 ]; then + rm "$cache_file" + die "Unable to fetch '$url'." + return 1 + fi +} +export -f cached_wget [ "$SOURCED" ] && return 0 diff --git a/test/wget b/test/wget new file mode 100755 index 0000000..0397509 --- /dev/null +++ b/test/wget @@ -0,0 +1,109 @@ +#!/usr/bin/env bash-shlib +# -*- mode: shell-script -*- + +include shunit + +depends sed grep git mkdir readlink + +export -f matches +export grep + +tmp=/tmp +tprog="../bin/compose" +tprog=$(readlink -f $tprog) + + +export PATH=".:$PATH" +short_tprog=$(basename "$tprog") + + +## +## Convenience function +## + +init_test() { + test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX) + cd "$test_tmpdir" + export CACHEDIR="$test_tmpdir/.cache" + mkdir -p "$CACHEDIR" +} + + + +tear_test() { + rm -rf "$test_tmpdir" +} + + +## +## Tests +## + + +function test_wget { + + init_test + PORT=45467 + assert_list <