From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1166134-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 215C8138350
	for <garchives@archives.gentoo.org>; Wed, 29 Apr 2020 05:34:01 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 2EC80E082B;
	Wed, 29 Apr 2020 05:34:00 +0000 (UTC)
Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id EA672E082B
	for <gentoo-commits@lists.gentoo.org>; Wed, 29 Apr 2020 05:33:59 +0000 (UTC)
Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 7FF0E34EF8F
	for <gentoo-commits@lists.gentoo.org>; Wed, 29 Apr 2020 05:33:58 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 064CE9C
	for <gentoo-commits@lists.gentoo.org>; Wed, 29 Apr 2020 05:33:57 +0000 (UTC)
From: "Robin H. Johnson" <robbat2@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Robin H. Johnson" <robbat2@gentoo.org>
Message-ID: <1588138428.adddc138088d7f2157cfcdd72402b9395be4ccb0.robbat2@gentoo>
Subject: [gentoo-commits] proj/gentoo-mirrorstats:master commit in: /
X-VCS-Repository: proj/gentoo-mirrorstats
X-VCS-Files: mirmon-wrapper.sh
X-VCS-Directories: /
X-VCS-Committer: robbat2
X-VCS-Committer-Name: Robin H. Johnson
X-VCS-Revision: adddc138088d7f2157cfcdd72402b9395be4ccb0
X-VCS-Branch: master
Date: Wed, 29 Apr 2020 05:33:57 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: f92d86cb-18eb-4eab-a60d-a8a4eac29a7d
X-Archives-Hash: 633a2b4bdb3faa5ffe0a5e44db790d25

commit:     adddc138088d7f2157cfcdd72402b9395be4ccb0
Author:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
AuthorDate: Wed Apr 29 05:33:48 2020 +0000
Commit:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
CommitDate: Wed Apr 29 05:33:48 2020 +0000
URL:        https://gitweb.gentoo.org/proj/gentoo-mirrorstats.git/commit/?id=adddc138

wrapper: input validations

Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>

 mirmon-wrapper.sh | 74 +++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 24 deletions(-)

diff --git a/mirmon-wrapper.sh b/mirmon-wrapper.sh
index ba731c6..2c50369 100755
--- a/mirmon-wrapper.sh
+++ b/mirmon-wrapper.sh
@@ -24,9 +24,6 @@ die() {
 }
 
 main() {
-	[[ -z "$MODE" ]] && die "--mode unset"
-	[[ -z "$XML_SRC" ]] && die "--xml-src unset"
-	[[ -z "$URL" ]] && die "--url unset"
 	SITEDIR=/var/www/mirrorstats.gentoo.org
 	REPODIR=${SITEDIR}/gentoo-mirrorstats/
 	MODEDIR=${REPODIR}/${MODE}_mirrors/
@@ -35,35 +32,40 @@ main() {
 	MIRMON=$(readlink -f "${SITEDIR}"/mirmon/mirmon)
 	CONFFILE=$(readlink -f "${MODEDIR}"/mirmon.conf)
 	VAR_GMIRRORS=${VARDIR}/g.mirrors
-	
-	# Grab mirrors from the web
-	mkdir -p "${VARDIR}"
-	"${REPODIR}"/get-mirrors-from-${XML_SRC}-xml.rb > "${VAR_GMIRRORS}".tmp
-	
 	EXTRA_MIRRORS=${MODEDIR}/g.mirrors.extra
+
+	# Grab mirrors from the web
+	mkdir -p "${VARDIR}" "${HTDOCS}" || die "Failed to mkdir"
+	"${REPODIR}/get-mirrors-from-${XML_SRC}-xml.rb" > "${VAR_GMIRRORS}".tmp || die "Unable to fetch mirror list"
+
 	[[ -e "${EXTRA_MIRRORS}" ]] && cat "${EXTRA_MIRRORS}" >>"${VAR_GMIRRORS}".tmp
-	
+
 	# Atomic rotate into place
-	mv -f "${VAR_GMIRRORS}"{.tmp,}
-	
+	mv -f "${VAR_GMIRRORS}"{.tmp,} || die "Failed to set mirror list"
+
 	# fatal if the state file is NOT present.
-	[[ -e ${VARDIR}/mirmon.state ]] || touch "${VARDIR}/mirmon.state"
-	
+	if [[ ! -e ${VARDIR}/mirmon.state ]]; then
+		touch "${VARDIR}/mirmon.state" || die "Could not create state file"
+	else
+		test -w "${VARDIR}/mirmon.state" || die "Cannot write state file!"
+	fi
+
 	# run mirmon
-	/usr/bin/perl "${MIRMON}" -c "${CONFFILE}" -q -get update
-	
+	/usr/bin/perl "${MIRMON}" -c "${CONFFILE}" -q -get update || die "mirmon failed: $?"
+
 	# Set up a nice link to our mirror page directly:
-	mkdir -p ${HTDOCS}
 	sed \
-	    -e "s#mirrors</H2>#<a href="${URL}">mirrors</a></H2>#" \
+	    -e "s#mirrors</H2>#<a href=\"${URL}\">mirrors</a></H2>#" \
 	    >"${HTDOCS}"/index.html.tmp <"${HTDOCS}"/index-wip.html \
-		&& mv -f "${HTDOCS}"/index.html{.tmp,}
-	
+		|| die "Failed to fix URLs(sed)"
+	mv -f "${HTDOCS}"/index.html{.tmp,} || die "Failed to fix URLs(mv)"
+
 	# Generate a json file containing the state of each mirror
 	"${REPODIR}"/generate-json.py \
 		"${VARDIR}"/mirmon.state \
 		>"${HTDOCS}"/state.json.tmp \
-		&& mv -f "${HTDOCS}"/state.json{.tmp,}
+	|| die "Failed to generate JSON (py)"
+	mv -f "${HTDOCS}"/state.json{.tmp,} || die "Failed to generate JSON (mv)"
 
 	# Done
 	exit 0
@@ -115,11 +117,35 @@ while [[ $# -gt 0 ]]; do
     esac
 done
 
+# Validations
+MODE_INPUTS=(
+	distfiles
+	rsync
+	snapshots
+	experimental
+	releases
+)
+MODE_VALID=0
+for m in "${MODE_INPUTS[@]}"; do
+	[[ "$m" == "$MODE" ]] && MODE_VALID=1
+done
+[[ $MODE_VALID -eq 1 ]] || MODE=''
+
+XML_SRC_INPUTS=(
+	distfiles
+	rsync
+)
+XML_SRC_VALID=0
+for m in "${XML_SRC_INPUTS[@]}"; do
+	[[ "$m" == "$XML_SRC" ]] && XML_SRC_VALID=1
+done
+[[ $XML_SRC_VALID -eq 1 ]] || XML_SRC=''
+
+[[ -z "$MODE" ]] && die "--mode must be one of: ${MODE_INPUTS[*]}"
+[[ -z "$XML_SRC" ]] && die "--xml-src must be one of: ${XML_SRC_INPUTS[*]}"
+[[ -z "$URL" ]] && die "--url unset"
+
 [[ $INVALID -eq 1 ]] && usage
 [[ $HELP -eq 1 ]] && dohelp
 
 main
-
-#MODE=snapshots
-#XML_SRC=distfiles
-#URL=https://www.gentoo.org/downloads/mirrors/