Skip to content

Commit

Permalink
Feat: Add input processing for multiple files and folders at the same…
Browse files Browse the repository at this point in the history
… time (#22)
  • Loading branch information
dungpham91 authored Oct 2, 2023
1 parent 217ac30 commit 0f2d2db
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,39 @@
import json
import sys

def check_folder_or_file(input_path):
def check_folder_or_file(input_paths):
"""
Check if the input is a folder, a single file, or multiple files.
Returns a list of files if it's a folder, or a list with a single file if it's a single file.
If input_path is '.', it lists all files in the current directory recursively.
Check if the input is a folder, a single file, or multiple files/folders.
Returns a list of files if they are folders, or a list with a single file/folder if it's a single file/folder.
If an input path is '.', it lists all files in the current directory recursively.
Parameters:
input_path (str): The path to the folder or file.
input_paths (str): A space-separated string of paths to files or folders.
Returns:
list: List of files if it's a folder, or a list with a single file if it's a single file.
list: List of files if they are folders, or a list with a single file/folder if it's a single file/folder.
"""
if input_path == '.':
# List all files in the current directory and its subdirectories recursively
files = []
for root, _, filenames in os.walk('.'):
for filename in filenames:
files.append(os.path.join(root, filename))
return files
elif os.path.isdir(input_path):
files = os.listdir(input_path)
return [os.path.join(input_path, file) for file in files]
elif os.path.isfile(input_path):
return [input_path]
else:
return []
input_paths = input_paths.split() # Chia chuỗi thành danh sách các đối tượng
files = []
for input_path in input_paths:
if input_path == '.':
# List all files in the current directory and its subdirectories recursively
for root, _, filenames in os.walk('.'):
for filename in filenames:
files.append(os.path.join(root, filename))
else:
# Check if the input_path is a directory
if os.path.isdir(input_path):
# List all files in the directory and its subdirectories recursively
for root, _, filenames in os.walk(input_path):
for filename in filenames:
files.append(os.path.join(root, filename))
else:
# If it's not a directory, assume it's a single file or folder
if os.path.exists(input_path):
files.append(input_path)

return files

def read_file(file_path):
"""
Expand Down Expand Up @@ -92,13 +99,13 @@ def validate_json(json_contents, file_name):
return f"No JSON content found in '{file_name}'."

def main():
input_path = os.getenv("INPUT_FILE_OR_DIR")
input_paths = os.getenv("INPUT_FILE_OR_DIR")

if not input_path:
if not input_paths:
print("The INPUT_FILE_OR_DIR environment variable is not set.")
exit(1)

files = check_folder_or_file(input_path)
files = check_folder_or_file(input_paths)
# Set a variable that tracks whether all JSON is valid or not
all_json_valid = True

Expand Down

0 comments on commit 0f2d2db

Please sign in to comment.