-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Command line to create a .precli.toml default config file
This new CLI enables a user to create a default config file (default to .precli.toml) in the current directory. This config file is available for users to customize the precli tool and its rules. Signed-off-by: Eric Brown <[email protected]>
- Loading branch information
Showing
14 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2024 Secure Sauce LLC | ||
# SPDX-License-Identifier: BUSL-1.1 | ||
import argparse | ||
import sys | ||
from argparse import Namespace | ||
|
||
import tomli_w | ||
|
||
from precli.core import loader | ||
|
||
|
||
def setup_arg_parser() -> Namespace: | ||
parser = argparse.ArgumentParser( | ||
description="precli-init - create default configuration file", | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
|
||
parser.add_argument( | ||
"-o", | ||
"--output", | ||
dest="output", | ||
action="store", | ||
default=".precli.toml", | ||
help="output the config to given file", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
return args | ||
|
||
|
||
def get_config() -> dict: | ||
parsers = loader.load_extension(group="precli.parsers") | ||
rules = [r for p in parsers.values() for r in p.rules.values()] | ||
|
||
config = {"rule": {}} | ||
|
||
for rule in rules: | ||
config["rule"][rule.id] = { | ||
"enabled": rule.config.enabled, | ||
"level": rule.config.level, | ||
} | ||
if rule.config.parameters: | ||
for parameter, value in rule.config.parameters.items(): | ||
config["rule"][rule.id][parameter] = value | ||
|
||
return config | ||
|
||
|
||
def main(): | ||
# Setup the command line arguments | ||
args = setup_arg_parser() | ||
|
||
# Fetch the default configuration | ||
config = get_config() | ||
|
||
# Write to the given file | ||
try: | ||
# TODO: check if file already exists and prompt to overwrite | ||
with open(args.output, "wb") as f: | ||
tomli_w.dump(config, f) | ||
except OSError: | ||
print(f"Error writing to file: {args.output}") | ||
return 1 | ||
else: | ||
print(f"Default config written to file: {args.output}") | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ | |
```toml | ||
enabled = true | ||
level = "warning" | ||
parameters.umask = 0o022 | ||
umask = 0o022 | ||
``` | ||
## See also | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ | |
```toml | ||
enabled = true | ||
level = "warning" | ||
parameters.umask = 0o022 | ||
umask = 0o022 | ||
``` | ||
## See also | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters