Skip to content

Commit

Permalink
feat: lookup for configuration in parent directory (#305)
Browse files Browse the repository at this point in the history
If salt-lint cannot find a configuration file in the current directory it attempts to locate it in a parent directory. However salt-lint does not try to load configuration that is outside the git repository.

---------

Signed-off-by: Roald Nefs <info@roaldnefs.com>
Co-authored-by: Roald Nefs <info@roaldnefs.com>
  • Loading branch information
dgo- and roaldnefs authored Jan 7, 2024
1 parent 2f12381 commit fc5151a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]
### Added
- Add Python 3.12 support ([#315](https://github.com/warpnet/salt-lint/pull/315)).
- Lookup configuration file in parent directory ([#305](https://github.com/warpnet/salt-lint/pull/305)).

### Fixed
- Ignore false positive result in rule 210 ([#303](https://github.com/warpnet/salt-lint/pull/303)).
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ jobs:
Salt-lint supports local configuration via a `.salt-lint` configuration file. Salt-lint checks the working directory for the presence of this file and applies any configuration found there. The configuration file location can also be overridden via the `-c path/to/file` CLI flag.

If salt-lint cannot find a configuration file in the current directory it attempts to locate it in a parent directory. However salt-lint does not try to load configuration that is outside the git repository.

If a value is provided on both the command line and via a configuration file, the values will be merged (if a list like **exclude_paths**), or the **True** value will be preferred, in the case of something like **quiet**.

The following values are supported, and function identically to their CLI counterparts:
Expand Down
28 changes: 23 additions & 5 deletions saltlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class Configuration(object):

def __init__(self, options={}):
self._options = options
# Configuration file to use, defaults to ".salt-lint".
config = options.get('c')
file = config if config is not None else '.salt-lint'

# Read the file contents
if os.path.exists(file):
with open(file, 'r', encoding="UTF-8") as f:
# Configuration file is not set, defaults to ".salt-lint".
if config is None:
config = get_config_path()

# Read the configuration file contents if it exists.
if config and os.path.exists(config):
with open(config, 'r', encoding="UTF-8") as f:
content = f.read()
else:
content = None
Expand Down Expand Up @@ -130,3 +132,19 @@ def is_file_ignored(self, filepath, rule):
if rule not in self.rules or 'ignore' not in self.rules[rule]:
return False
return self.rules[rule]['ignore'].match_file(filepath)


def get_config_path():
"""Return local configuration file."""
dirname = basename = os.getcwd()
while basename:
filename = os.path.abspath(os.path.join(dirname, ".salt-lint"))
if os.path.exists(filename):
return filename
if os.path.exists(os.path.abspath(os.path.join(dirname, ".git"))):
# Avoid looking outside .git folders as we do not want end-up
# picking config files from upper level projects if current
# project has no config.
return None
(dirname, basename) = os.path.split(dirname)
return None

0 comments on commit fc5151a

Please sign in to comment.