go.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Katherine Cox-Buday <cox.katherine.e@gmail.com>
  3. ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  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. (define-module (guix scripts import go)
  20. #:use-module (guix ui)
  21. #:use-module (guix utils)
  22. #:use-module (guix scripts)
  23. #:use-module (guix import go)
  24. #:use-module (guix import utils)
  25. #:use-module (guix scripts import)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (srfi srfi-37)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 format)
  32. #:use-module (ice-9 receive)
  33. #:export (guix-import-go))
  34. ;;;
  35. ;;; Command-line options.
  36. ;;;
  37. (define %default-options
  38. '((goproxy . "https://proxy.golang.org")))
  39. (define (show-help)
  40. (display (G_ "Usage: guix import go PACKAGE-PATH[@VERSION]
  41. Import and convert the Go module for PACKAGE-PATH. Optionally, a version
  42. can be specified after the arobas (@) character.\n"))
  43. (display (G_ "
  44. -h, --help display this help and exit"))
  45. (display (G_ "
  46. -r, --recursive generate package expressions for all Go modules
  47. that are not yet in Guix"))
  48. (display (G_ "
  49. -p, --goproxy=GOPROXY specify which goproxy server to use"))
  50. (display (G_ "
  51. --pin-versions use the exact versions of a module's dependencies"))
  52. (newline)
  53. (show-bug-report-information))
  54. (define %options
  55. ;; Specification of the command-line options.
  56. (cons* (option '(#\h "help") #f #f
  57. (lambda args
  58. (show-help)
  59. (exit 0)))
  60. (option '(#\r "recursive") #f #f
  61. (lambda (opt name arg result)
  62. (alist-cons 'recursive #t result)))
  63. (option '(#\p "goproxy") #t #f
  64. (lambda (opt name arg result)
  65. (alist-cons 'goproxy
  66. (string->symbol arg)
  67. (alist-delete 'goproxy result))))
  68. (option '("pin-versions") #f #f
  69. (lambda (opt name arg result)
  70. (alist-cons 'pin-versions? #t result)))
  71. %standard-import-options))
  72. ;;;
  73. ;;; Entry point.
  74. ;;;
  75. (define (guix-import-go . args)
  76. (define (parse-options)
  77. ;; Return the alist of option values.
  78. (args-fold* args %options
  79. (lambda (opt name arg result)
  80. (leave (G_ "~A: unrecognized option~%") name))
  81. (lambda (arg result)
  82. (alist-cons 'argument arg result))
  83. %default-options))
  84. (let* ((opts (parse-options))
  85. (args (filter-map (match-lambda
  86. (('argument . value)
  87. value)
  88. (_ #f))
  89. (reverse opts)))
  90. ;; Append the full version to the package symbol name when using
  91. ;; pinned versions.
  92. (package->definition* (if (assoc-ref opts 'pin-versions?)
  93. (cut package->definition <> 'full)
  94. package->definition)))
  95. (match args
  96. ((spec) ;e.g., github.com/golang/protobuf@v1.3.1
  97. (receive (name version)
  98. (package-name->name+version spec)
  99. (let ((arguments (list name
  100. #:goproxy (assoc-ref opts 'goproxy)
  101. #:version version
  102. #:pin-versions?
  103. (assoc-ref opts 'pin-versions?))))
  104. (if (assoc-ref opts 'recursive)
  105. ;; Recursive import.
  106. (map package->definition*
  107. (apply go-module-recursive-import arguments))
  108. ;; Single import.
  109. (let ((sexp (apply go-module->guix-package arguments)))
  110. (unless sexp
  111. (leave (G_ "failed to download meta-data for module '~a'~%")
  112. module-name))
  113. (package->definition* sexp))))))
  114. (()
  115. (leave (G_ "too few arguments~%")))
  116. ((many ...)
  117. (leave (G_ "too many arguments~%"))))))