Skip to content

Commit c54c686

Browse files
committed
feat: add docker-find script
1 parent 5dd7350 commit c54c686

8 files changed

Lines changed: 128 additions & 23 deletions

File tree

clit/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Configuration helpers."""
22
import json
33
import os
4+
from pathlib import Path
45
from typing import List
56

6-
from clit.constants import CONFIG_DIR
7+
CONFIG_DIR = Path("~/.config/dotfiles/").expanduser()
78

89

910
class JsonConfig:

clit/constants.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

clit/db.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from pathlib import Path
44
from typing import List, Optional
55

6-
from clit.constants import POSTGRES_DOCKER_CONTAINER_NAME
76
from clit.docker import DockerContainer
87
from clit.files import existing_directory_type, existing_file_type, shell
98

9+
POSTGRES_DOCKER_CONTAINER_NAME = "postgres10"
10+
1011

1112
class DatabaseServer:
1213
"""A database server URI parser."""

clit/dev.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Development helpers."""
22
import os
3+
import re
34
from pathlib import Path
45
from shutil import rmtree
56
from subprocess import call, check_output
@@ -10,10 +11,17 @@
1011
from plumbum import FG, RETCODE
1112
from requests_html import HTMLSession
1213

13-
from clit.constants import PYCHARM_MACOS_APP_PATH, TEST_NAMES_REGEX
1414
from clit.files import shell
1515
from clit.ui import prompt
1616

17+
# Possible formats for tests:
18+
# ___ test_name ___
19+
# ___ Error on setup of test_name ___
20+
# ___ test_name[Parameter] ___
21+
TEST_NAMES_REGEX = re.compile(r"___ .*(test[^\[\] ]+)[\[\]A-Za-z]* ___")
22+
23+
PYCHARM_MACOS_APP_PATH = Path("/Applications/PyCharm.app/Contents/MacOS/pycharm")
24+
1725

1826
@click.command()
1927
@click.argument("files", nargs=-1)
@@ -222,7 +230,7 @@ def setup_py():
222230
1,
223231
dedent(
224232
'''
225-
"""NOTICE: This file was generated automatically by the command: poetry-setup-py."""
233+
"""NOTICE: This file was generated automatically by the command: xpoetry setup-py."""
226234
'''
227235
).strip(),
228236
)

clit/docker.py

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
"""Docker module."""
2+
import argparse
23
import json
34
from pathlib import Path
5+
from subprocess import PIPE
46
from typing import List
57

6-
from clit.files import shell
8+
from clit.config import JsonConfig
9+
from clit.files import existing_directory_type, shell, shell_find
710
from clit.types import JsonDict
811

12+
YML_DIRS = JsonConfig("docker-find-yml-dirs.json")
13+
YML_FILES = JsonConfig("docker-find-yml-files.json")
14+
915

