-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
137 lines (101 loc) · 3.31 KB
/
Copy pathcli.py
File metadata and controls
137 lines (101 loc) · 3.31 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3
import tomllib
from dataclasses import dataclass
from pathlib import Path
from typing import Annotated
import typer
from termcolor import cprint
import optypes.types as types
from settings import settings
from utils.metric import get_metric
from utils.ping import ping_agent
from utils.uninstall import uninstall_function
from utils.update import update_function
app = typer.Typer(
help="OpenHubble CLI",
no_args_is_help=True,
add_completion=False,
)
@dataclass
class AgentConfig:
host: str = "127.0.0.1"
port: int = 9703
key: str = "apikey"
use_https: bool = False
def load_config(path: Path | None) -> AgentConfig:
config = AgentConfig()
if path is None:
return config
with path.open("rb") as f:
data = tomllib.load(f)
return AgentConfig(
host=data.get("host", config.host),
port=data.get("port", config.port),
key=data.get("key", config.key),
use_https=data.get("use_https", config.use_https),
)
def show_version(value: bool = True):
if value:
cprint(
f"OpenHubble CLI {settings.project_version}",
"cyan",
attrs=["bold"],
)
raise typer.Exit()
@app.callback()
def main(
version: Annotated[
bool | None,
typer.Option(
"--version", "-v",
callback=show_version,
is_eager=True,
help="Show application version"
)
] = None
):
pass
@app.command("version", rich_help_panel="CLI", help="Show application version")
def version():
show_version()
@app.command("update", rich_help_panel="CLI", help="Update application")
def update(force: types.force = False):
update_function(force)
@app.command("uninstall", rich_help_panel="CLI", help="Uninstall application")
def uninstall(force: types.force = False):
uninstall_function(force)
@app.command("ping", rich_help_panel="Ping Agent", help="Ping Agent server")
def ping(
host: types.host_type = "127.0.0.1",
port: types.port_type = 9703,
key: types.key_type = "apikey",
use_https: types.use_https_type = False
):
ping_agent(host, port, key, use_https)
@app.command("pingf", rich_help_panel="Ping Agent", help="Ping Agent server with a toml file")
def ping_file(config_file: types.config_file_type):
conf = load_config(config_file)
host = conf.host
port = conf.port
key = conf.key
use_https = conf.use_https
ping_agent(host, port, key, use_https)
@app.command("metric", rich_help_panel="Get Metrics", help="Get metric from agent")
def get_metrics(
host: types.host_type = "127.0.0.1",
port: types.port_type = 9703,
key: types.key_type = "apikey",
use_https: types.use_https_type = False,
metric: types.metric_type = "agent.hostname"
):
get_metric(host, port, key, use_https, metric)
@app.command("metricf", rich_help_panel="Get Metrics", help="Get metric from agent with a toml file")
def get_metrics_file(config_file: types.config_file_type, metric: types.metric_type = "agent.hostname"):
conf = load_config(config_file)
host = conf.host
port = conf.port
key = conf.key
use_https = conf.use_https
get_metric(host, port, key, use_https, metric)
if __name__ == "__main__":
app()