Skip to content

Instantly share code, notes, and snippets.

@mshafiee
Created November 7, 2024 09:22
Show Gist options
  • Save mshafiee/03b72674f144316afaf0a28e3e3b4d18 to your computer and use it in GitHub Desktop.
Save mshafiee/03b72674f144316afaf0a28e3e3b4d18 to your computer and use it in GitHub Desktop.
import os
import sys
def collect_go_files_content(directory):
content = ""
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".go"):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
file_content = f.read()
content += f"# {file_path}\n```go\n{file_content}\n```\n\n"
return content
def main():
# Check if directory path is provided as command line argument
if len(sys.argv) != 2:
print("Usage: python script.py <directory_path>")
sys.exit(1)
directory = sys.argv[1]
# Check if the provided directory exists
if not os.path.isdir(directory):
print(f"The directory {directory} does not exist.")
sys.exit(1)
# Collect content from .go files
content = collect_go_files_content(directory)
# Write content to code.md
with open("code.md", 'w', encoding='utf-8') as output_file:
output_file.write(content)
print("Collected source code written to code.md")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment