forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
88 lines (72 loc) · 2.14 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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/cmd/internal/env"
"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.",
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, _ []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)
fmt.Fprintln(out, "Platform: "+version.Platform)
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
}