File size: 2,251 Bytes
227e75d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
from pathlib import Path
from config.settings import Settings
from core.git_manager import GitManager
from core.file_scanner import FileScanner
from utils.file_writer import FileWriter

def main():
    # コマンドライン引数からパスを取得
    if len(sys.argv) != 2:
        print("Usage: python main.py <github_url or directory_path>")
        return 1
    
    target_path = sys.argv[1]
    timestamp = Settings.get_timestamp()
    output_file = Settings.get_output_file(timestamp)
    
    # GitHubのURLかローカルパスかを判定
    is_github = target_path.startswith(('http://', 'https://')) and 'github.com' in target_path
    
    try:
        if is_github:
            # GitHubリポジトリの場合
            clone_dir = Settings.get_clone_dir(timestamp)
            print(f"Cloning repository: {target_path}")
            
            git_manager = GitManager(target_path, clone_dir)
            git_manager.clone_repository()
            
            scanner = FileScanner(clone_dir)
            cleanup_needed = True
        else:
            # ローカルディレクトリの場合
            target_dir = Path(target_path)
            if not target_dir.exists():
                print(f"Error: Directory not found: {target_dir}")
                return 1
                
            scanner = FileScanner(target_dir)
            cleanup_needed = False
        
        # ファイルスキャンと保存
        print("Scanning files...")
        files = scanner.scan_files()
        
        print(f"Writing contents to {output_file}")
        writer = FileWriter(output_file)
        writer.write_contents(files)
        
        print(f"Found {len(files)} files")
        print(f"Results saved to {output_file}")
        
    except Exception as e:
        print(f"Error: {e}")
        return 1
        
    finally:
        # GitHubリポジトリの場合はクリーンアップ
        if is_github and cleanup_needed and 'git_manager' in locals():
            try:
                git_manager.cleanup()
                print("Cleanup completed")
            except Exception as e:
                print(f"Cleanup error: {e}")
    
    return 0

if __name__ == "__main__":
    exit(main())