Skip to content

Commit

Permalink
feat: Add syntax highlighting and clean up output (#1714)
Browse files Browse the repository at this point in the history
* feat: Add syntax highlighting, improve output and add colours to WSL on Windows

Co-authored-by: Owen Rumney <owen.rumney@aquasec.com>
  • Loading branch information
liamg and Owen Rumney committed May 6, 2022
1 parent 76ef458 commit b94dfe6
Show file tree
Hide file tree
Showing 7 changed files with 603 additions and 183 deletions.
7 changes: 6 additions & 1 deletion _examples/1000/main.tf
@@ -1,3 +1,8 @@

variable "bad" {
default = "0.0"
}

resource "aws_security_group" "test" {
name = "allow-all-sg"
description = "Allowing everyone to connect to this public instance"
Expand All @@ -16,6 +21,6 @@ resource "aws_security_group" "test" {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
cidr_blocks = ["0.0.${var.bad}/0"]
}
}
7 changes: 3 additions & 4 deletions go.mod
Expand Up @@ -5,22 +5,21 @@ go 1.16
require (
github.com/AlecAivazis/survey/v2 v2.3.4
github.com/Masterminds/semver v1.5.0
github.com/aquasecurity/defsec v0.46.3
github.com/aquasecurity/defsec v0.53.2
github.com/hashicorp/go-version v1.4.0
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/liamg/clinch v1.5.6
github.com/liamg/gifwrap v0.0.6
github.com/liamg/tml v0.6.0
github.com/owenrumney/squealer v0.3.2
github.com/owenrumney/squealer v1.0.0
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.1
github.com/zclconf/go-cty v1.10.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/liamg/memoryfs v1.2.0
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/liamg/memoryfs v1.4.1
github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/owenrumney/go-sarif/v2 v2.1.1
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
513 changes: 459 additions & 54 deletions go.sum

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions internal/app/tfsec/cmd/output.go
Expand Up @@ -73,6 +73,7 @@ func outputFormat(w io.Writer, addExtension bool, baseFilename, format, fsRoot,
WithColoursEnabled(!disableColours).
WithGroupingEnabled(!disableGrouping).
WithLinksFunc(gatherLinks).
WithFSRoot(fsRoot).
WithBaseDir(dir).
WithMetricsEnabled(!conciseOutput).
WithIncludeIgnored(includeIgnored).
Expand Down Expand Up @@ -104,11 +105,7 @@ func outputFormat(w io.Writer, addExtension bool, baseFilename, format, fsRoot,
return "", fmt.Errorf("invalid format specified: '%s'", format)
}

if makeRelative {
results = results.RelativeTo(fsRoot, dir)
} else {
results = results.Absolute(fsRoot)
}
factory.WithRelativePaths(makeRelative)

var outputPath string
if baseFilename != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/tfsec/cmd/prerun.go
Expand Up @@ -18,7 +18,7 @@ func prerun(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

// disable colour if running on windows - colour formatting doesn't work
if disableColours || runtime.GOOS == "windows" {
if disableColours || (runtime.GOOS == "windows" && os.Getenv("TERM") == "") {
tml.DisableFormatting()
} else {
tml.EnableFormatting()
Expand Down
2 changes: 1 addition & 1 deletion internal/app/tfsec/cmd/root.go
Expand Up @@ -88,7 +88,7 @@ func Root() *cobra.Command {
}

formats := strings.Split(format, ",")
if err := output(cmd, outputFlag, formats, root, dir, results, metrics); err != nil {
if err := output(cmd, outputFlag, formats, root, rel, results, metrics); err != nil {
return fmt.Errorf("failed to write output: %w", err)
}

Expand Down

0 comments on commit b94dfe6

Please sign in to comment.