chiark / gitweb /
nailing-carog: Read configuration
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 18 Jun 2020 14:45:59 +0000 (15:45 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 18 Jun 2020 14:56:09 +0000 (15:56 +0100)
Actually call loadconfigs.  Make it sensible.  And use its output.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
nailing-cargo

index 65116123abbdc9215b1432724329558f1e786454..d03011180f173b9eb5b11c4e40f6836e8a7bf61f 100755 (executable)
 #    <package> = <subdir>
 #    <package> = { <subdir> = ... }
 #
-# To control use of alternative Cargo.lock filename:
+# Configuration is read from
+#
+#    /etc/nailing-cargo/cfg.toml
+#    ~/.nailing-cargo.toml
+#    ./.nailing-cargo.toml
+#    ../Nailing-Cargo.toml
+#    ../Cargo.nail
+#
+#  To control use of alternative Cargo.lock filename:
 #    [alt_cargolock]
 #
 #    file = true               # equivalent to "Cargo.lock.example"
@@ -244,6 +252,7 @@ sub stat_exists ($$) {
 sub toml_or_enoent ($$) {
   my ($f,$what) = @_;
   my $toml = read_or_enoent($f) // return;
+  print STDERR "Read TOML from $f\n" if $dump;
   my ($v,$e) = from_toml($toml);
   if (!defined $v) {
     chomp $e;
@@ -260,19 +269,11 @@ sub load1config ($) {
 }
 
 sub loadconfigs () {
-  my $cfgleaf = ".nailing-cargo-cfg.toml";
+  my $dotfile = ".nailing-cargo.toml";
+  load1config("../Nailing-Cargo.toml");
+  load1config($dotfile);
+  load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
   load1config("/etc/nailing-cargo/cfg.toml");
-  load1config("$worksphere/$cfgleaf");
-  load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
-}
-
-sub getcfg ($$) {
-  my ($k, $def) = @_;
-  foreach my $cfg (@configs) {
-    my $v = $cfg->{$k};
-    return $v if defined $v;
-  }
-  return $def;
 }
 
 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
@@ -308,13 +309,16 @@ sub badcfg {
 }
 
 sub cfg_uc {
-  my $v = $nail;
-  foreach my $k (@_) {
-    last unless defined $v;
-    ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
-    $v = $v->{$k};
+  foreach my $cfg (@configs) {
+    my $v = $cfg;
+    foreach my $k (@_) {
+      last unless defined $v;
+      ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
+      $v = $v->{$k};
+    }
+    return $v if defined $v;
   }
-  return $v;
+  return undef;
 }
 
 sub cfge {
@@ -388,6 +392,8 @@ sub readnail () {
       $nail->{subdirs}
     ];
   }
+
+  unshift @configs, $nail;
 }
 
 our @alt_cargo_lock_stat;
@@ -851,6 +857,7 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
 
 die "$self: need command to run\n" unless @ARGV || $noact;
 
+loadconfigs();
 takelock();
 readnail();
 consider_alt_cargo_lock();