chiark / gitweb /
nailing-cargo: -o alias for --online, -O alias for --offline
[nailing-cargo.git] / nailing-cargo
index 4cc9eac3314c9087698cea39b6eaf2df3a2d9f4c..a4d2d4a177579bc1fd2ba994b9f701291499f70a 100755 (executable)
 #
 #         The arguments are
 #             --manifest-path=<path/to/Cargo.toml>
-#             --locked --target-dir=target
+#             --locked
+#             --target-dir=target
+#
+#    -T | --no-cargo-target-arg
+#    -t | --cargo-target-arg
+#         Suppress --target (or un-suppress it).  Only useful with -m.
+#         Done automatically when nailing-cargo sees that the cargo
+#         subcommand is one which needs it, eg `fetch'.
+#
+#    --online  | -o
+#    --offline | -O
+#         Whether to allow cargo to make network access.
+#         (nailing-cargo always passes --offline to cargo, unless
+#         --online is in force).  The default is offline,
+#         unless the cargo subcommand is one which implies
+#         online (currently, `fetch').
 #
 # Cargo.nail:
 #
 #    <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"
 #  or
 #    use = "null"
 #
+# Other settings:
+#    [misc]
+#    online = true   # forces default to be --online
+#    online = false  # forces default to be --offline
+#
 # Limitations:
 #
 #   Always dirties everyone's Cargo.toml, but tries to put them
@@ -195,7 +223,9 @@ our $lockfile = "../.nailing-cargo.lock";
 
 our $cargo_lock_update;
 our $cargo_manifest_args;
+our $cargo_target_arg=1;
 our $alt_cargo_lock;
+our $online;
 
 our @configs;
 our $verbose=1;
@@ -223,6 +253,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;
@@ -239,19 +270,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; }
@@ -287,13 +310,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 {
@@ -367,6 +393,8 @@ sub readnail () {
       $nail->{subdirs}
     ];
   }
+
+  unshift @configs, $nail;
 }
 
 our @alt_cargo_lock_stat;
@@ -492,20 +520,29 @@ sub calculate () {
 }
 
 sub addargs () {
+  $online //= cfg_bool qw(misc online);
+
   if (@ARGV>=2 &&
-      $ARGV[0] =~ m{\bcargo\b} &&
-      $ARGV[1] =~ m/^(?:generate-lockfile|update)$/) {
-    $cargo_lock_update //= 1;
-    $target = undef;
-  } else {
-    $cargo_lock_update //= 0;
+      $ARGV[0] =~ m{\bcargo\b}) {
+    if ($ARGV[1] =~ m/^(?:generate-lockfile|update)$/) {
+      $cargo_lock_update //= 1;
+      $target = undef;
+    }
+    if ($ARGV[1] =~ m/^(?:fetch)$/) {
+      $cargo_target_arg=0;
+      $online //= 1;
+    }
   }
+  $cargo_lock_update //= 0;
   $cargo_manifest_args //=
     (defined $oot_dir) && !$cargo_lock_update;
 
+  $online //= 0;
+
   if ($cargo_manifest_args) {
     push @ARGV, "--manifest-path=${src_absdir}/Cargo.toml",
-      qw(--locked --target-dir=target);
+      qw(--locked);
+    push @ARGV, qw(--target-dir=target) if $cargo_target_arg;
   }
 
   if (defined $target) {
@@ -515,6 +552,8 @@ sub addargs () {
     }
     push @ARGV, "--target=$target";
   }
+
+  push @ARGV, "--offline" unless $online;
 }
 
 our $oot_absdir;
@@ -678,7 +717,18 @@ sub nailed ($) {
 }    
 
 sub install () {
+  my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
+    ? (stat _) : ();
   foreach my $mf (keys %manifests) {
+    if (@our_unfound_stab) {
+      if (stat_exists $mf, "manifest in to-be-nailed directory") {
+       my @mf_stab = stat _ ;
+       if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
+         @our_unfound_stab = ();
+       }
+      }
+    }
+
     my $nailing = "$mf.nailing~";
     my $nailed = nailed($mf);
     my ($use, $rm);
@@ -701,6 +751,11 @@ sub install () {
     unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
     print STDERR "$self: nailed $mf\n" if $verbose>=3;
   }
+
+  if (@our_unfound_stab) {
+    print STDERR
+ "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
+  }
 }
 
 sub invoke () {
@@ -780,6 +835,10 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
        $cargo_lock_update= $1=~m/[a-z]/;
       } elsif (s{^-([mM])}{-}) {
        $cargo_manifest_args= $1=~m/[a-z]/;
+      } elsif (s{^-([tT])}{-}) {
+       $cargo_target_arg= $1=~m/[a-z]/;
+      } elsif (s{^-([oO])}{-}) {
+       $online= $1=~m/[a-z]/;
       } else {
        die "$self: unknown short option(s) $_\n";
       }
@@ -790,6 +849,10 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
     $cargo_lock_update= !!$1;
   } elsif (m{^--(no-)?cargo-manifest-args}) {
     $cargo_manifest_args= !!$1;
+  } elsif (m{^--(no-)?cargo-target-arg}) {
+    $cargo_target_arg= !!$1;
+  } elsif (m{^--(on|off)line$}) {
+    $online = $1 eq 'on';
   } else {
     die "$self: unknown long option $_\n";
   }
@@ -797,6 +860,7 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
 
 die "$self: need command to run\n" unless @ARGV || $noact;
 
+loadconfigs();
 takelock();
 readnail();
 consider_alt_cargo_lock();