[MM-27535] User invite limits for MM Cloud (#15197)
* Add a config for MM User Limit * Adding graceful errors for if an administrator invites people passed their user limit * Including changed vendor files * Adding unit test * Fix a bug * Push up working tests (Thanks Joram) * Add more cases, clean up logs in code * One more case * Refactoring based on PR comments * Updating i18n * Some changes based on PR review * Remove a comment * Bring back some translations that were somehow removed Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
89
vendor/golang.org/x/tools/go/packages/golist.go
сгенерированный
поставляемый
89
vendor/golang.org/x/tools/go/packages/golist.go
сгенерированный
поставляемый
@@ -89,6 +89,10 @@ type golistState struct {
|
||||
rootDirsError error
|
||||
rootDirs map[string]string
|
||||
|
||||
goVersionOnce sync.Once
|
||||
goVersionError error
|
||||
goVersion string // third field of 'go version'
|
||||
|
||||
// vendorDirs caches the (non)existence of vendor directories.
|
||||
vendorDirs map[string]bool
|
||||
}
|
||||
@@ -635,6 +639,39 @@ func (state *golistState) createDriverResponse(words ...string) (*driverResponse
|
||||
pkg.CompiledGoFiles = pkg.GoFiles
|
||||
}
|
||||
|
||||
// Temporary work-around for golang/go#39986. Parse filenames out of
|
||||
// error messages. This happens if there are unrecoverable syntax
|
||||
// errors in the source, so we can't match on a specific error message.
|
||||
if err := p.Error; err != nil && state.shouldAddFilenameFromError(p) {
|
||||
addFilenameFromPos := func(pos string) bool {
|
||||
split := strings.Split(pos, ":")
|
||||
if len(split) < 1 {
|
||||
return false
|
||||
}
|
||||
filename := strings.TrimSpace(split[0])
|
||||
if filename == "" {
|
||||
return false
|
||||
}
|
||||
if !filepath.IsAbs(filename) {
|
||||
filename = filepath.Join(state.cfg.Dir, filename)
|
||||
}
|
||||
info, _ := os.Stat(filename)
|
||||
if info == nil {
|
||||
return false
|
||||
}
|
||||
pkg.CompiledGoFiles = append(pkg.CompiledGoFiles, filename)
|
||||
pkg.GoFiles = append(pkg.GoFiles, filename)
|
||||
return true
|
||||
}
|
||||
found := addFilenameFromPos(err.Pos)
|
||||
// In some cases, go list only reports the error position in the
|
||||
// error text, not the error position. One such case is when the
|
||||
// file's package name is a keyword (see golang.org/issue/39763).
|
||||
if !found {
|
||||
addFilenameFromPos(err.Err)
|
||||
}
|
||||
}
|
||||
|
||||
if p.Error != nil {
|
||||
msg := strings.TrimSpace(p.Error.Err) // Trim to work around golang.org/issue/32363.
|
||||
// Address golang.org/issue/35964 by appending import stack to error message.
|
||||
@@ -664,6 +701,58 @@ func (state *golistState) createDriverResponse(words ...string) (*driverResponse
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (state *golistState) shouldAddFilenameFromError(p *jsonPackage) bool {
|
||||
if len(p.GoFiles) > 0 || len(p.CompiledGoFiles) > 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
goV, err := state.getGoVersion()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// On Go 1.14 and earlier, only add filenames from errors if the import stack is empty.
|
||||
// The import stack behaves differently for these versions than newer Go versions.
|
||||
if strings.HasPrefix(goV, "go1.13") || strings.HasPrefix(goV, "go1.14") {
|
||||
return len(p.Error.ImportStack) == 0
|
||||
}
|
||||
|
||||
// On Go 1.15 and later, only parse filenames out of error if there's no import stack,
|
||||
// or the current package is at the top of the import stack. This is not guaranteed
|
||||
// to work perfectly, but should avoid some cases where files in errors don't belong to this
|
||||
// package.
|
||||
return len(p.Error.ImportStack) == 0 || p.Error.ImportStack[len(p.Error.ImportStack)-1] == p.ImportPath
|
||||
}
|
||||
|
||||
func (state *golistState) getGoVersion() (string, error) {
|
||||
state.goVersionOnce.Do(func() {
|
||||
var b *bytes.Buffer
|
||||
// Invoke go version. Don't use invokeGo because it will supply build flags, and
|
||||
// go version doesn't expect build flags.
|
||||
inv := gocommand.Invocation{
|
||||
Verb: "version",
|
||||
Env: state.cfg.Env,
|
||||
Logf: state.cfg.Logf,
|
||||
}
|
||||
gocmdRunner := state.cfg.gocmdRunner
|
||||
if gocmdRunner == nil {
|
||||
gocmdRunner = &gocommand.Runner{}
|
||||
}
|
||||
b, _, _, state.goVersionError = gocmdRunner.RunRaw(state.cfg.Context, inv)
|
||||
if state.goVersionError != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sp := strings.Split(b.String(), " ")
|
||||
if len(sp) < 3 {
|
||||
state.goVersionError = fmt.Errorf("go version output: expected 'go version <version>', got '%s'", b.String())
|
||||
return
|
||||
}
|
||||
state.goVersion = sp[2]
|
||||
})
|
||||
return state.goVersion, state.goVersionError
|
||||
}
|
||||
|
||||
// getPkgPath finds the package path of a directory if it's relative to a root directory.
|
||||
func (state *golistState) getPkgPath(dir string) (string, bool, error) {
|
||||
absDir, err := filepath.Abs(dir)
|
||||
|
||||
66
vendor/golang.org/x/tools/go/packages/golist_overlay.go
сгенерированный
поставляемый
66
vendor/golang.org/x/tools/go/packages/golist_overlay.go
сгенерированный
поставляемый
@@ -102,8 +102,11 @@ func (state *golistState) processGolistOverlay(response *responseDeduper) (modif
|
||||
}
|
||||
}
|
||||
}
|
||||
// The overlay could have included an entirely new package.
|
||||
if pkg == nil {
|
||||
// The overlay could have included an entirely new package or an
|
||||
// ad-hoc package. An ad-hoc package is one that we have manually
|
||||
// constructed from inadequate `go list` results for a file= query.
|
||||
// It will have the ID command-line-arguments.
|
||||
if pkg == nil || pkg.ID == "command-line-arguments" {
|
||||
// Try to find the module or gopath dir the file is contained in.
|
||||
// Then for modules, add the module opath to the beginning.
|
||||
pkgPath, ok, err := state.getPkgPath(dir)
|
||||
@@ -127,35 +130,40 @@ func (state *golistState) processGolistOverlay(response *responseDeduper) (modif
|
||||
id = fmt.Sprintf("%s [%s.test]", pkgPath, pkgPath)
|
||||
}
|
||||
}
|
||||
// Try to reclaim a package with the same ID, if it exists in the response.
|
||||
for _, p := range response.dr.Packages {
|
||||
if reclaimPackage(p, id, opath, contents) {
|
||||
pkg = p
|
||||
break
|
||||
}
|
||||
}
|
||||
// Otherwise, create a new package.
|
||||
if pkg == nil {
|
||||
pkg = &Package{
|
||||
PkgPath: pkgPath,
|
||||
ID: id,
|
||||
Name: pkgName,
|
||||
Imports: make(map[string]*Package),
|
||||
}
|
||||
response.addPackage(pkg)
|
||||
havePkgs[pkg.PkgPath] = id
|
||||
// Add the production package's sources for a test variant.
|
||||
if isTestFile && !isXTest && testVariantOf != nil {
|
||||
pkg.GoFiles = append(pkg.GoFiles, testVariantOf.GoFiles...)
|
||||
pkg.CompiledGoFiles = append(pkg.CompiledGoFiles, testVariantOf.CompiledGoFiles...)
|
||||
// Add the package under test and its imports to the test variant.
|
||||
pkg.forTest = testVariantOf.PkgPath
|
||||
for k, v := range testVariantOf.Imports {
|
||||
pkg.Imports[k] = &Package{ID: v.ID}
|
||||
if pkg != nil {
|
||||
// TODO(rstambler): We should change the package's path and ID
|
||||
// here. The only issue is that this messes with the roots.
|
||||
} else {
|
||||
// Try to reclaim a package with the same ID, if it exists in the response.
|
||||
for _, p := range response.dr.Packages {
|
||||
if reclaimPackage(p, id, opath, contents) {
|
||||
pkg = p
|
||||
break
|
||||
}
|
||||
}
|
||||
if isXTest {
|
||||
pkg.forTest = forTest
|
||||
// Otherwise, create a new package.
|
||||
if pkg == nil {
|
||||
pkg = &Package{
|
||||
PkgPath: pkgPath,
|
||||
ID: id,
|
||||
Name: pkgName,
|
||||
Imports: make(map[string]*Package),
|
||||
}
|
||||
response.addPackage(pkg)
|
||||
havePkgs[pkg.PkgPath] = id
|
||||
// Add the production package's sources for a test variant.
|
||||
if isTestFile && !isXTest && testVariantOf != nil {
|
||||
pkg.GoFiles = append(pkg.GoFiles, testVariantOf.GoFiles...)
|
||||
pkg.CompiledGoFiles = append(pkg.CompiledGoFiles, testVariantOf.CompiledGoFiles...)
|
||||
// Add the package under test and its imports to the test variant.
|
||||
pkg.forTest = testVariantOf.PkgPath
|
||||
for k, v := range testVariantOf.Imports {
|
||||
pkg.Imports[k] = &Package{ID: v.ID}
|
||||
}
|
||||
}
|
||||
if isXTest {
|
||||
pkg.forTest = forTest
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
vendor/golang.org/x/tools/imports/forward.go
сгенерированный
поставляемый
27
vendor/golang.org/x/tools/imports/forward.go
сгенерированный
поставляемый
@@ -3,8 +3,10 @@
|
||||
package imports // import "golang.org/x/tools/imports"
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"golang.org/x/tools/internal/gocommand"
|
||||
intimp "golang.org/x/tools/internal/imports"
|
||||
)
|
||||
|
||||
@@ -29,25 +31,34 @@ var Debug = false
|
||||
var LocalPrefix string
|
||||
|
||||
// Process formats and adjusts imports for the provided file.
|
||||
// If opt is nil the defaults are used.
|
||||
// If opt is nil the defaults are used, and if src is nil the source
|
||||
// is read from the filesystem.
|
||||
//
|
||||
// Note that filename's directory influences which imports can be chosen,
|
||||
// so it is important that filename be accurate.
|
||||
// To process data ``as if'' it were in filename, pass the data as a non-nil src.
|
||||
func Process(filename string, src []byte, opt *Options) ([]byte, error) {
|
||||
var err error
|
||||
if src == nil {
|
||||
src, err = ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if opt == nil {
|
||||
opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
|
||||
}
|
||||
intopt := &intimp.Options{
|
||||
Env: &intimp.ProcessEnv{
|
||||
LocalPrefix: LocalPrefix,
|
||||
GocmdRunner: &gocommand.Runner{},
|
||||
},
|
||||
AllErrors: opt.AllErrors,
|
||||
Comments: opt.Comments,
|
||||
FormatOnly: opt.FormatOnly,
|
||||
Fragment: opt.Fragment,
|
||||
TabIndent: opt.TabIndent,
|
||||
TabWidth: opt.TabWidth,
|
||||
LocalPrefix: LocalPrefix,
|
||||
AllErrors: opt.AllErrors,
|
||||
Comments: opt.Comments,
|
||||
FormatOnly: opt.FormatOnly,
|
||||
Fragment: opt.Fragment,
|
||||
TabIndent: opt.TabIndent,
|
||||
TabWidth: opt.TabWidth,
|
||||
}
|
||||
if Debug {
|
||||
intopt.Env.Logf = log.Printf
|
||||
|
||||
73
vendor/golang.org/x/tools/internal/imports/fix.go
сгенерированный
поставляемый
73
vendor/golang.org/x/tools/internal/imports/fix.go
сгенерированный
поставляемый
@@ -32,25 +32,25 @@ import (
|
||||
|
||||
// importToGroup is a list of functions which map from an import path to
|
||||
// a group number.
|
||||
var importToGroup = []func(env *ProcessEnv, importPath string) (num int, ok bool){
|
||||
func(env *ProcessEnv, importPath string) (num int, ok bool) {
|
||||
if env.LocalPrefix == "" {
|
||||
var importToGroup = []func(localPrefix, importPath string) (num int, ok bool){
|
||||
func(localPrefix, importPath string) (num int, ok bool) {
|
||||
if localPrefix == "" {
|
||||
return
|
||||
}
|
||||
for _, p := range strings.Split(env.LocalPrefix, ",") {
|
||||
for _, p := range strings.Split(localPrefix, ",") {
|
||||
if strings.HasPrefix(importPath, p) || strings.TrimSuffix(p, "/") == importPath {
|
||||
return 3, true
|
||||
}
|
||||
}
|
||||
return
|
||||
},
|
||||
func(_ *ProcessEnv, importPath string) (num int, ok bool) {
|
||||
func(_, importPath string) (num int, ok bool) {
|
||||
if strings.HasPrefix(importPath, "appengine") {
|
||||
return 2, true
|
||||
}
|
||||
return
|
||||
},
|
||||
func(_ *ProcessEnv, importPath string) (num int, ok bool) {
|
||||
func(_, importPath string) (num int, ok bool) {
|
||||
firstComponent := strings.Split(importPath, "/")[0]
|
||||
if strings.Contains(firstComponent, ".") {
|
||||
return 1, true
|
||||
@@ -59,9 +59,9 @@ var importToGroup = []func(env *ProcessEnv, importPath string) (num int, ok bool
|
||||
},
|
||||
}
|
||||
|
||||
func importGroup(env *ProcessEnv, importPath string) int {
|
||||
func importGroup(localPrefix, importPath string) int {
|
||||
for _, fn := range importToGroup {
|
||||
if n, ok := fn(env, importPath); ok {
|
||||
if n, ok := fn(localPrefix, importPath); ok {
|
||||
return n
|
||||
}
|
||||
}
|
||||
@@ -278,7 +278,12 @@ func (p *pass) loadPackageNames(imports []*ImportInfo) error {
|
||||
unknown = append(unknown, imp.ImportPath)
|
||||
}
|
||||
|
||||
names, err := p.env.GetResolver().loadPackageNames(unknown, p.srcDir)
|
||||
resolver, err := p.env.GetResolver()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
names, err := resolver.loadPackageNames(unknown, p.srcDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -640,15 +645,23 @@ func getCandidatePkgs(ctx context.Context, wrappedCallback *scanCallback, filena
|
||||
wrappedCallback.exportsLoaded(pkg, exports)
|
||||
},
|
||||
}
|
||||
return env.GetResolver().scan(ctx, scanFilter)
|
||||
resolver, err := env.GetResolver()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return resolver.scan(ctx, scanFilter)
|
||||
}
|
||||
|
||||
func ScoreImportPaths(ctx context.Context, env *ProcessEnv, paths []string) map[string]int {
|
||||
func ScoreImportPaths(ctx context.Context, env *ProcessEnv, paths []string) (map[string]int, error) {
|
||||
result := make(map[string]int)
|
||||
for _, path := range paths {
|
||||
result[path] = env.GetResolver().scoreImportPath(ctx, path)
|
||||
resolver, err := env.GetResolver()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result
|
||||
for _, path := range paths {
|
||||
result[path] = resolver.scoreImportPath(ctx, path)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func PrimeCache(ctx context.Context, env *ProcessEnv) error {
|
||||
@@ -674,8 +687,9 @@ func candidateImportName(pkg *pkg) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// getAllCandidates gets all of the candidates to be imported, regardless of if they are needed.
|
||||
func getAllCandidates(ctx context.Context, wrapped func(ImportFix), searchPrefix, filename, filePkg string, env *ProcessEnv) error {
|
||||
// GetAllCandidates gets all of the packages starting with prefix that can be
|
||||
// imported by filename, sorted by import path.
|
||||
func GetAllCandidates(ctx context.Context, wrapped func(ImportFix), searchPrefix, filename, filePkg string, env *ProcessEnv) error {
|
||||
callback := &scanCallback{
|
||||
rootFound: func(gopathwalk.Root) bool {
|
||||
return true
|
||||
@@ -714,7 +728,8 @@ type PackageExport struct {
|
||||
Exports []string
|
||||
}
|
||||
|
||||
func getPackageExports(ctx context.Context, wrapped func(PackageExport), searchPkg, filename, filePkg string, env *ProcessEnv) error {
|
||||
// GetPackageExports returns all known packages with name pkg and their exports.
|
||||
func GetPackageExports(ctx context.Context, wrapped func(PackageExport), searchPkg, filename, filePkg string, env *ProcessEnv) error {
|
||||
callback := &scanCallback{
|
||||
rootFound: func(gopathwalk.Root) bool {
|
||||
return true
|
||||
@@ -749,8 +764,6 @@ var RequiredGoEnvVars = []string{"GO111MODULE", "GOFLAGS", "GOINSECURE", "GOMOD"
|
||||
// ProcessEnv contains environment variables and settings that affect the use of
|
||||
// the go command, the go/build package, etc.
|
||||
type ProcessEnv struct {
|
||||
LocalPrefix string
|
||||
|
||||
GocmdRunner *gocommand.Runner
|
||||
|
||||
BuildFlags []string
|
||||
@@ -830,16 +843,19 @@ func (e *ProcessEnv) env() []string {
|
||||
return env
|
||||
}
|
||||
|
||||
func (e *ProcessEnv) GetResolver() Resolver {
|
||||
func (e *ProcessEnv) GetResolver() (Resolver, error) {
|
||||
if e.resolver != nil {
|
||||
return e.resolver
|
||||
return e.resolver, nil
|
||||
}
|
||||
if err := e.init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(e.Env["GOMOD"]) == 0 {
|
||||
e.resolver = newGopathResolver(e)
|
||||
return e.resolver
|
||||
return e.resolver, nil
|
||||
}
|
||||
e.resolver = newModuleResolver(e)
|
||||
return e.resolver
|
||||
return e.resolver, nil
|
||||
}
|
||||
|
||||
func (e *ProcessEnv) buildContext() *build.Context {
|
||||
@@ -964,10 +980,13 @@ func addExternalCandidates(pass *pass, refs references, filename string) error {
|
||||
return false // We'll do our own loading after we sort.
|
||||
},
|
||||
}
|
||||
err := pass.env.GetResolver().scan(context.Background(), callback)
|
||||
resolver, err := pass.env.GetResolver()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = resolver.scan(context.Background(), callback); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Search for imports matching potential package references.
|
||||
type result struct {
|
||||
@@ -1408,6 +1427,10 @@ func findImport(ctx context.Context, pass *pass, candidates []pkgDistance, pkgNa
|
||||
pass.env.Logf("%s candidate %d/%d: %v in %v", pkgName, i+1, len(candidates), c.pkg.importPathShort, c.pkg.dir)
|
||||
}
|
||||
}
|
||||
resolver, err := pass.env.GetResolver()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Collect exports for packages with matching names.
|
||||
rescv := make([]chan *pkg, len(candidates))
|
||||
@@ -1446,7 +1469,7 @@ func findImport(ctx context.Context, pass *pass, candidates []pkgDistance, pkgNa
|
||||
}
|
||||
// If we're an x_test, load the package under test's test variant.
|
||||
includeTest := strings.HasSuffix(pass.f.Name.Name, "_test") && c.pkg.dir == pass.srcDir
|
||||
_, exports, err := pass.env.GetResolver().loadExports(ctx, c.pkg, includeTest)
|
||||
_, exports, err := resolver.loadExports(ctx, c.pkg, includeTest)
|
||||
if err != nil {
|
||||
if pass.env.Logf != nil {
|
||||
pass.env.Logf("loading exports in dir %s (seeking package %s): %v", c.pkg.dir, pkgName, err)
|
||||
|
||||
88
vendor/golang.org/x/tools/internal/imports/imports.go
сгенерированный
поставляемый
88
vendor/golang.org/x/tools/internal/imports/imports.go
сгенерированный
поставляемый
@@ -11,7 +11,6 @@ package imports
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
@@ -19,19 +18,22 @@ import (
|
||||
"go/printer"
|
||||
"go/token"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/ast/astutil"
|
||||
"golang.org/x/tools/internal/gocommand"
|
||||
)
|
||||
|
||||
// Options is golang.org/x/tools/imports.Options with extra internal-only options.
|
||||
type Options struct {
|
||||
Env *ProcessEnv // The environment to use. Note: this contains the cached module and filesystem state.
|
||||
|
||||
// LocalPrefix is a comma-separated string of import path prefixes, which, if
|
||||
// set, instructs Process to sort the import paths with the given prefixes
|
||||
// into another group after 3rd-party packages.
|
||||
LocalPrefix string
|
||||
|
||||
Fragment bool // Accept fragment of a source file (no package statement)
|
||||
AllErrors bool // Report all errors (not just the first 10 on different lines)
|
||||
|
||||
@@ -42,13 +44,8 @@ type Options struct {
|
||||
FormatOnly bool // Disable the insertion and deletion of imports
|
||||
}
|
||||
|
||||
// Process implements golang.org/x/tools/imports.Process with explicit context in env.
|
||||
// Process implements golang.org/x/tools/imports.Process with explicit context in opt.Env.
|
||||
func Process(filename string, src []byte, opt *Options) (formatted []byte, err error) {
|
||||
src, opt, err = initialize(filename, src, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileSet := token.NewFileSet()
|
||||
file, adjust, err := parse(fileSet, filename, src, opt)
|
||||
if err != nil {
|
||||
@@ -64,16 +61,12 @@ func Process(filename string, src []byte, opt *Options) (formatted []byte, err e
|
||||
}
|
||||
|
||||
// FixImports returns a list of fixes to the imports that, when applied,
|
||||
// will leave the imports in the same state as Process.
|
||||
// will leave the imports in the same state as Process. src and opt must
|
||||
// be specified.
|
||||
//
|
||||
// Note that filename's directory influences which imports can be chosen,
|
||||
// so it is important that filename be accurate.
|
||||
func FixImports(filename string, src []byte, opt *Options) (fixes []*ImportFix, err error) {
|
||||
src, opt, err = initialize(filename, src, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileSet := token.NewFileSet()
|
||||
file, _, err := parse(fileSet, filename, src, opt)
|
||||
if err != nil {
|
||||
@@ -84,13 +77,9 @@ func FixImports(filename string, src []byte, opt *Options) (fixes []*ImportFix,
|
||||
}
|
||||
|
||||
// ApplyFixes applies all of the fixes to the file and formats it. extraMode
|
||||
// is added in when parsing the file.
|
||||
// is added in when parsing the file. src and opts must be specified, but no
|
||||
// env is needed.
|
||||
func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options, extraMode parser.Mode) (formatted []byte, err error) {
|
||||
src, opt, err = initialize(filename, src, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Don't use parse() -- we don't care about fragments or statement lists
|
||||
// here, and we need to work with unparseable files.
|
||||
fileSet := token.NewFileSet()
|
||||
@@ -114,60 +103,9 @@ func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options, e
|
||||
return formatFile(fileSet, file, src, nil, opt)
|
||||
}
|
||||
|
||||
// GetAllCandidates gets all of the packages starting with prefix that can be
|
||||
// imported by filename, sorted by import path.
|
||||
func GetAllCandidates(ctx context.Context, callback func(ImportFix), searchPrefix, filename, filePkg string, opt *Options) error {
|
||||
_, opt, err := initialize(filename, []byte{}, opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return getAllCandidates(ctx, callback, searchPrefix, filename, filePkg, opt.Env)
|
||||
}
|
||||
|
||||
// GetPackageExports returns all known packages with name pkg and their exports.
|
||||
func GetPackageExports(ctx context.Context, callback func(PackageExport), searchPkg, filename, filePkg string, opt *Options) error {
|
||||
_, opt, err := initialize(filename, []byte{}, opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return getPackageExports(ctx, callback, searchPkg, filename, filePkg, opt.Env)
|
||||
}
|
||||
|
||||
// initialize sets the values for opt and src.
|
||||
// If they are provided, they are not changed. Otherwise opt is set to the
|
||||
// default values and src is read from the file system.
|
||||
func initialize(filename string, src []byte, opt *Options) ([]byte, *Options, error) {
|
||||
// Use defaults if opt is nil.
|
||||
if opt == nil {
|
||||
opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
|
||||
}
|
||||
|
||||
// Set the env if the user has not provided it.
|
||||
if opt.Env == nil {
|
||||
opt.Env = &ProcessEnv{}
|
||||
}
|
||||
// Set the gocmdRunner if the user has not provided it.
|
||||
if opt.Env.GocmdRunner == nil {
|
||||
opt.Env.GocmdRunner = &gocommand.Runner{}
|
||||
}
|
||||
if err := opt.Env.init(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if src == nil {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
src = b
|
||||
}
|
||||
|
||||
return src, opt, nil
|
||||
}
|
||||
|
||||
func formatFile(fileSet *token.FileSet, file *ast.File, src []byte, adjust func(orig []byte, src []byte) []byte, opt *Options) ([]byte, error) {
|
||||
mergeImports(opt.Env, fileSet, file)
|
||||
sortImports(opt.Env, fileSet, file)
|
||||
mergeImports(fileSet, file)
|
||||
sortImports(opt.LocalPrefix, fileSet, file)
|
||||
imps := astutil.Imports(fileSet, file)
|
||||
var spacesBefore []string // import paths we need spaces before
|
||||
for _, impSection := range imps {
|
||||
@@ -178,7 +116,7 @@ func formatFile(fileSet *token.FileSet, file *ast.File, src []byte, adjust func(
|
||||
lastGroup := -1
|
||||
for _, importSpec := range impSection {
|
||||
importPath, _ := strconv.Unquote(importSpec.Path.Value)
|
||||
groupNum := importGroup(opt.Env, importPath)
|
||||
groupNum := importGroup(opt.LocalPrefix, importPath)
|
||||
if groupNum != lastGroup && lastGroup != -1 {
|
||||
spacesBefore = append(spacesBefore, importPath)
|
||||
}
|
||||
|
||||
20
vendor/golang.org/x/tools/internal/imports/sortimports.go
сгенерированный
поставляемый
20
vendor/golang.org/x/tools/internal/imports/sortimports.go
сгенерированный
поставляемый
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
// sortImports sorts runs of consecutive import lines in import blocks in f.
|
||||
// It also removes duplicate imports when it is possible to do so without data loss.
|
||||
func sortImports(env *ProcessEnv, fset *token.FileSet, f *ast.File) {
|
||||
func sortImports(localPrefix string, fset *token.FileSet, f *ast.File) {
|
||||
for i, d := range f.Decls {
|
||||
d, ok := d.(*ast.GenDecl)
|
||||
if !ok || d.Tok != token.IMPORT {
|
||||
@@ -40,11 +40,11 @@ func sortImports(env *ProcessEnv, fset *token.FileSet, f *ast.File) {
|
||||
for j, s := range d.Specs {
|
||||
if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
|
||||
// j begins a new run. End this one.
|
||||
specs = append(specs, sortSpecs(env, fset, f, d.Specs[i:j])...)
|
||||
specs = append(specs, sortSpecs(localPrefix, fset, f, d.Specs[i:j])...)
|
||||
i = j
|
||||
}
|
||||
}
|
||||
specs = append(specs, sortSpecs(env, fset, f, d.Specs[i:])...)
|
||||
specs = append(specs, sortSpecs(localPrefix, fset, f, d.Specs[i:])...)
|
||||
d.Specs = specs
|
||||
|
||||
// Deduping can leave a blank line before the rparen; clean that up.
|
||||
@@ -60,7 +60,7 @@ func sortImports(env *ProcessEnv, fset *token.FileSet, f *ast.File) {
|
||||
|
||||
// mergeImports merges all the import declarations into the first one.
|
||||
// Taken from golang.org/x/tools/ast/astutil.
|
||||
func mergeImports(env *ProcessEnv, fset *token.FileSet, f *ast.File) {
|
||||
func mergeImports(fset *token.FileSet, f *ast.File) {
|
||||
if len(f.Decls) <= 1 {
|
||||
return
|
||||
}
|
||||
@@ -142,7 +142,7 @@ type posSpan struct {
|
||||
End token.Pos
|
||||
}
|
||||
|
||||
func sortSpecs(env *ProcessEnv, fset *token.FileSet, f *ast.File, specs []ast.Spec) []ast.Spec {
|
||||
func sortSpecs(localPrefix string, fset *token.FileSet, f *ast.File, specs []ast.Spec) []ast.Spec {
|
||||
// Can't short-circuit here even if specs are already sorted,
|
||||
// since they might yet need deduplication.
|
||||
// A lone import, however, may be safely ignored.
|
||||
@@ -191,7 +191,7 @@ func sortSpecs(env *ProcessEnv, fset *token.FileSet, f *ast.File, specs []ast.Sp
|
||||
// Reassign the import paths to have the same position sequence.
|
||||
// Reassign each comment to abut the end of its spec.
|
||||
// Sort the comments by new position.
|
||||
sort.Sort(byImportSpec{env, specs})
|
||||
sort.Sort(byImportSpec{localPrefix, specs})
|
||||
|
||||
// Dedup. Thanks to our sorting, we can just consider
|
||||
// adjacent pairs of imports.
|
||||
@@ -245,8 +245,8 @@ func sortSpecs(env *ProcessEnv, fset *token.FileSet, f *ast.File, specs []ast.Sp
|
||||
}
|
||||
|
||||
type byImportSpec struct {
|
||||
env *ProcessEnv
|
||||
specs []ast.Spec // slice of *ast.ImportSpec
|
||||
localPrefix string
|
||||
specs []ast.Spec // slice of *ast.ImportSpec
|
||||
}
|
||||
|
||||
func (x byImportSpec) Len() int { return len(x.specs) }
|
||||
@@ -255,8 +255,8 @@ func (x byImportSpec) Less(i, j int) bool {
|
||||
ipath := importPath(x.specs[i])
|
||||
jpath := importPath(x.specs[j])
|
||||
|
||||
igroup := importGroup(x.env, ipath)
|
||||
jgroup := importGroup(x.env, jpath)
|
||||
igroup := importGroup(x.localPrefix, ipath)
|
||||
jgroup := importGroup(x.localPrefix, jpath)
|
||||
if igroup != jgroup {
|
||||
return igroup < jgroup
|
||||
}
|
||||
|
||||
52
vendor/golang.org/x/tools/internal/imports/zstdlib.go
сгенерированный
поставляемый
52
vendor/golang.org/x/tools/internal/imports/zstdlib.go
сгенерированный
поставляемый
@@ -56,6 +56,7 @@ var stdlib = map[string][]string{
|
||||
},
|
||||
"bufio": []string{
|
||||
"ErrAdvanceTooFar",
|
||||
"ErrBadReadCount",
|
||||
"ErrBufferFull",
|
||||
"ErrFinalToken",
|
||||
"ErrInvalidUnreadByte",
|
||||
@@ -303,7 +304,9 @@ var stdlib = map[string][]string{
|
||||
"PrivateKey",
|
||||
"PublicKey",
|
||||
"Sign",
|
||||
"SignASN1",
|
||||
"Verify",
|
||||
"VerifyASN1",
|
||||
},
|
||||
"crypto/ed25519": []string{
|
||||
"GenerateKey",
|
||||
@@ -322,11 +325,13 @@ var stdlib = map[string][]string{
|
||||
"CurveParams",
|
||||
"GenerateKey",
|
||||
"Marshal",
|
||||
"MarshalCompressed",
|
||||
"P224",
|
||||
"P256",
|
||||
"P384",
|
||||
"P521",
|
||||
"Unmarshal",
|
||||
"UnmarshalCompressed",
|
||||
},
|
||||
"crypto/hmac": []string{
|
||||
"Equal",
|
||||
@@ -432,6 +437,7 @@ var stdlib = map[string][]string{
|
||||
"CurveP521",
|
||||
"Dial",
|
||||
"DialWithDialer",
|
||||
"Dialer",
|
||||
"ECDSAWithP256AndSHA256",
|
||||
"ECDSAWithP384AndSHA384",
|
||||
"ECDSAWithP521AndSHA512",
|
||||
@@ -507,6 +513,7 @@ var stdlib = map[string][]string{
|
||||
"ConstraintViolationError",
|
||||
"CreateCertificate",
|
||||
"CreateCertificateRequest",
|
||||
"CreateRevocationList",
|
||||
"DSA",
|
||||
"DSAWithSHA1",
|
||||
"DSAWithSHA256",
|
||||
@@ -581,6 +588,7 @@ var stdlib = map[string][]string{
|
||||
"PublicKeyAlgorithm",
|
||||
"PureEd25519",
|
||||
"RSA",
|
||||
"RevocationList",
|
||||
"SHA1WithRSA",
|
||||
"SHA256WithRSA",
|
||||
"SHA256WithRSAPSS",
|
||||
@@ -694,6 +702,7 @@ var stdlib = map[string][]string{
|
||||
"String",
|
||||
"Tx",
|
||||
"TxOptions",
|
||||
"Validator",
|
||||
"Value",
|
||||
"ValueConverter",
|
||||
"Valuer",
|
||||
@@ -2349,6 +2358,27 @@ var stdlib = map[string][]string{
|
||||
"IMAGE_DIRECTORY_ENTRY_RESOURCE",
|
||||
"IMAGE_DIRECTORY_ENTRY_SECURITY",
|
||||
"IMAGE_DIRECTORY_ENTRY_TLS",
|
||||
"IMAGE_DLLCHARACTERISTICS_APPCONTAINER",
|
||||
"IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE",
|
||||
"IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY",
|
||||
"IMAGE_DLLCHARACTERISTICS_GUARD_CF",
|
||||
"IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA",
|
||||
"IMAGE_DLLCHARACTERISTICS_NO_BIND",
|
||||
"IMAGE_DLLCHARACTERISTICS_NO_ISOLATION",
|
||||
"IMAGE_DLLCHARACTERISTICS_NO_SEH",
|
||||
"IMAGE_DLLCHARACTERISTICS_NX_COMPAT",
|
||||
"IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE",
|
||||
"IMAGE_DLLCHARACTERISTICS_WDM_DRIVER",
|
||||
"IMAGE_FILE_32BIT_MACHINE",
|
||||
"IMAGE_FILE_AGGRESIVE_WS_TRIM",
|
||||
"IMAGE_FILE_BYTES_REVERSED_HI",
|
||||
"IMAGE_FILE_BYTES_REVERSED_LO",
|
||||
"IMAGE_FILE_DEBUG_STRIPPED",
|
||||
"IMAGE_FILE_DLL",
|
||||
"IMAGE_FILE_EXECUTABLE_IMAGE",
|
||||
"IMAGE_FILE_LARGE_ADDRESS_AWARE",
|
||||
"IMAGE_FILE_LINE_NUMS_STRIPPED",
|
||||
"IMAGE_FILE_LOCAL_SYMS_STRIPPED",
|
||||
"IMAGE_FILE_MACHINE_AM33",
|
||||
"IMAGE_FILE_MACHINE_AMD64",
|
||||
"IMAGE_FILE_MACHINE_ARM",
|
||||
@@ -2371,6 +2401,25 @@ var stdlib = map[string][]string{
|
||||
"IMAGE_FILE_MACHINE_THUMB",
|
||||
"IMAGE_FILE_MACHINE_UNKNOWN",
|
||||
"IMAGE_FILE_MACHINE_WCEMIPSV2",
|
||||
"IMAGE_FILE_NET_RUN_FROM_SWAP",
|
||||
"IMAGE_FILE_RELOCS_STRIPPED",
|
||||
"IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP",
|
||||
"IMAGE_FILE_SYSTEM",
|
||||
"IMAGE_FILE_UP_SYSTEM_ONLY",
|
||||
"IMAGE_SUBSYSTEM_EFI_APPLICATION",
|
||||
"IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER",
|
||||
"IMAGE_SUBSYSTEM_EFI_ROM",
|
||||
"IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER",
|
||||
"IMAGE_SUBSYSTEM_NATIVE",
|
||||
"IMAGE_SUBSYSTEM_NATIVE_WINDOWS",
|
||||
"IMAGE_SUBSYSTEM_OS2_CUI",
|
||||
"IMAGE_SUBSYSTEM_POSIX_CUI",
|
||||
"IMAGE_SUBSYSTEM_UNKNOWN",
|
||||
"IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION",
|
||||
"IMAGE_SUBSYSTEM_WINDOWS_CE_GUI",
|
||||
"IMAGE_SUBSYSTEM_WINDOWS_CUI",
|
||||
"IMAGE_SUBSYSTEM_WINDOWS_GUI",
|
||||
"IMAGE_SUBSYSTEM_XBOX",
|
||||
"ImportDirectory",
|
||||
"NewFile",
|
||||
"Open",
|
||||
@@ -4188,6 +4237,7 @@ var stdlib = map[string][]string{
|
||||
"DevNull",
|
||||
"Environ",
|
||||
"ErrClosed",
|
||||
"ErrDeadlineExceeded",
|
||||
"ErrExist",
|
||||
"ErrInvalid",
|
||||
"ErrNoDeadline",
|
||||
@@ -4646,6 +4696,7 @@ var stdlib = map[string][]string{
|
||||
"ErrRange",
|
||||
"ErrSyntax",
|
||||
"FormatBool",
|
||||
"FormatComplex",
|
||||
"FormatFloat",
|
||||
"FormatInt",
|
||||
"FormatUint",
|
||||
@@ -4655,6 +4706,7 @@ var stdlib = map[string][]string{
|
||||
"Itoa",
|
||||
"NumError",
|
||||
"ParseBool",
|
||||
"ParseComplex",
|
||||
"ParseFloat",
|
||||
"ParseInt",
|
||||
"ParseUint",
|
||||
|
||||
12
vendor/modules.txt
поставляемый
12
vendor/modules.txt
поставляемый
@@ -515,7 +515,12 @@ github.com/willf/bitset
|
||||
# github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c
|
||||
## explicit
|
||||
github.com/xtgo/uuid
|
||||
# go.etcd.io/bbolt v1.3.4
|
||||
# github.com/ziutek/mymysql v1.5.4
|
||||
## explicit
|
||||
# github.com/zmb3/gogetdoc v0.0.0-20190228002656-b37376c5da6a
|
||||
## explicit
|
||||
# go.etcd.io/bbolt v1.3.5
|
||||
## explicit
|
||||
go.etcd.io/bbolt
|
||||
# go.uber.org/atomic v1.6.0
|
||||
go.uber.org/atomic
|
||||
@@ -612,7 +617,7 @@ golang.org/x/text/secure/bidirule
|
||||
golang.org/x/text/transform
|
||||
golang.org/x/text/unicode/bidi
|
||||
golang.org/x/text/unicode/norm
|
||||
# golang.org/x/tools v0.0.0-20200626171337-aa94e735be7f
|
||||
# golang.org/x/tools v0.0.0-20200806022845-90696ccdc692
|
||||
## explicit
|
||||
golang.org/x/tools/go/ast/astutil
|
||||
golang.org/x/tools/go/gcexportdata
|
||||
@@ -630,7 +635,8 @@ golang.org/x/tools/internal/gopathwalk
|
||||
golang.org/x/tools/internal/imports
|
||||
golang.org/x/tools/internal/packagesinternal
|
||||
golang.org/x/tools/internal/typesinternal
|
||||
# golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
|
||||
# golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
|
||||
## explicit
|
||||
golang.org/x/xerrors
|
||||
golang.org/x/xerrors/internal
|
||||
# google.golang.org/appengine v1.6.6
|
||||
|
||||
Ссылка в новой задаче
Block a user