Skip to content

Commit 198b0e7

Browse files
authored
feat: Add --version and --create-config options (#41)
1 parent 9d1a977 commit 198b0e7

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

cloud2sql/__main__.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from logging import getLogger
22
from typing import Optional
3+
4+
import yaml
35
from resotolib.args import Namespace, ArgumentParser
46
from resotolib.logger import setup_logger
57
from sqlalchemy import create_engine
68
from sqlalchemy.engine import Engine
79

10+
from cloud2sql import __version__
811
from cloud2sql.analytics import PosthogEventSender, NoEventSender, AnalyticsEventSender
9-
from cloud2sql.collect_plugins import collect_from_plugins, configure
12+
from cloud2sql.collect_plugins import collect_from_plugins, configure, default_config
1013
from cloud2sql.util import db_string_from_config, check_parquet_driver
1114

1215
# Will fail in case snowflake is not installed - which is fine.
@@ -21,11 +24,11 @@
2124
def parse_args() -> Namespace:
2225
parser = ArgumentParser(
2326
description="Collect data from cloud providers and store it in a database",
24-
epilog="Synchronizes cloud data to a database",
27+
epilog="Synchronizes cloud data to a database. Visit https://cloud2sql.com for more information.",
2528
env_args_prefix="CLOUD2SQL_",
2629
)
2730
parser.add_argument("--debug", action="store_true", help="Enable debug logging")
28-
parser.add_argument("--config", help="Path to config file", required=True)
31+
parser.add_argument("--config", help="Path to config file")
2932
parser.add_argument(
3033
"--show",
3134
choices=["progress", "log", "none"],
@@ -36,9 +39,29 @@ def parse_args() -> Namespace:
3639
"--analytics-opt-out",
3740
default=False,
3841
action="store_true",
39-
help="Do not send anonimized analytics data",
42+
help="Do not send anonymized analytics data",
43+
)
44+
parser.add_argument(
45+
"--version",
46+
default=False,
47+
action="store_true",
48+
help="Print version and exit",
49+
)
50+
parser.add_argument(
51+
"--create-config",
52+
action="store_true",
53+
help="Print empty configuration and exit. Use this to create your own config file.",
4054
)
4155
args = parser.parse_args()
56+
if args.version:
57+
print(f"Cloud2SQL Version {__version__}")
58+
exit(0)
59+
elif args.create_config:
60+
cfg = {"sources": default_config(), "destinations": {"sqlite": {"database": "cloud2sql.db"}}}
61+
print(yaml.safe_dump(cfg, sort_keys=False))
62+
exit(0)
63+
elif not args.config:
64+
parser.error("The following arguments are required: --config")
4265
args.log_level = "CRITICAL" if args.show != "log" else "DEBUG" if args.debug else "INFO"
4366
return args # type: ignore
4467

@@ -59,7 +82,7 @@ def main() -> None:
5982
sender = NoEventSender() if args.analytics_opt_out else PosthogEventSender()
6083
config = configure(args.config)
6184
engine = None
62-
if config["destinations"].keys() & set(["file", "s3", "gcs"]):
85+
if config["destinations"].keys() & {"file", "s3", "gcs"}:
6386
check_parquet_driver()
6487
else:
6588
engine = create_engine(db_string_from_config(config))

cloud2sql/collect_plugins.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from resotolib.config import Config
2121
from resotolib.core.actions import CoreFeedback
2222
from resotolib.core.model_export import node_to_dict
23-
from resotolib.json import from_json
23+
from resotolib.json import from_json, to_json
2424
from resotolib.proc import emergency_shutdown
2525
from resotolib.types import Json
2626
from rich import print as rich_print
@@ -45,6 +45,17 @@
4545
log = getLogger("resoto.cloud2sql")
4646

4747

48+
def default_config() -> Json:
49+
config: Config = Config # type: ignore
50+
for entry_point in pkg_resources.iter_entry_points("resoto.plugins"):
51+
plugin_class = entry_point.load()
52+
if issubclass(plugin_class, BaseCollectorPlugin):
53+
plugin_class.add_config(config)
54+
55+
Config.init_default_config()
56+
return to_json(Config.running_config.data)
57+
58+
4859
def collectors(raw_config: Json, feedback: CoreFeedback) -> Dict[str, BaseCollectorPlugin]:
4960
result = {}
5061
config: Config = Config # type: ignore
@@ -149,7 +160,7 @@ def collect(
149160
if engine:
150161
return collect_sql(collector, engine, feedback, args)
151162
else:
152-
if not set(["file", "s3", "gcs"]) & config["destinations"].keys():
163+
if not {"file", "s3", "gcs"} & config["destinations"].keys():
153164
raise ValueError("No file destination configured")
154165
return collect_to_file(collector, feedback, config["destinations"]["arrow"])
155166

0 commit comments

Comments
 (0)