builtins.fetch{url,tarball}: Allow name attribute

(cherry picked from commit d52d391164)
This commit is contained in:
Shea Levy 2016-08-15 07:37:11 -04:00
parent 66151dc154
commit 7bb4d028a8
3 changed files with 12 additions and 8 deletions

View File

@ -1657,6 +1657,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
if (state.restricted) throw Error(format("%1% is not allowed in restricted mode") % who);
string url;
string name;
state.forceValue(*args[0]);
@ -1665,9 +1666,11 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
state.forceAttrs(*args[0], pos);
for (auto & attr : *args[0]->attrs) {
string name(attr.name);
if (name == "url")
string n(attr.name);
if (n == "url")
url = state.forceStringNoCtx(*attr.value, *attr.pos);
else if (n == "name")
name = state.forceStringNoCtx(*attr.value, *attr.pos);
else
throw EvalError(format("unsupported argument %1% to %2%, at %3%") % attr.name % who % attr.pos);
}
@ -1678,7 +1681,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
} else
url = state.forceStringNoCtx(*args[0], pos);
Path res = downloadFileCached(url, unpack);
Path res = downloadFileCached(url, unpack, name);
mkString(v, res, PathSet({res}));
}

View File

@ -188,7 +188,7 @@ DownloadResult downloadFile(string url, const DownloadOptions & options)
}
Path downloadFileCached(const string & url, bool unpack)
Path downloadFileCached(const string & url, bool unpack, string name)
{
Path cacheDir = getEnv("XDG_CACHE_HOME", getEnv("HOME", "") + "/.cache") + "/nix/tarballs";
createDirs(cacheDir);
@ -223,9 +223,10 @@ Path downloadFileCached(const string & url, bool unpack)
storePath = "";
}
string name;
auto p = url.rfind('/');
if (p != string::npos) name = string(url, p + 1);
if (name == "") {
auto p = url.rfind('/');
if (p != string::npos) name = string(url, p + 1);
}
if (!skip) {

View File

@ -20,7 +20,7 @@ struct DownloadResult
DownloadResult downloadFile(string url, const DownloadOptions & options);
Path downloadFileCached(const string & url, bool unpack);
Path downloadFileCached(const string & url, bool unpack, string name = "");
MakeError(DownloadError, Error)