From ecdb81e159a29c92351bc51c3556a760074f7473 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@gnu.org>
Date: Tue, 13 Nov 2012 22:13:11 +0100
Subject: [PATCH] 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.
---
 guix-download.in       | 11 +++++------
 tests/guix-download.sh |  6 ++++++
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/guix-download.in b/guix-download.in
index 46efaa17e80..a3fd4b55d45 100644
--- a/guix-download.in
+++ b/guix-download.in
@@ -159,16 +159,15 @@ Report bugs to: ~a.~%") "@PACKAGE_BUGREPORT@"))
 
   (let* ((opts  (parse-options))
          (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)
                   ((http) http-fetch)
                   ((ftp)  ftp-fetch)
                   (else
-                   (begin
-                     (format (current-error-port)
-                             (_ "guix-download: ~a: unsupported URI scheme~%")
-                             (uri-scheme uri))
-                     (exit 1)))))
+                   (leave (_ "guix-download: ~a: unsupported URI scheme~%")
+                          (uri-scheme uri)))))
          (path  (call-with-temporary-output-file
                  (lambda (name port)
                    (fetch uri port)
diff --git a/tests/guix-download.sh b/tests/guix-download.sh
index 74ef48ba4bd..fc7b35d1b39 100644
--- a/tests/guix-download.sh
+++ b/tests/guix-download.sh
@@ -25,3 +25,9 @@ guix-download --version
 # Make sure it fails here.
 if guix-download http://www.example.com/does-not-exist
 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
-- 
GitLab