Skip to content

Commit

Permalink
give a better error message when no configuration file is provided or…
Browse files Browse the repository at this point in the history
… exists, fixes gjcarneiro#72
  • Loading branch information
gjcarneiro committed Dec 31, 2022
1 parent f5240be commit 391fda2
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions yacron/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
import logging
import signal
import sys
import os

from yacron.cron import Cron, ConfigError
import yacron.version

CONFIG_DEFAULT = "/etc/yacron.d"


def main_loop(loop):
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(prog="yacron")
parser.add_argument(
"-c", "--config", default="/etc/yacron.d", metavar="FILE-OR-DIR"
"-c",
"--config",
default=CONFIG_DEFAULT,
metavar="FILE-OR-DIR",
help="configuration file, or directory containing configuration files",
)
parser.add_argument("-l", "--log-level", default="INFO")
parser.add_argument("-v", "--validate-config", default=False, action="store_true")
parser.add_argument(
"-v", "--validate-config", default=False, action="store_true"
)
parser.add_argument("--version", default=False, action="store_true")
args = parser.parse_args()

Expand All @@ -27,6 +36,15 @@ def main_loop(loop):
print(yacron.version.version)
sys.exit(0)

if args.config == CONFIG_DEFAULT and not os.path.exists(args.config):
print(
"yacron error: configuration file not found, please provide one "
"with the --config option",
file=sys.stderr,
)
parser.print_help(sys.stderr)
sys.exit(1)

try:
cron = Cron(args.config)
except ConfigError as err:
Expand Down

0 comments on commit 391fda2

Please sign in to comment.