go.scm 4.7 KB

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