Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sc-12541 allow for multiple dir trees to walk #20

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This utility is distributed as a Docker container and can be pulled from cloudtr
You can feed a directory of files into the `walk-directories` command, which will find all files matching the supplied types and parse them into CloudTruth config formats. If you supply your CLOUDTRUTH_API_KEY via docker, the data will be uploaded to your CloudTruth account.

```
docker run --rm -e CLOUDTRUTH_API_KEY="myverysecureS3CR3T!!" -v ${PWD}/files:/app/files cloudtruth/dynamic-importer walk-directories --config-dir /app/samples/ -t dotenv -t json -t tf
docker run --rm -e CLOUDTRUTH_API_KEY="myverysecureS3CR3T!!" -v ${PWD}/files:/app/files cloudtruth/dynamic-importer walk-directories --config-dirs /app/samples/ -t dotenv -t json -t tf
```

## Processing a single file
Expand Down
40 changes: 22 additions & 18 deletions src/dynamic_importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@

@import_config.command()
@click.option(
"--config-dir",
"--config-dirs",
help="Full path to directory to walk and locate configs",
required=True,
multiple=True,
)
@click.option(
"-t",
Expand Down Expand Up @@ -293,30 +294,33 @@
@click.option("-c", help="Create missing projects and enviroments", is_flag=True)
@click.option("-u", help="Upsert values", is_flag=True)
def walk_directories(
config_dir, file_types, exclude_dirs, create_hierarchy, parse_descriptions, k, c, u
config_dirs, file_types, exclude_dirs, create_hierarchy, parse_descriptions, k, c, u
):
"""
Walks a directory, constructs templates and config data, and uploads to CloudTruth.
This is an interactive version of the process_configs and create_data commands. The
user will be prompted for project and environment names as files are walked.
"""
walked_files = {}
for root, dirs, files in os.walk(config_dir):
root = root.rstrip("/")

# skip over known non-config directories
for dir in DIRS_TO_IGNORE:
if dir in dirs:
dirs.remove(dir)

# skip over user-specified non-config directories
for dir in exclude_dirs:
dir = dir.rstrip("/")
if os.path.abspath(dir) in [f"{os.path.abspath(root)}/{d}" for d in dirs]:
click.echo(f"Excluding directory: {os.path.abspath(dir)}")
dirs.remove(os.path.basename(dir))

walked_files.update(walk_files(root, files, file_types, create_hierarchy))
for config_dir in config_dirs:
for root, dirs, files in os.walk(config_dir):
root = root.rstrip("/")

# skip over known non-config directories
for dir in DIRS_TO_IGNORE:
if dir in dirs:
dirs.remove(dir)

Check warning on line 312 in src/dynamic_importer/main.py

View check run for this annotation

Codecov / codecov/patch

src/dynamic_importer/main.py#L312

Added line #L312 was not covered by tests

# skip over user-specified non-config directories
for dir in exclude_dirs:
dir = dir.rstrip("/")
if os.path.abspath(dir) in [
f"{os.path.abspath(root)}/{d}" for d in dirs
]:
click.echo(f"Excluding directory: {os.path.abspath(dir)}")
dirs.remove(os.path.basename(dir))

walked_files.update(walk_files(root, files, file_types, create_hierarchy))

project_files = defaultdict(lambda: defaultdict(list))
for v in walked_files.values():
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_walk_directories_parse_descriptions(self, mock_client):
"walk-directories",
"-t",
"yaml",
"--config-dir",
"--config-dirs",
f"{current_dir}/../../samples/advanced",
"-c",
"-u",
Expand Down
8 changes: 4 additions & 4 deletions src/tests/test_directory_walking.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_walk_directories_one_file_type(mock_client):
"walk-directories",
"-t",
"dotenv",
"--config-dir",
"--config-dirs",
f"{current_dir}/../../samples/dotenvs",
"-c",
"-u",
Expand Down Expand Up @@ -147,7 +147,7 @@ def test_walk_directories_multiple_file_types(mock_client):
"walk-directories",
"-t",
"dotenv",
"--config-dir",
"--config-dirs",
f"{current_dir}/../../samples",
],
input="\n".join(
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_walk_directories_with_exclusion(mock_client):
"dotenv",
"-t",
"yaml",
"--config-dir",
"--config-dirs",
f"{current_dir}/../../samples",
"--exclude-dirs",
f"{current_dir}/../../samples/dotenvs",
Expand Down Expand Up @@ -261,7 +261,7 @@ def test_walk_directories_with_inheritance(mock_client):
"yaml",
"--create-hierarchy",
"-c",
"--config-dir",
"--config-dirs",
f"{current_dir}/../../samples",
],
input="\n".join(prompt_responses),
Expand Down
Loading