Skip to content

Commit

Permalink
ecshreve: some simple cleanup (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecshreve authored Mar 4, 2022
1 parent 4b1df72 commit 6b39bd2
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 59 deletions.
3 changes: 1 addition & 2 deletions _examples/hunter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func main() {
}

// Report results
err = h.Report(os.Stdout, results)
if err != nil {
if err = h.Report(os.Stdout, results); err != nil {
log.Fatal().Err(err).Send()
}
}
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// pillager is a tool for hunting through filesystems for sensitive information.
// Package pillager is a tool for hunting through filesystems for sensitive information.
//
// Installation
//
Expand Down
7 changes: 3 additions & 4 deletions internal/commands/hunt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Package commands contains the command line logic
// Package commands contains the command line logic.
//
// The commands package is the primary consumer of all packages in the /pkg directory
// The commands package is the primary consumer of all packages in the /pkg directory.
package commands

import (
Expand Down Expand Up @@ -72,8 +72,7 @@ var huntCmd = &cobra.Command{
return err
}

err = h.Report(os.Stdout, results)
if err != nil {
if err = h.Report(os.Stdout, results); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/validate/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/spf13/afero"
)

// Path checks if a filepath exists and
// returns it if so, otherwise returns a default path.
// Path checks if a file at the given path exists and returns it if so,
// otherwise returns a default path.
func Path(fs afero.Fs, path string) string {
ok, err := afero.Exists(fs, path)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ type HTMLTable struct{}
func (h HTMLTable) Report(w io.Writer, findings []report.Finding) error
```

## type [JSON](<https://github.com/brittonhayes/pillager/blob/main/pkg/format/report.go#L18>)
## type [JSON](<https://github.com/brittonhayes/pillager/blob/main/pkg/format/report.go#L19>)

```go
type JSON struct{}
```

### func \(JSON\) [Report](<https://github.com/brittonhayes/pillager/blob/main/pkg/format/report.go#L20>)
### func \(JSON\) [Report](<https://github.com/brittonhayes/pillager/blob/main/pkg/format/report.go#L21>)

```go
func (j JSON) Report(w io.Writer, findings []report.Finding) error
Expand All @@ -101,7 +101,9 @@ type Markdown struct{}
func (m Markdown) Report(w io.Writer, findings []report.Finding) error
```

## type [Reporter](<https://github.com/brittonhayes/pillager/blob/main/pkg/format/report.go#L14-L16>)
## type [Reporter](<https://github.com/brittonhayes/pillager/blob/main/pkg/format/report.go#L15-L17>)

Reporter is the interface that each of the canonical output formats implement\.

```go
type Reporter interface {
Expand Down
4 changes: 2 additions & 2 deletions pkg/format/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v2"
)

// Reporter is the interface that each of the canonical output formats implement.
type Reporter interface {
Report(io.Writer, []report.Finding) error
}
Expand All @@ -19,8 +20,7 @@ type JSON struct{}

func (j JSON) Report(w io.Writer, findings []report.Finding) error {
encoder := json.NewEncoder(w)
err := encoder.Encode(&findings)
if err != nil {
if err := encoder.Encode(&findings); err != nil {
return err
}

Expand Down
28 changes: 16 additions & 12 deletions pkg/hunter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import "github.com/brittonhayes/pillager/pkg/hunter"
```

Package hunter contains secret hunting and file scanning tools
Package hunter contains secret hunting and file scanning tools\.

## Index

Expand All @@ -28,7 +28,7 @@ Package hunter contains secret hunting and file scanning tools
- [func (h *Hunter) Report(w io.Writer, findings []report.Finding) error](<#func-hunter-report>)


## type [Config](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L22-L33>)
## type [Config](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L21-L32>)

Config takes all of the configurable parameters for a Hunter\.

Expand All @@ -47,67 +47,71 @@ type Config struct {
}
```

### func [NewConfig](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L37>)
### func [NewConfig](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L38>)

```go
func NewConfig(opts ...ConfigOption) *Config
```

NewConfig creates a Config instance\.

## type [ConfigOption](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L35>)

ConfigOption is a convenient type alias for func\(\*Config\)\.

```go
type ConfigOption func(*Config)
```

### func [WithFS](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L72>)
### func [WithFS](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L73>)

```go
func WithFS(fs afero.Fs) ConfigOption
```

### func [WithFormat](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L112>)
### func [WithFormat](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L113>)

```go
func WithFormat(reporter format.Reporter) ConfigOption
```

### func [WithGitleaksConfig](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L132>)
### func [WithGitleaksConfig](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L133>)

```go
func WithGitleaksConfig(g config.Config) ConfigOption
```

### func [WithLogLevel](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L84>)
### func [WithLogLevel](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L85>)

```go
func WithLogLevel(level string) ConfigOption
```

### func [WithRedact](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L106>)
### func [WithRedact](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L107>)

```go
func WithRedact(redact bool) ConfigOption
```

### func [WithScanPath](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L78>)
### func [WithScanPath](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L79>)

```go
func WithScanPath(path string) ConfigOption
```

### func [WithTemplate](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L125>)
### func [WithTemplate](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L126>)

```go
func WithTemplate(template string) ConfigOption
```

### func [WithVerbose](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L94>)
### func [WithVerbose](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L95>)

```go
func WithVerbose(verbose bool) ConfigOption
```

### func [WithWorkers](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L100>)
### func [WithWorkers](<https://github.com/brittonhayes/pillager/blob/main/pkg/hunter/config.go#L101>)

```go
func WithWorkers(count int) ConfigOption
Expand Down
5 changes: 3 additions & 2 deletions pkg/hunter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
"github.com/zricethezav/gitleaks/v8/config"
)

// Config takes all of the configurable
// parameters for a Hunter.
// Config takes all of the configurable parameters for a Hunter.
type Config struct {
Filesystem afero.Fs
Reporter format.Reporter
Expand All @@ -32,8 +31,10 @@ type Config struct {
Template string
}

// ConfigOption is a convenient type alias for func(*Config).
type ConfigOption func(*Config)

// NewConfig creates a Config instance.
func NewConfig(opts ...ConfigOption) *Config {
var (
defaultFS = afero.NewOsFs()
Expand Down
2 changes: 1 addition & 1 deletion pkg/hunter/docs.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package hunter contains secret hunting and file scanning tools
// Package hunter contains secret hunting and file scanning tools.
package hunter
28 changes: 17 additions & 11 deletions pkg/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import "github.com/brittonhayes/pillager/pkg/rules"
```

Package rules enables the parsing of Gitleaks rulesets
Package rules enables the parsing of Gitleaks rulesets\.

## Index

Expand All @@ -22,14 +22,16 @@ Package rules enables the parsing of Gitleaks rulesets

## Constants

ErrReadConfig is the custom error message used if an error is encountered reading the gitleaks config\.

```go
const (
ErrReadConfig = "Failed to read config"
)
const ErrReadConfig = "Failed to read gitleaks config"
```

## Variables

These strings contain default configs\. They are initialized at compile time via go:embed\.

```go
var (
//go:embed rules_simple.toml
Expand All @@ -40,21 +42,23 @@ var (
)
```

## type [Loader](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L25-L27>)
## type [Loader](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L27-L29>)

Loader represents a gitleaks config loader\.

```go
type Loader struct {
// contains filtered or unexported fields
}
```

### func [NewLoader](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L33>)
### func [NewLoader](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L35>)

```go
func NewLoader(opts ...LoaderOption) *Loader
```

NewLoader creates a configuration loader\.
NewLoader creates a Loader instance\.

### func \(\*Loader\) [Load](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L58>)

Expand All @@ -64,27 +68,29 @@ func (l *Loader) Load() config.Config

Load parses the gitleaks configuration\.

### func \(\*Loader\) [WithStrict](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L48>)
### func \(\*Loader\) [WithStrict](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L49>)

```go
func (l *Loader) WithStrict() LoaderOption
```

WithStrict enables more strict pillager scanning\.

## type [LoaderOption](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L29>)
## type [LoaderOption](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L32>)

LoaderOption sets a parameter for the gitleaks config loader\.

```go
type LoaderOption func(*Loader)
```

### func [FromFile](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L69>)
### func [FromFile](<https://github.com/brittonhayes/pillager/blob/main/pkg/rules/rules.go#L68>)

```go
func FromFile(file string) LoaderOption
```

FromFile decodes the configuration from a local file\.
FromFile decodes a gitleaks config from a local file\.



Expand Down
26 changes: 12 additions & 14 deletions pkg/rules/rules.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package rules enables the parsing of Gitleaks rulesets
// Package rules enables the parsing of Gitleaks rulesets.
package rules

import (
Expand All @@ -10,10 +10,11 @@ import (
"github.com/zricethezav/gitleaks/v8/config"
)

const (
ErrReadConfig = "Failed to read config"
)
// ErrReadConfig is the custom error message used if an error is encountered
// reading the gitleaks config.
const ErrReadConfig = "Failed to read gitleaks config"

// These strings contain default configs. They are initialized at compile time via go:embed.
var (
//go:embed rules_simple.toml
RulesDefault string
Expand All @@ -22,18 +23,18 @@ var (
RulesStrict string
)

// Loader represents a gitleaks config loader.
type Loader struct {
loader config.ViperConfig
}

// LoaderOption sets a parameter for the gitleaks config loader.
type LoaderOption func(*Loader)

// NewLoader creates a configuration
// loader.
// NewLoader creates a Loader instance.
func NewLoader(opts ...LoaderOption) *Loader {
var loader Loader
_, err := toml.Decode(RulesDefault, &loader.loader)
if err != nil {
if _, err := toml.Decode(RulesDefault, &loader.loader); err != nil {
log.Fatal().Err(err).Msg(ErrReadConfig)
}

Expand All @@ -47,8 +48,7 @@ func NewLoader(opts ...LoaderOption) *Loader {
// WithStrict enables more strict pillager scanning.
func (l *Loader) WithStrict() LoaderOption {
return func(l *Loader) {
_, err := toml.Decode(RulesStrict, &l.loader)
if err != nil {
if _, err := toml.Decode(RulesStrict, &l.loader); err != nil {
log.Fatal().Err(err).Msg(ErrReadConfig)
}
}
Expand All @@ -64,12 +64,10 @@ func (l *Loader) Load() config.Config {
return config
}

// FromFile decodes the configuration
// from a local file.
// FromFile decodes a gitleaks config from a local file.
func FromFile(file string) LoaderOption {
return func(l *Loader) {
_, err := toml.DecodeFile(file, &l.loader)
if err != nil {
if _, err := toml.DecodeFile(file, &l.loader); err != nil {
log.Fatal().Err(err).Msg(ErrReadConfig)
}
}
Expand Down
Loading

0 comments on commit 6b39bd2

Please sign in to comment.