Skip to content

Commit

Permalink
bhayes/version cmd (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Britton Hayes <[email protected]>
  • Loading branch information
brittonhayes and brittonhayes authored Mar 2, 2021
1 parent 58c1ea6 commit a58d4dd
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ builds:
- linux
- windows
- darwin
ldflags:
- -s -w -X github.com/brittonhayes/pillager/cmd.version={{.Version}}
- -X main.commit={{.Commit}}
- -X main.date={{.Date}}
- -X main.builtBy=goreleaser
archives:
- replacements:
darwin: Darwin
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ Pillager allows you to use powerful `go text/template` to customize the output f

```gotemplate
{{/*basic.tmpl*/}}
{{ range .Leaks}}
Leak: {{.Line}}
Line: {{.LineNumber}}
{{ range .Leaks -}}
File: {{ .File }}
{{end}}
Line: {{.LineNumber}}
Offender: {{ .Offender }}
{{ end -}}
```

#### Markdown Styling
Expand All @@ -115,6 +115,8 @@ File: {{ .File }}
{{end}}
```

> More template examples can be found in the [templates](./templates) directory.
## Documentation

:books: [View the docs](hunter)
Expand Down
9 changes: 0 additions & 9 deletions _examples/testdata/aws_leak.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
[
{
"line": " aws_access_key_id='AKIAIO5FODNN7EXAMPLE',",
"lineNumber": 5,
"offender": "AKIAIO5FODNN7EXAMPLE",
"commit": "",
"repo": "",
"repoURL": "",
"leakURL": "",
"rule": "AWS Access Key",
"commitMessage": "",
"author": "",
"email": "",
"file": "../test_data/test_repos/test_dir_1/server.test.py",
"date": "0001-01-01T00:00:00Z",
"tags": "key, AWS"
}
]
21 changes: 12 additions & 9 deletions cmd/hunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,40 @@ var huntCmd = &cobra.Command{
Long: "Hunt inside the file system for valuable information",
Example: `
# Run a basic hunt
pillager hunt ./
pillager hunt .
# Print out results in JSON format
pillager hunt ./example -r rules.toml -f json
pillager hunt ./example -f json
# Print out results in YAML format
pillager hunt . -f yaml
# Print out results with a custom inline template
pillager hunt ./example -r rules.toml -f custom --template "{{ range .Leaks}}Leak: {{.Line}}{{end}}"
pillager hunt . --template "{{ range .Leaks}}Leak: {{.Line}}{{end}}"
# Print out results with a custom template file
pillager hunt ./example -r rules.toml -f custom --template "$(cat templates/simple.tmpl)"
pillager hunt ./example --template "$(cat templates/simple.tmpl)"
`,
Args: cobra.MinimumNArgs(1),
RunE: StartHunt(),
Args: cobra.ExactArgs(1),
RunE: startHunt(),
}

func init() {
rootCmd.AddCommand(huntCmd)
huntCmd.Flags().IntVarP(&workers, "workers", "w", runtime.NumCPU(), "number of concurrent workers to create")
huntCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "toggle verbose output")
huntCmd.Flags().StringVarP(&rulesConfig, "rules", "r", "", "path to gitleaks rules.toml config")
huntCmd.Flags().StringVarP(&output, "format", "f", "json", "set output format (json, yaml, custom)")
huntCmd.Flags().StringVarP(&output, "format", "f", "json", "set output format (json, yaml)")
huntCmd.Flags().StringVarP(
&templ,
"template",
"t",
hunter.DefaultTemplate,
"",
"set go text/template string for output format",
)
}

func StartHunt() func(cmd *cobra.Command, args []string) error {
func startHunt() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
c := hunter.NewConfig(
afero.NewOsFs(),
Expand Down
21 changes: 21 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(versionCmd)
}

var version = "x.x.x"

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of Pillager",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("pillager v%s\n", version)
},
}
128 changes: 128 additions & 0 deletions go.sum

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion hunter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (c *Config) Default() *Config {
System: fs,
BasePath: v.Path(fs, "."),
Verbose: false,
Template: DefaultTemplate,
Gitleaks: rules.Load(""),
Format: JSONFormat,
}
Expand Down
9 changes: 7 additions & 2 deletions hunter/hound.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,26 @@ func NewHound(c *Config) *Hound {

// Howl prints out the Findings from the Hound in the preferred output format
func (h *Hound) Howl(findings scan.Report) {
if h.Config.Template != "" {
h.Config.Format = CustomFormat
}
switch h.Config.Format {
case JSONFormat:
b, err := json.Marshal(&findings)
b, err := json.Marshal(&findings.Leaks)
if err != nil {
log.Fatal("Failed to unmarshal findings")
}
fmt.Println(string(b))
case YAMLFormat:
b, err := yaml.Marshal(&findings)
b, err := yaml.Marshal(&findings.Leaks)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Println(string(b))
case CustomFormat:
RenderTemplate(os.Stdout, h.Config.Template, findings)
default:
RenderTemplate(os.Stdout, DefaultTemplate, findings)
}
}
6 changes: 3 additions & 3 deletions hunter/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
// DefaultTemplate is the base template used to
// format a Finding into the custom output format
const DefaultTemplate = `{{ with . -}}
{{ range .Leaks -}}Line: {{.LineNumber}}
{{ range .Leaks -}}
Line: {{.LineNumber}}
File: {{ .File }}
Offender: {{ .Offender }}
{{end}}
{{- end}}`
{{ end -}}{{- end}}`

// RenderTemplate renders a Hound finding in a
// custom go template format to the provided writer
Expand Down
6 changes: 4 additions & 2 deletions templates/html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pillager - Scan Results</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<link rel="stylesheet" href="https://unpkg.com/bulmaswatch/superhero/bulmaswatch.min.css">
</head>
<body>
<section class="section">
Expand All @@ -22,7 +22,9 @@
<p class="is-size-4">{{.File}}</p>
<p>
<span class="has-text-weight-bold">Tags:</span>
<span class="tag">{{.Tags}}</span>
<span class="tag">
<code>{{.Tags}}</code>
</span>
</p>
<p>
<span class="has-text-weight-bold">Leak:</span>
Expand Down
8 changes: 4 additions & 4 deletions templates/simple.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ range .Leaks}}
Leak: {{.Line}}
Line: {{.LineNumber}}
{{ range .Leaks -}}
File: {{ .File }}
{{end}}
Line: {{.LineNumber}}
Offender: {{ .Offender }}
{{ end -}}

0 comments on commit a58d4dd

Please sign in to comment.