// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gocommand

import (
	
	
	
	
)

// GoVersion reports the minor version number of the highest release
// tag built into the go command on the PATH.
//
// Note that this may be higher than the version of the go tool used
// to build this application, and thus the versions of the standard
// go/{scanner,parser,ast,types} packages that are linked into it.
// In that case, callers should either downgrade to the version of
// go used to build the application, or report an error that the
// application is too old to use the go command on the PATH.
func ( context.Context,  Invocation,  *Runner) (int, error) {
	.Verb = "list"
	.Args = []string{"-e", "-f", `{{context.ReleaseTags}}`, `--`, `unsafe`}
	.Env = append(append([]string{}, .Env...), "GO111MODULE=off")
	// Unset any unneeded flags, and remove them from BuildFlags, if they're
	// present.
	.ModFile = ""
	.ModFlag = ""
	var  []string
	for ,  := range .BuildFlags {
		// Flags can be prefixed by one or two dashes.
		 := strings.TrimPrefix(strings.TrimPrefix(, "-"), "-")
		if strings.HasPrefix(, "mod=") || strings.HasPrefix(, "modfile=") {
			continue
		}
		 = append(, )
	}
	.BuildFlags = 
	,  := .Run(, )
	if  != nil {
		return 0, 
	}
	 := .String()
	if len() < 3 {
		return 0, fmt.Errorf("bad ReleaseTags output: %q", )
	}
	// Split up "[go1.1 go1.15]" and return highest go1.X value.
	 := strings.Fields([1 : len()-2])
	for  := len() - 1;  >= 0; -- {
		var  int
		if ,  := fmt.Sscanf([], "go1.%d", &);  != nil {
			continue
		}
		return , nil
	}
	return 0, fmt.Errorf("no parseable ReleaseTags in %v", )
}

// GoVersionOutput returns the complete output of the go version command.
func ( context.Context,  Invocation,  *Runner) (string, error) {
	.Verb = "version"
	,  := .Run(, )
	if  != nil {
		return "", 
	}
	return .String(), nil
}

// ParseGoVersionOutput extracts the Go version string
// from the output of the "go version" command.
// Given an unrecognized form, it returns an empty string.
func ( string) string {
	 := regexp.MustCompile(`^go version (go\S+|devel \S+)`)
	 := .FindStringSubmatch()
	if len() != 2 {
		return "" // unrecognized version
	}
	return [1]
}