json.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix scripts import json)
  22. #:use-module (json)
  23. #:use-module (guix ui)
  24. #:use-module (guix utils)
  25. #:use-module (guix scripts)
  26. #:use-module (guix import utils)
  27. #:use-module (guix import json)
  28. #:use-module (guix scripts import)
  29. #:use-module (guix packages)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-9 gnu)
  32. #:use-module (srfi srfi-11)
  33. #:use-module (srfi srfi-37)
  34. #:use-module (srfi srfi-41)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 rdelim)
  37. #:use-module (ice-9 format)
  38. #:export (guix-import-json))
  39. ;;;
  40. ;;; Command-line options.
  41. ;;;
  42. (define %default-options
  43. '())
  44. (define (show-help)
  45. (display (G_ "Usage: guix import json PACKAGE-FILE
  46. Import and convert the JSON package definition in PACKAGE-FILE.\n"))
  47. (display (G_ "
  48. -h, --help display this help and exit"))
  49. (display (G_ "
  50. -V, --version display version information and exit"))
  51. (newline)
  52. (show-bug-report-information))
  53. (define %options
  54. ;; Specification of the command-line options.
  55. (cons* (option '(#\h "help") #f #f
  56. (lambda args
  57. (show-help)
  58. (exit 0)))
  59. (option '(#\V "version") #f #f
  60. (lambda args
  61. (show-version-and-exit "guix import json")))
  62. %standard-import-options))
  63. ;;;
  64. ;;; Entry point.
  65. ;;;
  66. (define (guix-import-json . args)
  67. (define (parse-options)
  68. ;; Return the alist of option values.
  69. (parse-command-line args %options (list %default-options)
  70. #:build-options? #f))
  71. (let* ((opts (parse-options))
  72. (args (filter-map (match-lambda
  73. (('argument . value)
  74. value)
  75. (_ #f))
  76. (reverse opts))))
  77. (match args
  78. ((file-name)
  79. (catch 'system-error
  80. (lambda ()
  81. (or (json->code file-name)
  82. (leave (G_ "invalid JSON in file '~a'~%") file-name)))
  83. (lambda args
  84. (leave (G_ "failed to access '~a': ~a~%")
  85. file-name (strerror (system-error-errno args))))))
  86. (()
  87. (leave (G_ "too few arguments~%")))
  88. ((many ...)
  89. (leave (G_ "too many arguments~%"))))))