Skip to content

Commit

Permalink
Add --config-file option
Browse files Browse the repository at this point in the history
And add documentation for custom configuration.

Fixes #20

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Mar 21, 2023
1 parent 0fa86e1 commit fa79347
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ with linting.

## Try it out!

Run `regal` pointed at one or more files or directories to have them linted:
Run `regal lint` pointed at one or more files or directories to have them linted:

```shell
./regal policy/
regal lint policy/
```

## Rules
Expand All @@ -39,7 +39,24 @@ See: https://github.com/StyraInc/regal/issues/36

## Configuration

TODO
A custom configuration file may be used to override the [default configuration](bundle/regal/config/data.yaml) options
provided by Regal. This is particularly useful for e.g. enabling/disabling certain rules, or to provide more
fine-grained options for the linter rules that support it.

**.regal/config.yaml**
```yaml
rules:
style:
prefer-snake-case:
enabled: false
```
Regal will automatically search for a configuration file (`.regal/config.yaml`) in the current directory, and if not
found, traverse the parent directories either until either one is found, or the top of the directory hierarchy is
reached. If no configuration file is found, Regal will use the default configuration.

A custom configuration may be also be provided using the `--config-file`/`-c` option for `regal lint`, which when
provided will be used to override the default configuration.

## Development

Expand Down
34 changes: 23 additions & 11 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

type lintCommandParams struct {
timeout time.Duration
timeout time.Duration
configFile string
}

//nolint:gochecknoglobals
Expand Down Expand Up @@ -51,6 +52,7 @@ func init() {
},
}

lintCommand.Flags().StringVarP(&params.configFile, "config-file", "c", "", "set path of configuration file")
lintCommand.Flags().DurationVar(&params.timeout, "timeout", 0, "set timeout for linting (default unlimited)")

RootCommand.AddCommand(lintCommand)
Expand All @@ -77,22 +79,32 @@ func lint(args []string, params lintCommandParams) error {

policies, err := loader.AllRegos(args)
if err != nil {
return fmt.Errorf("failed to load policy from provided args %w", err)
}

// TODO: Allow user-provided path to config
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get cwd %w", err)
return fmt.Errorf("failed to load policy from provided args: %w", err)
}

regal := linter.NewLinter().WithAddedBundle(regalRules)

userConfig, err := config.FindConfig(cwd)
if err == nil {
defer rio.CloseFileIgnore(userConfig)
if params.configFile != "" {
userConfig, err := os.Open(params.configFile)
if err != nil {
return fmt.Errorf("failed to open config file %w", err)
}

regal = regal.WithUserConfig(rio.MustYAMLToMap(userConfig))
} else {
cwd, err := os.Getwd()
if err != nil {
// Should perhaps just log this?
return fmt.Errorf("failed to get cwd %w", err)
}

// Try find .regal/config.yaml in current, or any parent directory
userConfig, err := config.FindConfig(cwd)
if err == nil {
defer rio.CloseFileIgnore(userConfig)

regal = regal.WithUserConfig(rio.MustYAMLToMap(userConfig))
}
}

rep, err := regal.Lint(ctx, policies)
Expand Down

0 comments on commit fa79347

Please sign in to comment.