Commit | Line | Data |
---|---|---|
233e7676 LC |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org> | |
e49951eb | 3 | ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org> |
98eb8cbe | 4 | ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org> |
073c34d7 | 5 | ;;; |
233e7676 | 6 | ;;; This file is part of GNU Guix. |
073c34d7 | 7 | ;;; |
233e7676 | 8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
073c34d7 LC |
9 | ;;; under the terms of the GNU General Public License as published by |
10 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
11 | ;;; your option) any later version. | |
12 | ;;; | |
233e7676 | 13 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
073c34d7 LC |
14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;;; GNU General Public License for more details. | |
17 | ;;; | |
18 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
073c34d7 LC |
20 | |
21 | (define-module (guix ui) | |
22 | #:use-module (guix utils) | |
23 | #:use-module (guix store) | |
cdd5d6f9 | 24 | #:use-module (guix config) |
073c34d7 | 25 | #:use-module (guix packages) |
9b222abe | 26 | #:use-module (guix build-system) |
9bb2b96a | 27 | #:use-module (guix derivations) |
299112d3 LC |
28 | #:use-module ((guix licenses) #:select (license? license-name)) |
29 | #:use-module (srfi srfi-1) | |
30 | #:use-module (srfi srfi-11) | |
2cd09108 | 31 | #:use-module (srfi srfi-19) |
073c34d7 LC |
32 | #:use-module (srfi srfi-26) |
33 | #:use-module (srfi srfi-34) | |
a5975ced | 34 | #:use-module (srfi srfi-37) |
e31ff8b8 | 35 | #:autoload (ice-9 ftw) (scandir) |
64fc89b6 | 36 | #:use-module (ice-9 match) |
9bb2b96a | 37 | #:use-module (ice-9 format) |
2cd09108 | 38 | #:use-module (ice-9 regex) |
073c34d7 LC |
39 | #:export (_ |
40 | N_ | |
41 | leave | |
cdd5d6f9 | 42 | show-version-and-exit |
3441e164 | 43 | show-bug-report-information |
969e678e | 44 | string->number* |
9bb2b96a | 45 | show-what-to-build |
073c34d7 | 46 | call-with-error-handling |
64fc89b6 | 47 | with-error-handling |
ac5de156 | 48 | read/eval |
eb0880e7 | 49 | read/eval-package-expression |
299112d3 | 50 | location->string |
c61b026e | 51 | switch-symlinks |
f651b477 | 52 | config-directory |
299112d3 LC |
53 | fill-paragraph |
54 | string->recutils | |
e49951eb | 55 | package->recutils |
2876b989 | 56 | package-specification->name+version+output |
2cd09108 NK |
57 | string->generations |
58 | string->duration | |
a5975ced | 59 | args-fold* |
e49951eb | 60 | run-guix-command |
a2011be5 LC |
61 | program-name |
62 | guix-warning-port | |
63 | warning | |
e49951eb | 64 | guix-main)) |
073c34d7 LC |
65 | |
66 | ;;; Commentary: | |
67 | ;;; | |
68 | ;;; User interface facilities for command-line tools. | |
69 | ;;; | |
70 | ;;; Code: | |
71 | ||
72 | (define %gettext-domain | |
73 | "guix") | |
74 | ||
75 | (define _ (cut gettext <> %gettext-domain)) | |
76 | (define N_ (cut ngettext <> <> <> %gettext-domain)) | |
77 | ||
b2a886f6 LC |
78 | (define-syntax-rule (define-diagnostic name prefix) |
79 | "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all | |
80 | messages." | |
81 | (define-syntax name | |
82 | (lambda (x) | |
83 | (define (augmented-format-string fmt) | |
84 | (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt))) | |
85 | ||
89d02b98 LC |
86 | (syntax-case x () |
87 | ((name (underscore fmt) args (... ...)) | |
88 | (and (string? (syntax->datum #'fmt)) | |
89 | (free-identifier=? #'underscore #'_)) | |
b2a886f6 LC |
90 | (with-syntax ((fmt* (augmented-format-string #'fmt)) |
91 | (prefix (datum->syntax x prefix))) | |
92 | #'(format (guix-warning-port) (gettext fmt*) | |
93 | (program-name) (program-name) prefix | |
94 | args (... ...)))) | |
89d02b98 | 95 | ((name (N-underscore singular plural n) args (... ...)) |
b2a886f6 | 96 | (and (string? (syntax->datum #'singular)) |
89d02b98 LC |
97 | (string? (syntax->datum #'plural)) |
98 | (free-identifier=? #'N-underscore #'N_)) | |
b2a886f6 LC |
99 | (with-syntax ((s (augmented-format-string #'singular)) |
100 | (p (augmented-format-string #'plural)) | |
101 | (prefix (datum->syntax x prefix))) | |
102 | #'(format (guix-warning-port) | |
103 | (ngettext s p n %gettext-domain) | |
104 | (program-name) (program-name) prefix | |
105 | args (... ...)))))))) | |
106 | ||
107 | (define-diagnostic warning "warning: ") ; emit a warning | |
108 | ||
109 | (define-diagnostic report-error "error: ") | |
110 | (define-syntax-rule (leave args ...) | |
111 | "Emit an error message and exit." | |
112 | (begin | |
113 | (report-error args ...) | |
114 | (exit 1))) | |
115 | ||
116 | (define (install-locale) | |
117 | "Install the current locale settings." | |
118 | (catch 'system-error | |
119 | (lambda _ | |
120 | (setlocale LC_ALL "")) | |
121 | (lambda args | |
122 | (warning (_ "failed to install locale: ~a~%") | |
123 | (strerror (system-error-errno args)))))) | |
124 | ||
e49951eb | 125 | (define (initialize-guix) |
633f045f | 126 | "Perform the usual initialization for stand-alone Guix commands." |
e49951eb | 127 | (install-locale) |
39e9f95d | 128 | (textdomain %gettext-domain) |
e14c3929 LC |
129 | |
130 | ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be | |
131 | ;; notified via an EPIPE later. | |
132 | (sigaction SIGPIPE SIG_IGN) | |
133 | ||
e49951eb MW |
134 | (setvbuf (current-output-port) _IOLBF) |
135 | (setvbuf (current-error-port) _IOLBF)) | |
136 | ||
cdd5d6f9 LC |
137 | (define* (show-version-and-exit #:optional (command (car (command-line)))) |
138 | "Display version information for COMMAND and `(exit 0)'." | |
139 | (simple-format #t "~a (~a) ~a~%" | |
140 | command %guix-package-name %guix-version) | |
64a967cc LC |
141 | (display (_ "Copyright (C) 2013 the Guix authors |
142 | License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> | |
143 | This is free software: you are free to change and redistribute it. | |
144 | There is NO WARRANTY, to the extent permitted by law. | |
145 | ")) | |
cdd5d6f9 LC |
146 | (exit 0)) |
147 | ||
3441e164 LC |
148 | (define (show-bug-report-information) |
149 | (format #t (_ " | |
150 | Report bugs to: ~a.") %guix-bug-report-address) | |
151 | (format #t (_ " | |
152 | ~a home page: <~a>") %guix-package-name %guix-home-page-url) | |
153 | (display (_ " | |
154 | General help using GNU software: <http://www.gnu.org/gethelp/>")) | |
155 | (newline)) | |
156 | ||
969e678e LC |
157 | (define (string->number* str) |
158 | "Like `string->number', but error out with an error message on failure." | |
159 | (or (string->number str) | |
160 | (leave (_ "~a: invalid number~%") str))) | |
161 | ||
073c34d7 LC |
162 | (define (call-with-error-handling thunk) |
163 | "Call THUNK within a user-friendly error handler." | |
164 | (guard (c ((package-input-error? c) | |
165 | (let* ((package (package-error-package c)) | |
166 | (input (package-error-invalid-input c)) | |
167 | (location (package-location package)) | |
168 | (file (location-file location)) | |
169 | (line (location-line location)) | |
170 | (column (location-column location))) | |
98eb8cbe | 171 | (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%") |
073c34d7 LC |
172 | file line column |
173 | (package-full-name package) input))) | |
9b222abe LC |
174 | ((package-cross-build-system-error? c) |
175 | (let* ((package (package-error-package c)) | |
176 | (loc (package-location package)) | |
177 | (system (package-build-system package))) | |
178 | (leave (_ "~a: ~a: build system `~a' does not support cross builds~%") | |
179 | (location->string loc) | |
180 | (package-full-name package) | |
181 | (build-system-name system)))) | |
ef86c39f | 182 | ((nix-connection-error? c) |
98eb8cbe | 183 | (leave (_ "failed to connect to `~a': ~a~%") |
ef86c39f LC |
184 | (nix-connection-error-file c) |
185 | (strerror (nix-connection-error-code c)))) | |
073c34d7 LC |
186 | ((nix-protocol-error? c) |
187 | ;; FIXME: Server-provided error messages aren't i18n'd. | |
98eb8cbe | 188 | (leave (_ "build failed: ~a~%") |
073c34d7 | 189 | (nix-protocol-error-message c)))) |
e14c3929 LC |
190 | ;; Catch EPIPE and the likes. |
191 | (catch 'system-error | |
192 | thunk | |
193 | (lambda args | |
194 | (leave (_ "~a~%") | |
195 | (strerror (system-error-errno args))))))) | |
073c34d7 | 196 | |
ac5de156 LC |
197 | (define (read/eval str) |
198 | "Read and evaluate STR, raising an error if something goes wrong." | |
eb0880e7 LC |
199 | (let ((exp (catch #t |
200 | (lambda () | |
201 | (call-with-input-string str read)) | |
202 | (lambda args | |
203 | (leave (_ "failed to read expression ~s: ~s~%") | |
204 | str args))))) | |
ac5de156 LC |
205 | (catch #t |
206 | (lambda () | |
207 | (eval exp the-scm-module)) | |
208 | (lambda args | |
209 | (leave (_ "failed to evaluate expression `~a': ~s~%") | |
210 | exp args))))) | |
211 | ||
212 | (define (read/eval-package-expression str) | |
213 | "Read and evaluate STR and return the package it refers to, or exit an | |
214 | error." | |
215 | (match (read/eval str) | |
216 | ((? package? p) p) | |
217 | (_ | |
218 | (leave (_ "expression ~s does not evaluate to a package~%") | |
219 | str)))) | |
eb0880e7 | 220 | |
dd36b51b LC |
221 | (define* (show-what-to-build store drv |
222 | #:key dry-run? (use-substitutes? #t)) | |
9bb2b96a | 223 | "Show what will or would (depending on DRY-RUN?) be built in realizing the |
4d60610a | 224 | derivations listed in DRV. Return #t if there's something to build, #f |
dd36b51b LC |
225 | otherwise. When USE-SUBSTITUTES?, check and report what is prerequisites are |
226 | available for download." | |
227 | (let*-values (((build download) | |
59688fc4 LC |
228 | (fold2 (lambda (drv build download) |
229 | (let-values (((b d) | |
230 | (derivation-prerequisites-to-build | |
231 | store drv | |
232 | #:use-substitutes? | |
233 | use-substitutes?))) | |
234 | (values (append b build) | |
235 | (append d download)))) | |
dd36b51b LC |
236 | '() '() |
237 | drv)) | |
238 | ((build) ; add the DRV themselves | |
239 | (delete-duplicates | |
59688fc4 LC |
240 | (append (map derivation-file-name |
241 | (remove (lambda (drv) | |
242 | (let ((out (derivation->output-path | |
243 | drv))) | |
244 | (or (valid-path? store out) | |
245 | (and use-substitutes? | |
246 | (has-substitutes? store | |
247 | out))))) | |
248 | drv)) | |
dd36b51b LC |
249 | (map derivation-input-path build)))) |
250 | ((download) ; add the references of DOWNLOAD | |
1a8b7834 LC |
251 | (if use-substitutes? |
252 | (delete-duplicates | |
253 | (append download | |
254 | (remove (cut valid-path? store <>) | |
255 | (append-map | |
256 | substitutable-references | |
257 | (substitutable-path-info store | |
258 | download))))) | |
259 | download))) | |
00554b2a | 260 | ;; TODO: Show the installed size of DOWNLOAD. |
9bb2b96a | 261 | (if dry-run? |
dd36b51b LC |
262 | (begin |
263 | (format (current-error-port) | |
83e61a73 LC |
264 | (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]" |
265 | "~:[The following derivations would be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
266 | (length build)) |
267 | (null? build) build) | |
268 | (format (current-error-port) | |
83e61a73 LC |
269 | (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]" |
270 | "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
271 | (length download)) |
272 | (null? download) download)) | |
273 | (begin | |
274 | (format (current-error-port) | |
83e61a73 LC |
275 | (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]" |
276 | "~:[The following derivations will be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
277 | (length build)) |
278 | (null? build) build) | |
279 | (format (current-error-port) | |
83e61a73 LC |
280 | (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]" |
281 | "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
282 | (length download)) |
283 | (null? download) download))) | |
284 | (pair? build))) | |
9bb2b96a | 285 | |
073c34d7 LC |
286 | (define-syntax with-error-handling |
287 | (syntax-rules () | |
288 | "Run BODY within a user-friendly error condition handler." | |
289 | ((_ body ...) | |
290 | (call-with-error-handling | |
291 | (lambda () | |
292 | body ...))))) | |
293 | ||
64fc89b6 LC |
294 | (define (location->string loc) |
295 | "Return a human-friendly, GNU-standard representation of LOC." | |
296 | (match loc | |
297 | (#f (_ "<unknown location>")) | |
298 | (($ <location> file line column) | |
299 | (format #f "~a:~a:~a" file line column)))) | |
300 | ||
c61b026e LC |
301 | (define (switch-symlinks link target) |
302 | "Atomically switch LINK, a symbolic link, to point to TARGET. Works | |
303 | both when LINK already exists and when it does not." | |
304 | (let ((pivot (string-append link ".new"))) | |
305 | (symlink target pivot) | |
306 | (rename-file pivot link))) | |
307 | ||
f651b477 LC |
308 | (define (config-directory) |
309 | "Return the name of the configuration directory, after making sure that it | |
310 | exists. Honor the XDG specs, | |
311 | <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>." | |
312 | (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME") | |
313 | (and=> (getenv "HOME") | |
314 | (cut string-append <> "/.config"))) | |
315 | (cut string-append <> "/guix")))) | |
316 | (catch 'system-error | |
317 | (lambda () | |
318 | (mkdir dir) | |
319 | dir) | |
320 | (lambda args | |
321 | (match (system-error-errno args) | |
322 | ((or EEXIST 0) | |
323 | dir) | |
324 | (err | |
325 | (leave (_ "failed to create configuration directory `~a': ~a~%") | |
326 | dir (strerror err)))))))) | |
327 | ||
299112d3 LC |
328 | (define* (fill-paragraph str width #:optional (column 0)) |
329 | "Fill STR such that each line contains at most WIDTH characters, assuming | |
330 | that the first character is at COLUMN. | |
331 | ||
332 | When STR contains a single line break surrounded by other characters, it is | |
333 | converted to a space; sequences of more than one line break are preserved." | |
334 | (define (maybe-break chr result) | |
335 | (match result | |
336 | ((column newlines chars) | |
337 | (case chr | |
338 | ((#\newline) | |
339 | `(,column ,(+ 1 newlines) ,chars)) | |
340 | (else | |
341 | (let ((chars (case newlines | |
342 | ((0) chars) | |
343 | ((1) (cons #\space chars)) | |
344 | (else | |
345 | (append (make-list newlines #\newline) chars)))) | |
346 | (column (case newlines | |
347 | ((0) column) | |
348 | ((1) (+ 1 column)) | |
349 | (else 0)))) | |
350 | (let ((chars (cons chr chars)) | |
351 | (column (+ 1 column))) | |
352 | (if (> column width) | |
353 | (let*-values (((before after) | |
354 | (break (cut eqv? #\space <>) chars)) | |
355 | ((len) | |
356 | (length before))) | |
357 | (if (<= len width) | |
358 | `(,len | |
359 | 0 | |
360 | ,(if (null? after) | |
361 | before | |
362 | (append before (cons #\newline (cdr after))))) | |
363 | `(,column 0 ,chars))) ; unbreakable | |
364 | `(,column 0 ,chars))))))))) | |
365 | ||
366 | (match (string-fold maybe-break | |
367 | `(,column 0 ()) | |
368 | str) | |
369 | ((_ _ chars) | |
370 | (list->string (reverse chars))))) | |
371 | ||
2876b989 LC |
372 | \f |
373 | ;;; | |
374 | ;;; Packages. | |
375 | ;;; | |
376 | ||
299112d3 LC |
377 | (define (string->recutils str) |
378 | "Return a version of STR where newlines have been replaced by newlines | |
379 | followed by \"+ \", which makes for a valid multi-line field value in the | |
380 | `recutils' syntax." | |
381 | (list->string | |
382 | (string-fold-right (lambda (chr result) | |
383 | (if (eqv? chr #\newline) | |
384 | (cons* chr #\+ #\space result) | |
385 | (cons chr result))) | |
386 | '() | |
387 | str))) | |
388 | ||
389 | (define* (package->recutils p port | |
390 | #:optional (width (or (and=> (getenv "WIDTH") | |
391 | string->number) | |
392 | 80))) | |
393 | "Write to PORT a `recutils' record of package P, arranging to fit within | |
394 | WIDTH columns." | |
395 | (define (description->recutils str) | |
396 | (let ((str (_ str))) | |
397 | (string->recutils | |
398 | (fill-paragraph str width | |
399 | (string-length "description: "))))) | |
400 | ||
401 | ;; Note: Don't i18n field names so that people can post-process it. | |
402 | (format port "name: ~a~%" (package-name p)) | |
403 | (format port "version: ~a~%" (package-version p)) | |
404 | (format port "location: ~a~%" | |
405 | (or (and=> (package-location p) location->string) | |
406 | (_ "unknown"))) | |
407 | (format port "home-page: ~a~%" (package-home-page p)) | |
408 | (format port "license: ~a~%" | |
409 | (match (package-license p) | |
410 | (((? license? licenses) ...) | |
411 | (string-join (map license-name licenses) | |
412 | ", ")) | |
413 | ((? license? license) | |
414 | (license-name license)) | |
415 | (x | |
416 | (_ "unknown")))) | |
417 | (format port "synopsis: ~a~%" | |
418 | (string-map (match-lambda | |
419 | (#\newline #\space) | |
420 | (chr chr)) | |
421 | (or (and=> (package-synopsis p) _) | |
422 | ""))) | |
423 | (format port "description: ~a~%" | |
424 | (and=> (package-description p) description->recutils)) | |
425 | (newline port)) | |
426 | ||
2cd09108 NK |
427 | (define (string->generations str) |
428 | "Return the list of generations matching a pattern in STR. This function | |
429 | accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"." | |
430 | (define (maybe-integer) | |
431 | (let ((x (string->number str))) | |
432 | (and (integer? x) | |
433 | x))) | |
434 | ||
435 | (define (maybe-comma-separated-integers) | |
436 | (let ((lst (delete-duplicates | |
437 | (map string->number | |
438 | (string-split str #\,))))) | |
439 | (and (every integer? lst) | |
440 | lst))) | |
441 | ||
442 | (cond ((maybe-integer) | |
443 | => | |
444 | list) | |
445 | ((maybe-comma-separated-integers) | |
446 | => | |
447 | identity) | |
448 | ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str) | |
449 | => | |
450 | (lambda (match) | |
451 | (let ((s (string->number (match:substring match 1))) | |
452 | (e (string->number (match:substring match 2)))) | |
453 | (and (every integer? (list s e)) | |
454 | (<= s e) | |
455 | (iota (1+ (- e s)) s))))) | |
456 | ((string-match "^([0-9]+)\\.\\.$" str) | |
457 | => | |
458 | (lambda (match) | |
459 | (let ((s (string->number (match:substring match 1)))) | |
460 | (and (integer? s) | |
461 | `(>= ,s))))) | |
462 | ((string-match "^\\.\\.([0-9]+)$" str) | |
463 | => | |
464 | (lambda (match) | |
465 | (let ((e (string->number (match:substring match 1)))) | |
466 | (and (integer? e) | |
467 | `(<= ,e))))) | |
468 | (else #f))) | |
469 | ||
470 | (define (string->duration str) | |
471 | "Return the duration matching a pattern in STR. This function accepts the | |
472 | following patterns: \"1d\", \"1w\", \"1m\"." | |
473 | (define (hours->duration hours match) | |
474 | (make-time time-duration 0 | |
475 | (* 3600 hours (string->number (match:substring match 1))))) | |
476 | ||
477 | (cond ((string-match "^([0-9]+)d$" str) | |
478 | => | |
479 | (lambda (match) | |
480 | (hours->duration 24 match))) | |
481 | ((string-match "^([0-9]+)w$" str) | |
482 | => | |
483 | (lambda (match) | |
484 | (hours->duration (* 24 7) match))) | |
485 | ((string-match "^([0-9]+)m$" str) | |
486 | => | |
487 | (lambda (match) | |
488 | (hours->duration (* 24 30) match))) | |
489 | (else #f))) | |
490 | ||
2876b989 LC |
491 | (define* (package-specification->name+version+output spec |
492 | #:optional (output "out")) | |
493 | "Parse package specification SPEC and return three value: the specified | |
494 | package name, version number (or #f), and output name (or OUTPUT). SPEC may | |
495 | optionally contain a version number and an output name, as in these examples: | |
496 | ||
497 | guile | |
498 | guile-2.0.9 | |
499 | guile:debug | |
500 | guile-2.0.9:debug | |
501 | " | |
502 | (let*-values (((name sub-drv) | |
503 | (match (string-rindex spec #\:) | |
504 | (#f (values spec output)) | |
505 | (colon (values (substring spec 0 colon) | |
506 | (substring spec (+ 1 colon)))))) | |
507 | ((name version) | |
508 | (package-name->name+version name))) | |
509 | (values name version sub-drv))) | |
510 | ||
511 | \f | |
512 | ;;; | |
513 | ;;; Command-line option processing. | |
514 | ;;; | |
515 | ||
a5975ced LC |
516 | (define (args-fold* options unrecognized-option-proc operand-proc . seeds) |
517 | "A wrapper on top of `args-fold' that does proper user-facing error | |
518 | reporting." | |
519 | (catch 'misc-error | |
520 | (lambda () | |
521 | (apply args-fold options unrecognized-option-proc | |
522 | operand-proc seeds)) | |
523 | (lambda (key proc msg args . rest) | |
524 | ;; XXX: MSG is not i18n'd. | |
525 | (leave (_ "invalid argument: ~a~%") | |
526 | (apply format #f msg args))))) | |
527 | ||
e49951eb | 528 | (define (show-guix-usage) |
e49951eb | 529 | (format (current-error-port) |
25c93676 LC |
530 | (_ "Try `guix --help' for more information.~%")) |
531 | (exit 1)) | |
e49951eb | 532 | |
e31ff8b8 LC |
533 | (define (command-files) |
534 | "Return the list of source files that define Guix sub-commands." | |
535 | (define directory | |
536 | (and=> (search-path %load-path "guix.scm") | |
537 | (compose (cut string-append <> "/guix/scripts") | |
538 | dirname))) | |
539 | ||
2b8cf44f LC |
540 | (define dot-scm? |
541 | (cut string-suffix? ".scm" <>)) | |
542 | ||
543 | ;; In Guile 2.0.5 `scandir' would return "." and ".." regardless even though | |
544 | ;; they don't match `dot-scm?'. Work around it by doing additional | |
545 | ;; filtering. | |
e31ff8b8 | 546 | (if directory |
2b8cf44f | 547 | (filter dot-scm? (scandir directory dot-scm?)) |
e31ff8b8 LC |
548 | '())) |
549 | ||
550 | (define (commands) | |
551 | "Return the list of Guix command names." | |
552 | (map (compose (cut string-drop-right <> 4) | |
553 | basename) | |
554 | (command-files))) | |
555 | ||
556 | (define (show-guix-help) | |
557 | (format #t (_ "Usage: guix COMMAND ARGS... | |
558 | Run COMMAND with ARGS.\n")) | |
559 | (newline) | |
560 | (format #t (_ "COMMAND must be one of the sub-commands listed below:\n")) | |
561 | (newline) | |
562 | ;; TODO: Display a synopsis of each command. | |
b30b13dc | 563 | (format #t "~{ ~a~%~}" (sort (commands) string<?)) |
e31ff8b8 LC |
564 | (show-bug-report-information)) |
565 | ||
a2011be5 LC |
566 | (define program-name |
567 | ;; Name of the command-line program currently executing, or #f. | |
568 | (make-parameter #f)) | |
569 | ||
ec5d0a85 LC |
570 | (define (run-guix-command command . args) |
571 | "Run COMMAND with the given ARGS. Report an error when COMMAND is not | |
572 | found." | |
573 | (define module | |
574 | (catch 'misc-error | |
575 | (lambda () | |
576 | (resolve-interface `(guix scripts ,command))) | |
577 | (lambda - | |
25c93676 LC |
578 | (format (current-error-port) |
579 | (_ "guix: ~a: command not found~%") command) | |
580 | (show-guix-usage)))) | |
ec5d0a85 LC |
581 | |
582 | (let ((command-main (module-ref module | |
583 | (symbol-append 'guix- command)))) | |
584 | (parameterize ((program-name command)) | |
585 | (apply command-main args)))) | |
586 | ||
a2011be5 LC |
587 | (define guix-warning-port |
588 | (make-parameter (current-warning-port))) | |
589 | ||
e49951eb MW |
590 | (define (guix-main arg0 . args) |
591 | (initialize-guix) | |
592 | (let () | |
593 | (define (option? str) (string-prefix? "-" str)) | |
594 | (match args | |
25c93676 LC |
595 | (() |
596 | (format (current-error-port) | |
597 | (_ "guix: missing command name~%")) | |
598 | (show-guix-usage)) | |
e12b3eb9 | 599 | ((or ("-h") ("--help")) |
25c93676 LC |
600 | (show-guix-help)) |
601 | (("--version") | |
602 | (show-version-and-exit "guix")) | |
603 | (((? option? o) args ...) | |
604 | (format (current-error-port) | |
605 | (_ "guix: unrecognized option '~a'~%") o) | |
606 | (show-guix-usage)) | |
e49951eb | 607 | ((command args ...) |
ec5d0a85 LC |
608 | (apply run-guix-command |
609 | (string->symbol command) | |
610 | args))))) | |
e49951eb | 611 | |
073c34d7 | 612 | ;;; ui.scm ends here |