From ab952e58639a4e7c0596b4bedc16cd99adbe68a3 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Tue, 24 Mar 2020 15:24:51 -0400 Subject: [PATCH] mirror: Introduce two functions for running commands They are thin wrappers over `system`, but two main points: * They log the (unescaped) command. * `run` will auto-die. This removes the need to pepper `== 0 or die` in the script. --- mirror-nixos-branch.pl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mirror-nixos-branch.pl b/mirror-nixos-branch.pl index e9226c8..25ab9ec 100755 --- a/mirror-nixos-branch.pl +++ b/mirror-nixos-branch.pl @@ -15,6 +15,27 @@ use List::MoreUtils qw(uniq); use Net::Amazon::S3; use POSIX qw(strftime); +# Runs the given command, printing the (unescaped) command. +# This command continues on failure. +sub runAllowFailure { + print STDERR " \$ ", join(" ", @_), "\n"; + system(@_); +} + +# Runs the given command, printing the (unescaped) command. +# This command dies on failure. +sub run { + my $context = caller(0); + my $code = runAllowFailure(@_); + unless ($code == 0) { + my $exit = $code >> 8; + my $errno = $code - ($exit << 8); + die "Command failed with code ($exit) errno ($errno).\n"; + } + + return $code; +} + my $channelName = $ARGV[0]; my $releaseUrl = $ARGV[1];