Move `sha256' to (guix hash).
[jackhill/guix/guix.git] / guix / scripts / hash.scm
CommitLineData
6c365eca
NK
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (guix scripts hash)
72626a71
LC
21 #:use-module (guix base32)
22 #:use-module (guix hash)
23 #:use-module (guix ui)
24 #:use-module (guix utils)
25 #:use-module (rnrs io ports)
26 #:use-module (rnrs files)
27 #:use-module (ice-9 match)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-26)
30 #:use-module (srfi srfi-37)
31 #:export (guix-hash))
6c365eca
NK
32
33\f
34;;;
35;;; Command-line options.
36;;;
37
38(define %default-options
39 ;; Alist of default option values.
40 `((format . ,bytevector->nix-base32-string)))
41
42(define (show-help)
43 (display (_ "Usage: guix hash [OPTION] FILE
44Return the cryptographic hash of FILE.
45
46Supported formats: 'nix-base32' (default), 'base32', and 'base16'
47('hex' and 'hexadecimal' can be used as well).\n"))
48 (format #t (_ "
49 -f, --format=FMT write the hash in the given format"))
50 (newline)
51 (display (_ "
52 -h, --help display this help and exit"))
53 (display (_ "
54 -V, --version display version information and exit"))
55 (newline)
56 (show-bug-report-information))
57
58(define %options
59 ;; Specification of the command-line options.
60 (list (option '(#\f "format") #t #f
61 (lambda (opt name arg result)
62 (define fmt-proc
63 (match arg
64 ("nix-base32"
65 bytevector->nix-base32-string)
66 ("base32"
67 bytevector->base32-string)
68 ((or "base16" "hex" "hexadecimal")
69 bytevector->base16-string)
70 (x
71 (leave (_ "unsupported hash format: ~a~%")
72 arg))))
73
74 (alist-cons 'format fmt-proc
75 (alist-delete 'format result))))
76
77 (option '(#\h "help") #f #f
78 (lambda args
79 (show-help)
80 (exit 0)))
81 (option '(#\V "version") #f #f
82 (lambda args
83 (show-version-and-exit "guix hash")))))
84
85
86\f
87;;;
88;;; Entry point.
89;;;
90
91(define (guix-hash . args)
92 (define (parse-options)
93 ;; Return the alist of option values.
a5975ced
LC
94 (args-fold* args %options
95 (lambda (opt name arg result)
96 (leave (_ "unrecognized option: ~a~%")
97 name))
98 (lambda (arg result)
99 (alist-cons 'argument arg result))
100 %default-options))
6c365eca 101
ccbce848
LC
102 (define (eof->null x)
103 (if (eof-object? x)
104 #vu8()
105 x))
6c365eca 106
ccbce848
LC
107 (let* ((opts (parse-options))
108 (args (filter-map (match-lambda
109 (('argument . value)
110 value)
111 (_ #f))
112 (reverse opts)))
113 (fmt (assq-ref opts 'format)))
114
115 (match args
116 ((file)
117 (catch 'system-error
118 (lambda ()
119 (format #t "~a~%"
120 (call-with-input-file file
121 (compose fmt sha256 eof->null get-bytevector-all))))
122 (lambda args
123 (leave (_ "~a~%")
124 (strerror (system-error-errno args))))))
125 (_
126 (leave (_ "wrong number of arguments~%"))))))