#!/Users/chranj/homebrew/opt/python/bin/python3.7

"""Patter command line parser/script."""

import sys
import argparse

from patter.exceptions import MissingPipeException
from patter.patter import Patter


parser = argparse.ArgumentParser(
    description="Send stdout directly to a Mattermost channel or user."
)
parser.add_argument(
    "-p",
    help="Don't format message as code",
    action="store_false",
)
parser.add_argument(
    "-s",
    "--syntax",
    help="Syntax highlighting (default none)",
    default="",
)
parser.add_argument(
    "-v",
    "--verbose",
    help="verbose output",
    action="store_true",
)
parser.add_argument("-f", "--file", help="file to attach")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-c", "--channel", help="recipient channel")
group.add_argument("-u", "--user", help="recipient user")

try:
    args = parser.parse_args()
except ValueError:
    parser.print_help()
    sys.exit(1)

message = None
if not sys.stdin.isatty():
    message = sys.stdin.read()
else:
    if not args.file:
        raise MissingPipeException("patter must receive input from stdin")

patter = Patter(
    message,
    args.file,
    args.p,
    args.user,
    args.channel,
    args.syntax,
    args.verbose,
)
patter.check_env_vars()
patter.send_message()
