go.scm 4.7 KB

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