Sfoglia il codice sorgente

Rework reading mk.conf variable values

Markus Hennecke 1 anno fa
parent
commit
afa7f1a9fb
1 ha cambiato i file con 28 aggiunte e 8 eliminazioni
  1. 28 8
      autoupdate.pl

+ 28 - 8
autoupdate.pl

@@ -28,6 +28,31 @@ use FindBin;
 use File::Spec;
 use POSIX qw/uname :sys_wait_h :signal_h/;
 
+
+# Tries to get a variable either from the environment or /etc/mk.conf
+# falling back to the passed default value if the variable was not
+# found in either location
+sub get_mkconf_variable {
+	my $var = shift;
+	my $default = shift || '';
+
+	if (defined $ENV{$var}) {
+		print STDERR "$var = $ENV{$var}\n";
+		return $ENV{$var};
+	}
+
+	my $mkconf = '/etc/mk.conf';
+	return $default if (! -f $mkconf);
+
+	open(my $fh, '-|', 'make', '-f', '/etc/mk.conf', '-V', $var)
+	    or die "Unable to query $mkconf for '$var': $!\n";
+        my $result = do { local $/ = undef; <$fh> };
+	close($fh);
+	chomp $result;
+	return $result || $default;
+}
+
+
 # Defaults to 1, can be set via the --verbose switch
 my $verbose = 1;
 
@@ -46,20 +71,15 @@ my $always_update = 0;
 
 # If set to 1 the make clean part of the ports build will use sudo
 my $sudo_make_clean = 0;
-my $sudo = $ENV{SUDO};
-$sudo = `make -f /etc/mk.conf -V SUDO` if (not defined $sudo);
-$sudo = '' if (not defined $sudo);
-chomp $sudo;
+my $sudo = get_mkconf_variable('SUDO', '');
 
 # If defined via command line the script will use that file as input for
 # out of date ports. If the filename is '-' it will use stdin.
 my $out_of_date = undef;
 
 # The default location of the ports tree and the path where we should look
-# for ports. TODO: Read these variables from /etc/mk.conf and fall back
-# to the default only if we do not find it either there or in the environment.
-my $portsdir = $ENV{PORTSDIR} || '/usr/ports';
-my $portsdir_path = $ENV{PORTSDIR_PATH} || "$portsdir:$portsdir/mystuff";
+my $portsdir = get_mkconf_variable('PORTSDIR', '/usr/ports');
+my $portsdir_path = get_mkconf_variable('PORTSDIR_PATH', "$portsdir:$portsdir/mystuff");
 
 # Number of jobs currently active
 my $jobs = 0;