pulumi/sdk/go/common/util/cmdutil/diag.go

102 lines
3.2 KiB
Go
Raw Permalink Normal View History

2018-05-22 19:43:36 +00:00
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmdutil
import (
"os"
"sync"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
var (
snkMutex sync.Mutex
snk diag.Sink
)
// By default we'll attempt to figure out if we should have colors or not. This can be overridden
// for any command by passing --color=... at the command line.
var globalColorization *colors.Colorization
// GetGlobalColorization gets the global setting for how things should be colored.
// This is helpful for the parts of our stack that do not take a DisplayOptions struct.
func GetGlobalColorization() colors.Colorization {
if globalColorization != nil {
// User has set an explicit colorization preference. We'll respect whatever they asked for,
// no matter what.
return *globalColorization
}
// Colorization is set to 'auto' (either explicit set to that by the user, or not set at all).
// Figure out the best thing to do here.
// If the external environment has requested no colors, then turn off all colors when in 'auto' mode.
if _, ok := os.LookupEnv("NO_COLOR"); ok {
return colors.Never
}
// Disable colors if we're not in an interactive session (i.e. we're redirecting stdout). This
// will just inject color tags into the stream which are not desirable here.
if !InteractiveTerminal() {
return colors.Never
}
// Things otherwise look good. Turn on colors.
return colors.Always
}
// SetGlobalColorization sets the global setting for how things should be colored.
// This is helpful for the parts of our stack that do not take a DisplayOptions struct.
func SetGlobalColorization(value string) error {
switch value {
case "auto":
globalColorization = nil
case "always":
c := colors.Always
globalColorization = &c
case "never":
c := colors.Never
globalColorization = &c
case "raw":
c := colors.Raw
globalColorization = &c
default:
return errors.Errorf("unsupported color option: '%s'. Supported values are: auto, always, never, raw", value)
}
return nil
}
// Diag lazily allocates a sink to be used if we can't create a compiler.
func Diag() diag.Sink {
snkMutex.Lock()
defer snkMutex.Unlock()
if snk == nil {
snk = diag.DefaultSink(os.Stdout, os.Stderr, diag.FormatOptions{
Color: GetGlobalColorization(),
})
}
return snk
}
// InitDiag forces initialization of the diagnostics sink with the given options.
func InitDiag(opts diag.FormatOptions) {
contract.Assertf(snk == nil, "Cannot initialize diagnostics sink more than once")
snk = diag.DefaultSink(os.Stdout, os.Stderr, opts)
}