From 3513ae71655b05fff01bc90e2f667b998a58d067 Mon Sep 17 00:00:00 2001 From: Kirill Markin Date: Tue, 18 Jun 2024 11:56:27 +0200 Subject: [PATCH] add --create-settings option --- README.md | 8 ++++++++ repo_to_text/main.py | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d359974..820da48 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,14 @@ You can customize the behavior of `repo-to-text` with the following options: This will save the file in the specified output directory instead of the current directory. +- `--create-settings`: Create a default `.repo-to-text-settings.yaml` file with predefined settings. This is useful if you want to start with a template settings file and customize it according to your needs. To create the default settings file, run the following command in your terminal: + + ```bash + repo-to-text --create-settings + ``` + + This will create a file named `.repo-to-text-settings.yaml` in the current directory. If the file already exists, an error will be raised to prevent overwriting. + - `--debug`: Enable DEBUG logging. By default, `repo-to-text` runs with INFO logging level. To enable DEBUG logging, use the `--debug` flag: ```bash diff --git a/repo_to_text/main.py b/repo_to_text/main.py index 79c094e..c7f0c9e 100644 --- a/repo_to_text/main.py +++ b/repo_to_text/main.py @@ -5,6 +5,7 @@ import logging import argparse import yaml from datetime import datetime +import textwrap # Importing the missing pathspec module import pathspec @@ -196,15 +197,49 @@ def save_repo_to_text(path='.', output_dir=None) -> str: return output_file +def create_default_settings_file(): + settings_file = '.repo-to-text-settings.yaml' + if os.path.exists(settings_file): + raise FileExistsError(f"The settings file '{settings_file}' already exists. Please remove it or rename it if you want to create a new default settings file.") + + default_settings = textwrap.dedent("""\ + # Details: https://github.com/kirill-markin/repo-to-text + # Syntax: gitignore rules + + # Ignore files and directories for all sections from gitignore file + # Default: True + gitignore-import-and-ignore: True + + # Ignore files and directories for tree + # and "Contents of ..." sections + ignore-tree-and-content: + - ".repo-to-text-settings.yaml" + + # Ignore files and directories for "Contents of ..." section + ignore-content: + - "README.md" + - "LICENSE" + """) + with open('.repo-to-text-settings.yaml', 'w') as f: + f.write(default_settings) + print("Default .repo-to-text-settings.yaml created.") + def main(): parser = argparse.ArgumentParser(description='Convert repository structure and contents to text') parser.add_argument('--debug', action='store_true', help='Enable debug logging') parser.add_argument('--output-dir', type=str, help='Directory to save the output file') + parser.add_argument('--create-settings', action='store_true', help='Create default .repo-to-text-settings.yaml file') # Новый аргумент args = parser.parse_args() setup_logging(debug=args.debug) logging.debug('repo-to-text script started') - save_repo_to_text(output_dir=args.output_dir) + + if args.create_settings: + create_default_settings_file() + logging.debug('.repo-to-text-settings.yaml file created') + else: + save_repo_to_text(output_dir=args.output_dir) + logging.debug('repo-to-text script finished') if __name__ == '__main__':