nix-prefetch-url: Improve option handling

This commit is contained in:
Eelco Dolstra 2012-10-23 18:04:00 +02:00
parent 2980d1fba9
commit a28b4445a4
3 changed files with 42 additions and 10 deletions

View file

@ -91,8 +91,9 @@ cryptographic hash as <literal>nix-store --dump
<varlistentry><term><option>--type</option> <replaceable>hashAlgo</replaceable></term> <varlistentry><term><option>--type</option> <replaceable>hashAlgo</replaceable></term>
<listitem><para>Specify a cryptographic hash, which can be one of <listitem><para>Use the specified cryptographic hash algorithm,
<literal>md5</literal>, <literal>sha1</literal>, and which can be one of <literal>md5</literal>,
<literal>sha1</literal>, and
<literal>sha256</literal>.</para></listitem> <literal>sha256</literal>.</para></listitem>
</varlistentry> </varlistentry>

View file

@ -18,6 +18,7 @@
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>nix-prefetch-url</command> <command>nix-prefetch-url</command>
<arg><option>--type</option> <replaceable>hashAlgo</replaceable></arg>
<arg choice='plain'><replaceable>url</replaceable></arg> <arg choice='plain'><replaceable>url</replaceable></arg>
<arg><replaceable>hash</replaceable></arg> <arg><replaceable>hash</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
@ -45,11 +46,6 @@ download it again when you build your Nix expression. Since
as <command>nix-prefetch-url</command>, the redundant download can be as <command>nix-prefetch-url</command>, the redundant download can be
avoided.</para> avoided.</para>
<para>The environment variable <envar>NIX_HASH_ALGO</envar> specifies
which hash algorithm to use. It can be either <literal>md5</literal>,
<literal>sha1</literal>, or <literal>sha256</literal>. The default is
<literal>sha256</literal>.</para>
<para>If <replaceable>hash</replaceable> is specified, then a download <para>If <replaceable>hash</replaceable> is specified, then a download
is not performed if the Nix store already contains a file with the is not performed if the Nix store already contains a file with the
same hash and base name. Otherwise, the file is downloaded, and an same hash and base name. Otherwise, the file is downloaded, and an
@ -63,6 +59,24 @@ of the downloaded file in the Nix store is also printed.</para>
</refsection> </refsection>
<refsection><title>Options</title>
<variablelist>
<varlistentry><term><option>--type</option> <replaceable>hashAlgo</replaceable></term>
<listitem><para>Use the specified cryptographic hash algorithm,
which can be one of <literal>md5</literal>,
<literal>sha1</literal>, and
<literal>sha256</literal>.</para></listitem>
</varlistentry>
</variablelist>
</refsection>
<refsection><title>Examples</title> <refsection><title>Examples</title>
<screen> <screen>

View file

@ -8,11 +8,28 @@ use Nix::Store;
use Nix::Config; use Nix::Config;
use Nix::Utils; use Nix::Utils;
my $url = shift; my $hashType = $ENV{'NIX_HASH_ALGO'} || "sha256"; # obsolete
my $expHash = shift;
my $hashType = $ENV{'NIX_HASH_ALGO'} || "sha256";
my $cacheDir = $ENV{'NIX_DOWNLOAD_CACHE'}; my $cacheDir = $ENV{'NIX_DOWNLOAD_CACHE'};
my @args;
my $arg;
while ($arg = shift) {
if ($arg eq "--help") {
exec "man nix-prefetch-url" or die;
} elsif ($arg eq "--type") {
$hashType = shift;
die "$0: `$arg' requires an argument\n" unless defined $hashType;
} elsif (substr($arg, 0, 1) eq "-") {
die "$0: unknown flag `$arg'\n";
} else {
push @args, $arg;
}
}
my $url = $args[0];
my $expHash = $args[1];
if (!defined $url || $url eq "") { if (!defined $url || $url eq "") {
print STDERR <<EOF print STDERR <<EOF
Usage: nix-prefetch-url URL [EXPECTED-HASH] Usage: nix-prefetch-url URL [EXPECTED-HASH]