Files
releases/cmd/version.go
T
Torin Sandall e34a10624d runtime: Fix logging configuration (#3959)
This commit updates the runtime to pass the logger to the plugin
manager and the reporter. In addition, the reporter is updated to pass
the logger into the rest client that it creates. With this change, we
no longer rely on the global logger and the logging configuration is
applied correctly.

To verify that this change is going to fix the problem, I have
searched for references to logrus and the logging package.

Grepping for references to logrus reveals that outside of the logging
package, we only refer to formatters and fields (never the global
logrus logger directly).

Grepping for references to logging.{New, Get, NewStandardLogger}
shows that we only create new loggers if one has not been injected
into the manager or rest client. Since we are passing/injecting the
logger from the runtime, I am fairly confident this will fix the
underlying issue.

Fixes #3958

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-11-03 21:54:32 +01:00

84 lines
1.9 KiB
Go

// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package cmd
import (
"context"
"crypto/rand"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/open-policy-agent/opa/internal/report"
"github.com/open-policy-agent/opa/internal/uuid"
"github.com/open-policy-agent/opa/version"
)
func init() {
var check bool
var versionCommand = &cobra.Command{
Use: "version",
Short: "Print the version of OPA",
Long: "Show version and build information for OPA.",
Run: func(cmd *cobra.Command, args []string) {
generateCmdOutput(os.Stdout, check)
},
}
// The version command can also be used to check for the latest released OPA version.
// Some tools could use this for feature flagging purposes and hence this option is OFF by-default.
versionCommand.Flags().BoolVarP(&check, "check", "c", false, "check for latest OPA release")
RootCommand.AddCommand(versionCommand)
}
func generateCmdOutput(out io.Writer, check bool) {
fmt.Fprintln(out, "Version: "+version.Version)
fmt.Fprintln(out, "Build Commit: "+version.Vcs)
fmt.Fprintln(out, "Build Timestamp: "+version.Timestamp)
fmt.Fprintln(out, "Build Hostname: "+version.Hostname)
fmt.Fprintln(out, "Go Version: "+version.GoVersion)
var wasmAvailable string
if version.WasmRuntimeAvailable() {
wasmAvailable = "available"
} else {
wasmAvailable = "unavailable"
}
fmt.Fprintln(out, "WebAssembly: "+wasmAvailable)
if check {
err := checkOPAUpdate(out)
if err != nil {
fmt.Fprintf(out, "Error: %v\n", err)
os.Exit(1)
}
}
}
func checkOPAUpdate(out io.Writer) error {
id, err := uuid.New(rand.Reader)
if err != nil {
return err
}
reporter, err := report.New(id, report.Options{})
if err != nil {
return err
}
resp, err := reporter.SendReport(context.Background())
if err != nil {
return err
}
fmt.Fprintln(out, resp.Pretty())
return nil
}