~michel-slm/myrepos-utils

4dc111cade46afcde97270f9ede6eeac855456d4 — Michel Alexandre Salim 2 years ago c165172
Implement `mr-utils find`
3 files changed, 45 insertions(+), 6 deletions(-)

A README.md
M myrepos_utils/cli.py
M myrepos_utils/utils.py
A README.md => README.md +20 -0
@@ 0,0 1,20 @@
# myrepos-utils

## Usage

Let's say you have the following repositories configured in `~/.mrconfig`:

```
[src/github/owner1/projA]
...

[src/github/owner2/projB]
...
```

This will let you quickly switch to `~/src/github/owner1/projA`:
```
cd (mr-utils find github projA)
```

If there are multiple matches, they will be printed out.

M myrepos_utils/cli.py => myrepos_utils/cli.py +6 -4
@@ 23,9 23,11 @@ def cli():
    pass


@cli.command(help="cd to a directory tracked by myrepos")
@click.argument("paths", nargs=-1)
def cd(paths: list[str]):
@cli.command(help="find repo directories tracked by myrepos")
@click.argument("query", nargs=-1)
def find(query: list[str]):
    import os

    os.chdir(utils.find_dir(paths))
    matches = utils.find_repo(query)
    for repo in matches:
        click.echo(repo)

M myrepos_utils/utils.py => myrepos_utils/utils.py +19 -2
@@ 12,5 12,22 @@
# For more information on free software, see
# <https://www.gnu.org/philosophy/free-sw.en.html>.

def find_dir(paths: list[str], config = None):
    raise NotImplementedError
import configparser
import os

CONFIG = configparser.ConfigParser()
CONFIG.read(os.path.expanduser("~/.mrconfig"))


def get_repos(config: configparser.ConfigParser = CONFIG) -> list[str]:
    return list(config.keys())


def find_repo(
    query: list[str], config: configparser.ConfigParser = CONFIG
) -> list[str]:
    import re

    repos = get_repos(config)
    r = f".*{'.*'.join(query)}.*"
    return [os.path.expanduser(f"~/{repo}") for repo in repos if re.match(r, repo)]