github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/toolchain/switch.go (about) 1 // Copyright 2023 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 5 package toolchain 6 7 import ( 8 "github.com/shogo82148/std/context" 9 10 "github.com/shogo82148/std/cmd/go/internal/gover" 11 ) 12 13 // A Switcher collects errors to be reported and then decides 14 // between reporting the errors or switching to a new toolchain 15 // to resolve them. 16 // 17 // The client calls [Switcher.Error] repeatedly with errors encountered 18 // and then calls [Switcher.Switch]. If the errors included any 19 // *gover.TooNewErrors (potentially wrapped) and switching is 20 // permitted by GOTOOLCHAIN, Switch switches to a new toolchain. 21 // Otherwise Switch prints all the errors using base.Error. 22 // 23 // See https://go.dev/doc/toolchain#switch. 24 type Switcher struct { 25 TooNew *gover.TooNewError 26 Errors []error 27 } 28 29 // Error reports the error to the Switcher, 30 // which saves it for processing during Switch. 31 func (s *Switcher) Error(err error) 32 33 // NeedSwitch reports whether Switch would attempt to switch toolchains. 34 func (s *Switcher) NeedSwitch() bool 35 36 // Switch decides whether to switch to a newer toolchain 37 // to resolve any of the saved errors. 38 // It switches if toolchain switches are permitted and there is at least one TooNewError. 39 // 40 // If Switch decides not to switch toolchains, it prints the errors using base.Error and returns. 41 // 42 // If Switch decides to switch toolchains but cannot identify a toolchain to use. 43 // it prints the errors along with one more about not being able to find the toolchain 44 // and returns. 45 // 46 // Otherwise, Switch prints an informational message giving a reason for the 47 // switch and the toolchain being invoked and then switches toolchains. 48 // This operation never returns. 49 func (s *Switcher) Switch(ctx context.Context) 50 51 // SwitchOrFatal attempts a toolchain switch based on the information in err 52 // and otherwise falls back to base.Fatal(err). 53 func SwitchOrFatal(ctx context.Context, err error) 54 55 // NewerToolchain returns the name of the toolchain to use when we need 56 // to switch to a newer toolchain that must support at least the given Go version. 57 // See https://go.dev/doc/toolchain#switch. 58 // 59 // If the latest major release is 1.N.0, we use the latest patch release of 1.(N-1) if that's >= version. 60 // Otherwise we use the latest 1.N if that's allowed. 61 // Otherwise we use the latest release. 62 func NewerToolchain(ctx context.Context, version string) (string, error) 63 64 // HasAuto reports whether the GOTOOLCHAIN setting allows "auto" upgrades. 65 func HasAuto() bool 66 67 // HasPath reports whether the GOTOOLCHAIN setting allows "path" upgrades. 68 func HasPath() bool