1
0
Fork 0
mirror of https://github.com/Findus23/new-github-changelog-generator.git synced 2024-08-27 19:52:18 +02:00
new-github-changelog-generator/generator/cli.py

62 lines
2.6 KiB
Python
Raw Normal View History

2019-02-03 18:26:13 +01:00
import argparse
2019-02-14 13:47:07 +01:00
import sys
from datetime import datetime
from os import path
from os.path import expanduser
from shutil import copyfile
import pkg_resources
2019-02-03 14:10:17 +01:00
2019-02-14 14:00:59 +01:00
from generator import generate_changelog, config
2019-02-02 22:12:08 +01:00
2019-02-03 18:26:13 +01:00
def parsed_date(s):
try:
return datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise argparse.ArgumentTypeError(msg)
2019-02-14 13:47:07 +01:00
def initfile(globally: bool):
home = expanduser("~")
targetfile = home + "/.config/github-changelog-generator.yaml" if globally else "./github-changelog-generator.yaml"
if path.exists(targetfile):
sys.exit("The config file at '{path}' already exists.".format(path=targetfile))
default_config_file = pkg_resources.resource_filename('generator', 'defaultconfig.yaml')
copyfile(default_config_file, targetfile)
print("An example config has been copied to '{path}'. \n".format(path=targetfile) +
"Please edit it to your use case and don't forget to add a GitHub api_token to not run into rate limits.")
2019-02-03 14:10:17 +01:00
2019-02-14 13:47:07 +01:00
def main():
2019-02-03 18:26:13 +01:00
parser = argparse.ArgumentParser(description='Generate changelogs from closed GitHub issues and merged PRs.')
2019-02-14 13:47:07 +01:00
subparsers = parser.add_subparsers(dest="command")
init = subparsers.add_parser("init")
init.add_argument("--global", dest="globally", action='store_true', help="store config file in ~/.config/")
init.set_defaults(globally=False)
generate = subparsers.add_parser("generate")
sincegroup = generate.add_mutually_exclusive_group()
2019-02-03 19:07:33 +01:00
sincegroup.add_argument('--since', metavar='"YYYY-MM-DD HH:MM:SS"', type=parsed_date,
help='date of previous release')
sincegroup.add_argument('--previous-version', type=str)
2019-02-14 13:47:07 +01:00
group = generate.add_mutually_exclusive_group(required=True)
2019-02-03 18:26:13 +01:00
group.add_argument('--html', action='store_const', const="html", dest="output", help="output as HTML")
group.add_argument('--markdown', action='store_const', const="markdown", dest="output", help="output as markdown")
2019-02-03 19:36:40 +01:00
group.add_argument('--debian-changelog', action='store_const', const="debianchangelog", dest="output",
help="output as debian changelog file")
2019-02-03 18:26:13 +01:00
args = parser.parse_args()
2019-02-14 13:47:07 +01:00
if args.command == "init":
initfile(args.globally)
elif args.command == "generate":
2019-02-14 14:00:59 +01:00
if config.api_token == "none_found":
sys.exit("no config file found\nPlease create one by running\n"
"github-changelog-generator init")
2019-02-14 13:47:07 +01:00
generate_changelog(args.since, args.output, args.previous_version)
else:
raise ValueError("invalid subcommand")