File size: 1,301 Bytes
ee9a09c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import json
from pathlib import Path

import requests


def index_local(api_url: str, path: str) -> dict:
    response = requests.post(
        f"{api_url.rstrip('/')}/repos/local",
        json={"path": str(Path(path).resolve())},
        timeout=240,
    )
    response.raise_for_status()
    return response.json()


def index_github(api_url: str, url: str) -> dict:
    response = requests.post(
        f"{api_url.rstrip('/')}/repos/github",
        json={"url": url},
        timeout=300,
    )
    response.raise_for_status()
    return response.json()


def main() -> None:
    parser = argparse.ArgumentParser(description="Seed the app by indexing a repository.")
    parser.add_argument("--api-url", default="http://localhost:8000/api")
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("--local-path", help="Local repository path to index.")
    group.add_argument("--github-url", help="GitHub repository URL to clone and index.")
    args = parser.parse_args()

    if args.local_path:
        summary = index_local(args.api_url, args.local_path)
    else:
        summary = index_github(args.api_url, args.github_url)

    print(json.dumps(summary, indent=2))


if __name__ == "__main__":
    main()