import/json: Use json->code.
[jackhill/guix/guix.git] / guix / scripts / import / json.scm
CommitLineData
fb1db385
RW
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
3;;; Copyright © 2015, 2017 Ricardo Wurmus <rekado@elephly.net>
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 import json)
21 #:use-module (json)
22 #:use-module (guix ui)
23 #:use-module (guix utils)
24 #:use-module (guix scripts)
25 #:use-module (guix import utils)
c8934323 26 #:use-module (guix import json)
fb1db385
RW
27 #:use-module (guix scripts import)
28 #:use-module (guix packages)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9 gnu)
31 #:use-module (srfi srfi-11)
32 #:use-module (srfi srfi-37)
33 #:use-module (srfi srfi-41)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 rdelim)
36 #:use-module (ice-9 format)
37 #:export (guix-import-json))
38
39\f
40;;;
41;;; Command-line options.
42;;;
43
44(define %default-options
45 '())
46
47(define (show-help)
48 (display (G_ "Usage: guix import json PACKAGE-FILE
49Import and convert the JSON package definition in PACKAGE-FILE.\n"))
50 (display (G_ "
51 -h, --help display this help and exit"))
52 (display (G_ "
53 -V, --version display version information and exit"))
54 (newline)
55 (show-bug-report-information))
56
57(define %options
58 ;; Specification of the command-line options.
59 (cons* (option '(#\h "help") #f #f
60 (lambda args
61 (show-help)
62 (exit 0)))
63 (option '(#\V "version") #f #f
64 (lambda args
65 (show-version-and-exit "guix import json")))
66 %standard-import-options))
67
68\f
69;;;
70;;; Entry point.
71;;;
72
73(define (guix-import-json . args)
74 (define (parse-options)
75 ;; Return the alist of option values.
76 (args-fold* args %options
77 (lambda (opt name arg result)
78 (leave (G_ "~A: unrecognized option~%") name))
79 (lambda (arg result)
80 (alist-cons 'argument arg result))
81 %default-options))
82
83 (let* ((opts (parse-options))
84 (args (filter-map (match-lambda
85 (('argument . value)
86 value)
87 (_ #f))
88 (reverse opts))))
89 (match args
90 ((file-name)
c8934323
RW
91 (or (json->code file-name)
92 (leave (G_ "invalid JSON in file '~a'~%") file-name)))
fb1db385
RW
93 (()
94 (leave (G_ "too few arguments~%")))
95 ((many ...)
96 (leave (G_ "too many arguments~%"))))))