Skip to content

Commit 07acfa9

Browse files
authored
Merge pull request #3049 from simonbaird/validate-input-text-color-output
Support red/green color for ec validate input text output
2 parents 048da8f + 6668965 commit 07acfa9

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

cmd/validate/input.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
4343
effectiveTime string
4444
filePaths []string
4545
filterType string
46+
forceColor bool
4647
info bool
4748
namespaces []string
49+
noColor bool
4850
output []string
4951
policy policy.Policy
5052
policyConfiguration string
@@ -213,6 +215,8 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
213215
return err
214216
}
215217

218+
utils.SetColorEnabled(data.noColor, data.forceColor)
219+
216220
p := format.NewTargetParser(input.Text, format.Options{ShowSuccesses: showSuccesses, ShowWarnings: showWarnings}, cmd.OutOrStdout(), utils.FS(cmd.Context()))
217221
if err := report.WriteAll(data.output, p); err != nil {
218222
return err
@@ -264,6 +268,12 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
264268
- "include-exclude": Uses traditional include/exclude filtering without pipeline intentions
265269
- "ec-policy": Uses Enterprise Contract policy filtering with pipeline intention support`))
266270

271+
cmd.Flags().BoolVar(&data.noColor, "no-color", false, hd.Doc(`
272+
Disable color when using text output even when the current terminal supports it`))
273+
274+
cmd.Flags().BoolVar(&data.forceColor, "color", false, hd.Doc(`
275+
Enable color when using text output even when the current terminal does not support it`))
276+
267277
if err := cmd.MarkFlagRequired("file"); err != nil {
268278
panic(err)
269279
}

docs/modules/ROOT/pages/ec_validate_input.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ of the git repo. For git repos not hosted on 'github.com' or 'gitlab.com', prefi
3939

4040
== Options
4141

42+
--color:: Enable color when using text output even when the current terminal does not support it (Default: false)
4243
--effective-time:: Run policy checks with the provided time. Useful for testing rules with
4344
effective dates in the future. The value can be "now" (default) - for
4445
current time, or a RFC3339 formatted value, e.g. 2022-11-18T00:00:00Z. (Default: now)
@@ -50,6 +51,7 @@ current time, or a RFC3339 formatted value, e.g. 2022-11-18T00:00:00Z. (Default:
5051
--info:: Include additional information on the failures. For instance for policy
5152
violations, include the title and the description of the failed policy
5253
rule. (Default: false)
54+
--no-color:: Disable color when using text output even when the current terminal supports it (Default: false)
5355
-o, --output:: Write output to a file in a specific format, e.g. yaml=/tmp/output.yaml. Use empty string
5456
path for stdout, e.g. yaml. May be used multiple times. Possible formats are:
5557
json, yaml, text, summary. In following format and file path

features/__snapshots__/validate_input.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@
2828

2929
[valid policy URL:stderr - 1]
3030

31+
---
32+
[valid policy URL with text output:stdout - 1]
33+
Success: true
34+
Result: SUCCESS
35+
Violations: 0, Warnings: 0, Successes: 1
36+
Input File: pipeline_definition.yaml
37+
38+
Results:
39+
✓ [Success] main.acceptor
40+
FilePath: pipeline_definition.yaml
41+
42+
43+
---
44+
45+
[valid policy URL with text output:stderr - 1]
46+
3147
---
3248

3349
[policy with multiple sources:stdout - 1]

features/validate_input.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ Feature: validate input
2727
Then the exit status should be 0
2828
Then the output should match the snapshot
2929

30+
Scenario: valid policy URL with text output
31+
Given a git repository named "happy-day-config" with
32+
| policy.yaml | examples/happy_config.yaml |
33+
Given a git repository named "happy-day-policy" with
34+
| main.rego | examples/happy_day.rego |
35+
Given a pipeline definition file named "pipeline_definition.yaml" containing
36+
"""
37+
---
38+
apiVersion: tekton.dev/v1
39+
kind: Pipeline
40+
metadata:
41+
name: basic-build
42+
spec:
43+
tasks:
44+
- name: appstudio-init
45+
taskRef:
46+
name: init
47+
version: "0.1"
48+
"""
49+
When ec command is run with "validate input --file pipeline_definition.yaml --policy git::https://${GITHOST}/git/happy-day-config.git --output text --show-successes --color"
50+
Then the exit status should be 0
51+
Then the output should match the snapshot
52+
3053
Scenario: policy URL with no rego files
3154
Given a git repository named "sad-day-config" with
3255
| policy.yaml | examples/sad_config.yaml |

0 commit comments

Comments
 (0)