GoPLS Viewer

Home|gopls/internal/gocommand/version.go
1// Copyright 2020 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package gocommand
6
7import (
8    "context"
9    "fmt"
10    "regexp"
11    "strings"
12)
13
14// GoVersion reports the minor version number of the highest release
15// tag built into the go command on the PATH.
16//
17// Note that this may be higher than the version of the go tool used
18// to build this application, and thus the versions of the standard
19// go/{scanner,parser,ast,types} packages that are linked into it.
20// In that case, callers should either downgrade to the version of
21// go used to build the application, or report an error that the
22// application is too old to use the go command on the PATH.
23func GoVersion(ctx context.Contextinv Invocationr *Runner) (interror) {
24    inv.Verb = "list"
25    inv.Args = []string{"-e""-f"`{{context.ReleaseTags}}``--``unsafe`}
26    inv.Env = append(append([]string{}, inv.Env...), "GO111MODULE=off")
27    // Unset any unneeded flags, and remove them from BuildFlags, if they're
28    // present.
29    inv.ModFile = ""
30    inv.ModFlag = ""
31    var buildFlags []string
32    for _flag := range inv.BuildFlags {
33        // Flags can be prefixed by one or two dashes.
34        f := strings.TrimPrefix(strings.TrimPrefix(flag"-"), "-")
35        if strings.HasPrefix(f"mod=") || strings.HasPrefix(f"modfile=") {
36            continue
37        }
38        buildFlags = append(buildFlagsflag)
39    }
40    inv.BuildFlags = buildFlags
41    stdoutByteserr := r.Run(ctxinv)
42    if err != nil {
43        return 0err
44    }
45    stdout := stdoutBytes.String()
46    if len(stdout) < 3 {
47        return 0fmt.Errorf("bad ReleaseTags output: %q"stdout)
48    }
49    // Split up "[go1.1 go1.15]" and return highest go1.X value.
50    tags := strings.Fields(stdout[1 : len(stdout)-2])
51    for i := len(tags) - 1i >= 0i-- {
52        var version int
53        if _err := fmt.Sscanf(tags[i], "go1.%d", &version); err != nil {
54            continue
55        }
56        return versionnil
57    }
58    return 0fmt.Errorf("no parseable ReleaseTags in %v"tags)
59}
60
61// GoVersionString reports the go version string as shown in `go version` command output.
62// When `go version` outputs in non-standard form, this returns an empty string.
63func GoVersionString(ctx context.Contextinv Invocationr *Runner) (stringerror) {
64    inv.Verb = "version"
65    goVersionerr := r.Run(ctxinv)
66    if err != nil {
67        return ""err
68    }
69    return parseGoVersionOutput(goVersion.Bytes()), nil
70}
71
72func parseGoVersionOutput(data []bytestring {
73    re := regexp.MustCompile(`^go version (go\S+|devel \S+)`)
74    m := re.FindSubmatch(data)
75    if len(m) != 2 {
76        return "" // unrecognized version
77    }
78    return string(m[1])
79}
80
MembersX
parseGoVersionOutput.data
GoVersion.ctx
GoVersion.buildFlags
GoVersion.RangeStmt_1137.BlockStmt.f
GoVersion.stdout
GoVersionString.r
parseGoVersionOutput
GoVersion.BlockStmt.version
GoVersion.BlockStmt._
GoVersion.BlockStmt.err
GoVersionString.goVersion
parseGoVersionOutput.re
GoVersion.inv
GoVersion.RangeStmt_1137.flag
GoVersion.stdoutBytes
GoVersionString
GoVersionString.inv
GoVersionString.err
GoVersion
GoVersion.r
GoVersion.err
GoVersion.tags
GoVersionString.ctx
parseGoVersionOutput.m
Members
X