...
1
2
3
4
5 package gover
6
7 import (
8 "cmd/go/internal/base"
9 "context"
10 "errors"
11 "fmt"
12 "strings"
13 )
14
15
16
17
18
19
20
21
22
23
24 func FromToolchain(name string) string {
25 if strings.ContainsAny(name, "\\/") {
26
27
28
29 return ""
30 }
31
32 var v string
33 if strings.HasPrefix(name, "go") {
34 v = name[2:]
35 } else {
36 return ""
37 }
38
39 if i := strings.IndexAny(v, " \t-"); i >= 0 {
40 v = v[:i]
41 }
42 if !IsValid(v) {
43 return ""
44 }
45 return v
46 }
47
48 func maybeToolchainVersion(name string) string {
49 if IsValid(name) {
50 return name
51 }
52 return FromToolchain(name)
53 }
54
55
56
57
58 func ToolchainMax(x, y string) string {
59 if Compare(FromToolchain(x), FromToolchain(y)) < 0 {
60 return y
61 }
62 return x
63 }
64
65
66
67 var Startup struct {
68 GOTOOLCHAIN string
69 AutoFile string
70 AutoGoVersion string
71 AutoToolchain string
72 }
73
74
75 type TooNewError struct {
76 What string
77 GoVersion string
78 Toolchain string
79 }
80
81 func (e *TooNewError) Error() string {
82 var explain string
83 if Startup.GOTOOLCHAIN != "" && Startup.GOTOOLCHAIN != "auto" {
84 explain = "; GOTOOLCHAIN=" + Startup.GOTOOLCHAIN
85 }
86 if Startup.AutoFile != "" && (Startup.AutoGoVersion != "" || Startup.AutoToolchain != "") {
87 explain += fmt.Sprintf("; %s sets ", base.ShortPath(Startup.AutoFile))
88 if Startup.AutoToolchain != "" {
89 explain += "toolchain " + Startup.AutoToolchain
90 } else {
91 explain += "go " + Startup.AutoGoVersion
92 }
93 }
94 return fmt.Sprintf("%v requires go >= %v (running go %v%v)", e.What, e.GoVersion, Local(), explain)
95 }
96
97 var ErrTooNew = errors.New("module too new")
98
99 func (e *TooNewError) Is(err error) bool {
100 return err == ErrTooNew
101 }
102
103
104
105 type Switcher interface {
106 Error(err error)
107 Switch(ctx context.Context)
108 }
109
View as plain text