import: go: Correctly report diagnostics upon version mismatch.

* guix/import/go.scm (strip-v-prefix, ensure-v-prefix)
(validate-version): New procedures.
(go-module->guix-package): Use 'validate-version' when defining
'version*'.  Remove 'else' clause in SRFI-34 guard.
This commit is contained in:
Ludovic Courtès 2022-01-09 22:36:38 +01:00
parent d1e3c96759
commit c1db66fa0a
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5

View File

@ -3,7 +3,7 @@
;;; Copyright © 2020 Helio Machado <0x2b3bfa0+guix@googlemail.com>
;;; Copyright © 2021 François Joulaud <francois.joulaud@radiofrance.com>
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2021-2022 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
@ -41,6 +41,7 @@
#:autoload (guix base32) (bytevector->nix-base32-string)
#:autoload (guix build utils) (mkdir-p)
#:autoload (gcrypt hash) (hash-algorithm sha256)
#:use-module (ice-9 format)
#:use-module (ice-9 match)
#:use-module (ice-9 peg)
#:use-module (ice-9 rdelim)
@ -54,6 +55,7 @@
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-26)
#:use-module (srfi srfi-34)
#:use-module (srfi srfi-35)
#:use-module (sxml match)
#:use-module ((sxml xpath) #:renamer (lambda (s)
(if (eq? 'filter s)
@ -569,6 +571,34 @@ control system is being used."
(formatted-message (G_ "unsupported vcs type '~a' for package '~a'")
vcs-type vcs-repo-url)))))
(define (strip-v-prefix version)
"Strip from VERSION the \"v\" prefix that Go uses."
(string-trim version #\v))
(define (ensure-v-prefix version)
"Add a \"v\" prefix to VERSION if it does not already have one."
(if (string-prefix? "v" version)
version
(string-append "v" version)))
(define (validate-version version available-versions module-path)
"Raise an error if VERSION is not among AVAILABLE-VERSIONS, unless VERSION
is a pseudo-version. Return VERSION."
;; Pseudo-versions do not appear in the versions list; skip the
;; following check.
(if (or (go-pseudo-version? version)
(member version available-versions))
version
(raise
(make-compound-condition
(formatted-message (G_ "version ~a of ~a is not available~%")
version module-path available-versions)
(condition (&fix-hint
(hint (format #f (G_ "Pick one of the following \
available versions:~{ ~a~}.")
(map strip-v-prefix
available-versions)))))))))
(define* (go-module->guix-package module-path #:key
(goproxy "https://proxy.golang.org")
version
@ -577,17 +607,11 @@ control system is being used."
The meta-data is fetched from the GOPROXY server and https://pkg.go.dev/.
When VERSION is unspecified, the latest version available is used."
(let* ((available-versions (go-module-available-versions goproxy module-path))
(version* (or version
(go-module-version-string goproxy module-path))) ;latest
;; Elide the "v" prefix Go uses.
(strip-v-prefix (cut string-trim <> #\v))
;; Pseudo-versions do not appear in the versions list; skip the
;; following check.
(_ (unless (or (go-pseudo-version? version*)
(member version* available-versions))
(error (format #f "error: version ~s is not available
hint: use one of the following available versions ~a\n"
version* available-versions))))
(version* (validate-version
(or (and version (ensure-v-prefix version))
(go-module-version-string goproxy module-path)) ;latest
available-versions
module-path))
(content (fetch-go.mod goproxy module-path version*))
(dependencies+versions (go.mod-requirements (parse-go.mod content)))
(dependencies (if pin-versions?
@ -628,10 +652,10 @@ hint: use one of the following available versions ~a\n"
(synopsis ,synopsis)
(description ,(and=> description beautify-description))
(license ,(match (list->licenses licenses)
(() #f) ;unknown license
((license) ;a single license
(() #f) ;unknown license
((license) ;a single license
license)
((license ...) ;a list of licenses
((license ...) ;a list of licenses
`(list ,@license)))))
(if pin-versions?
dependencies+versions
@ -651,12 +675,6 @@ This package and its dependencies won't be imported.~%")
(uri->string (http-get-error-uri c))
(http-get-error-code c)
(http-get-error-reason c))
(values #f '()))
(else
(warning (G_ "Failed to import package ~s.
reason: ~s.~%")
package-name
(exception-args c))
(values #f '())))
(apply go-module->guix-package args)))))