-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
84 lines (63 loc) · 2.15 KB
/
cli.py
File metadata and controls
84 lines (63 loc) · 2.15 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
"""
Dokku API CLI - Simple wrapper for Makefile commands
This module provides a command-line interface that wraps the Makefile commands,
allowing users to manage Dokku API after installing from PyPI.
Usage:
dokku-api <command> # Run any Makefile command
dokku-api help # Show all available commands from Makefile
"""
import shutil
import subprocess
import sys
from pathlib import Path
try:
import dokku_api
except ImportError:
print("Install the dokku-api package first using 'pip install dokku-api'")
sys.exit(1)
def get_package_dir():
"""
Get the directory where the package is installed.
"""
return Path(dokku_api.__file__).parent
def run_make_command(command, args=None):
"""
Run a Makefile command using the config directory's Makefile.
"""
if not shutil.which("make"):
print("Error: 'make' not found. Install GNU Make first.")
return 1
try:
package_dir = get_package_dir()
makefile_path = package_dir / "Makefile"
# Ensure Makefile exists in directory
if not makefile_path.exists():
print("Error: Could not find Makefile at the Dokku API package")
return 1
# Ensure .env exists in directory
source = package_dir / ".env.sample"
destination = package_dir / ".env"
if not destination.exists():
shutil.copy(source, destination)
# Run make from the config directory where .env is located
cmd = ["make", "-f", str(makefile_path), command] + (args or [])
# Set working directory to config directory for make execution
return subprocess.run(cmd, cwd=package_dir).returncode
except Exception as e:
print(f"Error running make command: {e}")
return 1
def main():
"""
Main CLI entry point.
"""
if len(sys.argv) < 2:
print(__doc__)
return 1
command = sys.argv[1]
args = sys.argv[2:] if len(sys.argv) > 2 else []
if command in ["help", "--help", "-h"]:
return run_make_command("help")
return run_make_command(command, args)
if __name__ == "__main__":
sys.exit(main())