-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path__init__.py
More file actions
48 lines (38 loc) · 1.55 KB
/
__init__.py
File metadata and controls
48 lines (38 loc) · 1.55 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
import click
from code42cli.errors import Code42CLIError
from code42cli.logger import get_logger_for_server
from code42cli.logger.enums import ServerProtocol
from code42cli.output_formats import OutputFormat
def _try_get_logger_for_server(hostname, protocol, output_format, certs):
try:
return get_logger_for_server(hostname, protocol, output_format, certs)
except Exception as err:
raise Code42CLIError(
f"Unable to connect to {hostname}. Failed with error: {err}."
)
class SendToCommand(click.Command):
def invoke(self, ctx):
certs = ctx.params.get("certs")
hostname = ctx.params.get("hostname")
protocol = ctx.params.get("protocol")
output_format = ctx.params.get("format", OutputFormat.RAW)
ignore_cert_validation = ctx.params.get("ignore_cert_validation")
_handle_incompatible_args(protocol, ignore_cert_validation, certs)
if ignore_cert_validation:
certs = "ignore"
ctx.obj.logger = _try_get_logger_for_server(
hostname, protocol, output_format, certs
)
return super().invoke(ctx)
def _handle_incompatible_args(protocol, ignore_cert_validation, certs):
if protocol == ServerProtocol.TLS_TCP:
return
arg = None
if ignore_cert_validation is not None:
arg = "--ignore-cert-validation"
elif certs is not None:
arg = "--certs"
if arg is not None:
raise click.BadOptionUsage(
arg, f"'{arg}' can only be used with '--protocol {ServerProtocol.TLS_TCP}'."
)