guix-download: Gracefully handle invalid URIs.

* guix-download.in (guix-download): Error out when `string->uri'
  returns #f.  Use `leave' when the scheme is unknown.
* tests/guix-download.sh: Add tests.
master
Ludovic Courtès 2012-11-13 22:13:11 +01:00
parent db32c281dd
commit ecdb81e159
2 changed files with 11 additions and 6 deletions

View File

@ -159,16 +159,15 @@ Report bugs to: ~a.~%") "@PACKAGE_BUGREPORT@"))
(let* ((opts (parse-options)) (let* ((opts (parse-options))
(store (open-connection)) (store (open-connection))
(uri (string->uri (assq-ref opts 'argument))) (uri (or (string->uri (assq-ref opts 'argument))
(leave (_ "guix-download: ~a: failed to parse URI~%")
(assq-ref opts 'argument))))
(fetch (case (uri-scheme uri) (fetch (case (uri-scheme uri)
((http) http-fetch) ((http) http-fetch)
((ftp) ftp-fetch) ((ftp) ftp-fetch)
(else (else
(begin (leave (_ "guix-download: ~a: unsupported URI scheme~%")
(format (current-error-port) (uri-scheme uri)))))
(_ "guix-download: ~a: unsupported URI scheme~%")
(uri-scheme uri))
(exit 1)))))
(path (call-with-temporary-output-file (path (call-with-temporary-output-file
(lambda (name port) (lambda (name port)
(fetch uri port) (fetch uri port)

View File

@ -25,3 +25,9 @@ guix-download --version
# Make sure it fails here. # Make sure it fails here.
if guix-download http://www.example.com/does-not-exist if guix-download http://www.example.com/does-not-exist
then false; else true; fi then false; else true; fi
if guix-download unknown://some/where;
then false; else true; fi
if guix-download not/a/uri;
then false; else true; fi