json.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  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. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  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
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  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)
  26. #:use-module (guix import json)
  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. ;;; Command-line options.
  40. ;;;
  41. (define %default-options
  42. '())
  43. (define (show-help)
  44. (display (G_ "Usage: guix import json PACKAGE-FILE
  45. Import and convert the JSON package definition in PACKAGE-FILE.\n"))
  46. (display (G_ "
  47. -h, --help display this help and exit"))
  48. (display (G_ "
  49. -V, --version display version information and exit"))
  50. (newline)
  51. (show-bug-report-information))
  52. (define %options
  53. ;; Specification of the command-line options.
  54. (cons* (option '(#\h "help") #f #f
  55. (lambda args
  56. (show-help)
  57. (exit 0)))
  58. (option '(#\V "version") #f #f
  59. (lambda args
  60. (show-version-and-exit "guix import json")))
  61. %standard-import-options))
  62. ;;;
  63. ;;; Entry point.
  64. ;;;
  65. (define (guix-import-json . args)
  66. (define (parse-options)
  67. ;; Return the alist of option values.
  68. (args-fold* args %options
  69. (lambda (opt name arg result)
  70. (leave (G_ "~A: unrecognized option~%") name))
  71. (lambda (arg result)
  72. (alist-cons 'argument arg result))
  73. %default-options))
  74. (let* ((opts (parse-options))
  75. (args (filter-map (match-lambda
  76. (('argument . value)
  77. value)
  78. (_ #f))
  79. (reverse opts))))
  80. (match args
  81. ((file-name)
  82. (catch 'system-error
  83. (lambda ()
  84. (or (json->code file-name)
  85. (leave (G_ "invalid JSON in file '~a'~%") file-name)))
  86. (lambda args
  87. (leave (G_ "failed to access '~a': ~a~%")
  88. file-name (strerror (system-error-errno args))))))
  89. (()
  90. (leave (G_ "too few arguments~%")))
  91. ((many ...)
  92. (leave (G_ "too many arguments~%"))))))