1016
class DockerContainer:
1117
"""A helper for Docker containers."""
@@ -31,3 +37,103 @@ def replace_mount_dir(self, path: Path) -> Path:
3137
new_path = str(path).replace(source, mount["Destination"])
3238
return Path(new_path)
3339
return path
40+
41+
42+
def rescan_files(dirs):
43+
"""Rescan all directories and save the yml files that were found."""
44+
sorted_dirs = sorted(dirs)
45+
YML_DIRS.dump(sorted_dirs)
46+
47+
files = set()
48+
for dir in sorted_dirs:
49+
print(f"Files on {dir}")
50+
for file in shell_find(f"{dir} -name docker-compose.yml"):
51+
print(f" {file}")
52+
files.add(str(file))
53+
sorted_files = sorted(files)
54+
YML_FILES.dump(sorted_files)
55+
56+
57+
def scan_command(parser, args):
58+
"""Scan directories and add them to the list."""
59+
dirs = YML_DIRS.load_set()
60+
if not args.dir:
61+
print(f"Rescanning existing directories")
62+
for dir in args.dir:
63+
dirs.add(str(dir))
64+
print(f"Directory added: {dir}")
65+
rescan_files(dirs)
66+
67+
68+
def rm_command(parser, args):
69+
"""Remove directories from the list."""
70+
dirs = YML_DIRS.load_set()
71+
for one_dir in args.dir:
72+
str_dir = str(one_dir)
73+
if str_dir in dirs:
74+
dirs.remove(str_dir)
75+
print(f"Directory removed: {one_dir}")
76+
else:
77+
print(f"Directory was not configured: {one_dir}")
78+
rescan_files(dirs)
79+
80+
81+
def ls_command(parser, args):
82+
"""List registered yml files."""
83+
for yml_file in sorted(YML_FILES.load_set()):
84+
print(yml_file)
85+
86+
87+
def yml_command(parser, args):
88+
"""Run a docker-compose command on one of the yml files."""
89+
found = set()
90+
partial_name = args.yml_file
91+
92+
for file in YML_FILES.load_set():
93+
if partial_name in file:
94+
found.add(file)
95+
if not found:
96+
print(f"No .yml file was found with the string '{partial_name}'")
97+
exit(1)
98+
99+
sorted_found = sorted(found)
100+
if len(sorted_found) > 1:
101+
choices = "\n".join(sorted_found)
102+
chosen_yml = shell(f"echo '{choices}' | fzf --cycle --tac", quiet=True, stdout=PIPE).stdout.strip()
103+
if not chosen_yml:
104+
print("No .yml file was chosen")
105+
exit(2)
106+
else:
107+
chosen_yml = sorted_found[0]
108+
shell(f"docker-compose -f {chosen_yml} {' '.join(args.docker_compose_arg)}")
109+
110+
111+
# TODO: Convert to click
112+
def docker_find():
113+
"""Find docker.compose.yml files."""
114+
parser = argparse.ArgumentParser(description="find docker.compose.yml files")
115+
parser.set_defaults(chosen_function=None)
116+
subparsers = parser.add_subparsers(title="commands")
117+
118+
parser_scan = subparsers.add_parser("scan", help="scan directories and add them to the list")
119+
parser_scan.add_argument("dir", nargs="*", help="directory to scan", type=existing_directory_type)
120+
parser_scan.set_defaults(chosen_function=scan_command)
121+
122+
parser_rm = subparsers.add_parser("rm", help="remove directories from the list")
123+
parser_rm.add_argument("dir", nargs="+", help="directory to remove", type=existing_directory_type)
124+
parser_rm.set_defaults(chosen_function=rm_command)
125+
126+
parser_ls = subparsers.add_parser("ls", help="list yml files")
127+
parser_ls.set_defaults(chosen_function=ls_command)
128+
129+
parser_yml = subparsers.add_parser("yml", help="choose one of the yml files to call docker-compose on")
130+
parser_yml.add_argument("yml_file", help="partial name of the desired .yml file")
131+
parser_yml.add_argument("docker_compose_arg", nargs=argparse.REMAINDER, help="docker-compose arguments")
132+
parser_yml.set_defaults(chosen_function=yml_command)
133+
134+
args = parser.parse_args()
135+
if not args.chosen_function:
136+
parser.print_help()
137+
return
138+
args.chosen_function(parser, args)
139+
return

clit/files.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from plumbum import FG
1313

1414
from clit import CONFIG, LOGGER, read_config, save_config
15-
from clit.constants import SECTION_SYMLINKS_DIRS, SECTION_SYMLINKS_FILES
15+
16+
SECTION_SYMLINKS_FILES = "symlinks/files"
17+
SECTION_SYMLINKS_DIRS = "symlinks/dirs"
1618

1719

1820
@click.command()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ classifiers = [
2424
# poetry run generate-setup-py
2525
[tool.poetry.scripts]
2626
backup-full = "clit.files:backup_full"
27+
docker-find = "clit.docker:docker_find"
2728
git-local-prune = "clit.git:prune_local_branches"
2829
git-vacuum = "clit.git:vacuum"
2930
pycharm-cli = "clit.dev:pycharm_cli"
30-
xpytest = "clit.dev:extra_pytest"
3131
pypi = "clit.dev:pypi"
3232
xpoetry = "clit.dev:extra_poetry"
3333
xpostgres = "clit.db:extra_postgres"
34+
xpytest = "clit.dev:extra_pytest"
3435

3536
[tool.poetry.dependencies]
3637
python = "^3.6 || ^3.7"

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
"""NOTICE: This file was generated automatically by the command: poetry-setup-py."""
2+
"""NOTICE: This file was generated automatically by the command: xpoetry setup-py."""
33
from distutils.core import setup
44

55
packages = ["clit"]
@@ -20,6 +20,7 @@
2020
entry_points = {
2121
"console_scripts": [
2222
"backup-full = clit.files:backup_full",
23+
"docker-find = clit.docker:docker_find",
2324
"git-local-prune = clit.git:prune_local_branches",
2425
"git-vacuum = clit.git:vacuum",
2526
"pycharm-cli = clit.dev:pycharm_cli",

0 commit comments

Comments
 (0)