Skip to content

Commit

Permalink
Merge pull request #1769 from zregvart/issue/EC-564
Browse files Browse the repository at this point in the history
Humanize v-e-c Task logs
  • Loading branch information
zregvart authored Jul 26, 2024
2 parents 2afc5d7 + 751f2e5 commit 85ae44e
Show file tree
Hide file tree
Showing 54 changed files with 568 additions and 78 deletions.
9 changes: 8 additions & 1 deletion cmd/root/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package root

import (
"context"
"io"
"time"

hd "github.com/MakeNowJust/heredoc"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/enterprise-contract/ec-cli/internal/kubernetes"
Expand All @@ -35,6 +37,7 @@ var (
debug bool = false
trace bool = false
globalTimeout = 5 * time.Minute
logfile string
)

func NewRootCmd() *cobra.Command {
Expand All @@ -51,7 +54,7 @@ func NewRootCmd() *cobra.Command {
SilenceUsage: true,

PersistentPreRun: func(cmd *cobra.Command, _ []string) {
logging.InitLogging(verbose, quiet, debug, trace)
logging.InitLogging(verbose, quiet, debug, trace, logfile)

// Create a new context now that flags have been parsed so a custom timeout can be used.
ctx := cmd.Context()
Expand All @@ -60,6 +63,9 @@ func NewRootCmd() *cobra.Command {
},

PersistentPostRun: func(cmd *cobra.Command, _ []string) {
if f, ok := logrus.StandardLogger().Out.(io.Closer); ok {
f.Close()
}
if cancel != nil {
cancel()
}
Expand All @@ -77,5 +83,6 @@ func setFlags(rootCmd *cobra.Command) {
rootCmd.PersistentFlags().BoolVar(&debug, "debug", debug, "same as verbose but also show function names and line numbers")
rootCmd.PersistentFlags().BoolVar(&trace, "trace", trace, "enable trace logging")
rootCmd.PersistentFlags().DurationVar(&globalTimeout, "timeout", globalTimeout, "max overall execution duration")
rootCmd.PersistentFlags().StringVar(&logfile, "logfile", "", "file to write the logging output. If not specified logging output will be written to stderr")
kubernetes.AddKubeconfigFlag(rootCmd)
}
4 changes: 2 additions & 2 deletions cmd/validate/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func validateDefinitionCmd(validate definitionValidationFn) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
var allErrors error
report := definition.NewReport()
showSuccesses, _ := cmd.Flags().GetBool("show-successes")
for i := range data.filePaths {
fpath := data.filePaths[i]
var sources []source.PolicySource
Expand All @@ -98,7 +99,6 @@ func validateDefinitionCmd(validate definitionValidationFn) *cobra.Command {
if out, err := validate(ctx, fpath, sources, data.namespaces); err != nil {
allErrors = multierror.Append(allErrors, err)
} else {
showSuccesses, _ := cmd.Flags().GetBool("show-successes")
if !showSuccesses {
for i := range out.PolicyCheck {
out.PolicyCheck[i].Successes = []evaluator.Result{}
Expand All @@ -107,7 +107,7 @@ func validateDefinitionCmd(validate definitionValidationFn) *cobra.Command {
report.Add(*out)
}
}
p := format.NewTargetParser(definition.JSONReport, cmd.OutOrStdout(), utils.FS(cmd.Context()))
p := format.NewTargetParser(definition.JSONReport, format.Options{ShowSuccesses: showSuccesses}, cmd.OutOrStdout(), utils.FS(cmd.Context()))
for _, target := range data.output {
if err := report.Write(target, p); err != nil {
allErrors = multierror.Append(allErrors, err)
Expand Down
11 changes: 7 additions & 4 deletions cmd/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
defer c.Destroy()
}

showSuccesses, _ := cmd.Flags().GetBool("show-successes")

// worker is responsible for processing one component at a time from the jobs channel,
// and for emitting a corresponding result for the component on the results channel.
worker := func(id int, jobs <-chan app.SnapshotComponent, results chan<- result) {
Expand All @@ -332,7 +334,6 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
// Skip on err to not panic. Error is return on routine completion.
if err == nil {
res.component.Violations = out.Violations()
showSuccesses, _ := cmd.Flags().GetBool("show-successes")
res.component.Warnings = out.Warnings()

successes := out.Successes()
Expand Down Expand Up @@ -403,11 +404,11 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
data.output = append(data.output, fmt.Sprintf("%s=%s", applicationsnapshot.JSON, data.outputFile))
}

report, err := applicationsnapshot.NewReport(data.snapshot, components, data.policy, manyData, manyPolicyInput)
report, err := applicationsnapshot.NewReport(data.snapshot, components, data.policy, manyData, manyPolicyInput, showSuccesses)
if err != nil {
return err
}
p := format.NewTargetParser(applicationsnapshot.JSON, cmd.OutOrStdout(), utils.FS(cmd.Context()))
p := format.NewTargetParser(applicationsnapshot.JSON, format.Options{ShowSuccesses: showSuccesses}, cmd.OutOrStdout(), utils.FS(cmd.Context()))
utils.SetColorEnabled(data.noColor, data.forceColor)
if err := report.WriteAll(data.output, p); err != nil {
return err
Expand Down Expand Up @@ -465,7 +466,9 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
cmd.Flags().StringSliceVar(&data.output, "output", data.output, hd.Doc(`
write output to a file in a specific format. Use empty string path for stdout.
May be used multiple times. Possible formats are:
`+strings.Join(validOutputFormats, ", ")+`.
`+strings.Join(validOutputFormats, ", ")+`. In following format and file path
additional options can be provided in key=value form following the question
mark (?) sign, for example: --output text=output.txt?show-successes=false
`))

cmd.Flags().StringVarP(&data.outputFile, "output-file", "o", data.outputFile,
Expand Down
17 changes: 12 additions & 5 deletions cmd/validate/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"errors"
"fmt"
"sort"
"strings"
"sync"

hd "github.com/MakeNowJust/heredoc"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"

"github.com/enterprise-contract/ec-cli/internal/applicationsnapshot"
"github.com/enterprise-contract/ec-cli/internal/evaluator"
"github.com/enterprise-contract/ec-cli/internal/format"
"github.com/enterprise-contract/ec-cli/internal/input"
Expand Down Expand Up @@ -56,7 +58,7 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
Short: "Validate arbitrary JSON or yaml file input conformance with the Enterprise Contract",
Long: hd.Doc(`
Validate conformance of arbitrary JSON or yaml file input with the Enterprise Contract
For each file, validation is performed to determine if the file conforms to rego policies
defined in the the EnterpriseContractPolicy.
`),
Expand Down Expand Up @@ -112,6 +114,8 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {

var lock sync.WaitGroup

showSuccesses, _ := cmd.Flags().GetBool("show-successes")

for _, f := range data.filePaths {
lock.Add(1)
go func(fpath string) {
Expand All @@ -129,7 +133,6 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
// Skip on err to not panic. Error is return on routine completion.
if err == nil {
res.input.Violations = out.Violations()
showSuccesses, _ := cmd.Flags().GetBool("show-successes")
res.input.Warnings = out.Warnings()

successes := out.Successes()
Expand Down Expand Up @@ -176,7 +179,7 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
return err
}

p := format.NewTargetParser(input.JSON, cmd.OutOrStdout(), utils.FS(cmd.Context()))
p := format.NewTargetParser(input.JSON, format.Options{ShowSuccesses: showSuccesses}, cmd.OutOrStdout(), utils.FS(cmd.Context()))
if err := report.WriteAll(data.output, p); err != nil {
return err
}
Expand All @@ -197,10 +200,14 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
* git reference (github.com/user/repo//default?ref=main), or
* inline JSON ('{sources: {...}, configuration: {...}}')")`))

validOutputFormats := applicationsnapshot.OutputFormats
cmd.Flags().StringSliceVarP(&data.output, "output", "o", data.output, hd.Doc(`
Write output to a file in a specific format, e.g. yaml=/tmp/output.yaml. Use empty string
path for stdout, e.g. yaml. May be used multiple times. Possible formats are json, yaml,
and summary`))
path for stdout, e.g. yaml. May be used multiple times. Possible formats are:
`+strings.Join(validOutputFormats, ", ")+`. In following format and file path
additional options can be provided in key=value form following the question
mark (?) sign, for example: --output text=output.txt?show-successes=false
`))

cmd.Flags().BoolVarP(&data.strict, "strict", "s", data.strict,
"Return non-zero status on non-successful validation")
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ec [flags]
--debug:: same as verbose but also show function names and line numbers (Default: false)
-h, --help:: help for ec (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_fetch.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Fetch remote resources

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_fetch_policy.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Notes:

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_init.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Initialize a directory for use

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_init_policies.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Initialize the "my-policy" directory with minimal EC policy scaffolding:

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_inspect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Inspect policy rules

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_inspect_policy-data.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ec inspect policy-data --source git::https://github.com/enterprise-contract/ec-p

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_inspect_policy.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Display details about the latest Enterprise Contract release policy in json form

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ec opa [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_bench.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ec opa bench <query> [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_build.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ ec opa build <path> [<path> [...]] [flags]
== Options inherited from parent commands
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_capabilities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ ec opa capabilities [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_check.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ec opa check <path> [path [...]] [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_deps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ec opa deps <query> [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_eval.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ ec opa eval <query> [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--trace:: enable trace logging (Default: false)
--verbose:: more verbose output (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_exec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ec opa exec <path> [<path> [...]] [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--trace:: enable trace logging (Default: false)
--verbose:: more verbose output (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_fmt.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ec opa fmt [path [...]] [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_inspect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ec opa inspect <path> [<path> [...]] [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_parse.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ec opa parse <path> [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_run.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ ec opa run [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_sign.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ ec opa sign <path> [<path> [...]] [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_test.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ ec opa test <path> [path [...]] [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--trace:: enable trace logging (Default: false)

Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_opa_version.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ec opa version [flags]

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_sigstore.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Perform certain sigstore operations

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_sigstore_initialize.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ec initialize -mirror <url> -root <url>

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--quiet:: less verbose output (Default: false)
--timeout:: max overall execution duration (Default: 5m0s)
--trace:: enable trace logging (Default: false)
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/ec_test.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ the output will include a detailed trace of how the policy was evaluated, e.g.

--debug:: same as verbose but also show function names and line numbers (Default: false)
--kubeconfig:: path to the Kubernetes config file to use
--logfile:: file to write the logging output. If not specified logging output will be written to stderr
--timeout:: max overall execution duration (Default: 5m0s)
--verbose:: more verbose output (Default: false)

Expand Down
Loading

0 comments on commit 85ae44e

Please sign in to comment.