From c35508340b0ba6a718660d22065c2e3495dff2db Mon Sep 17 00:00:00 2001 From: nathaniel smith Date: Wed, 30 Aug 2017 00:07:33 -0700 Subject: [PATCH] fix arguments so init works, start on pygit2 stuff --- tildewiki/main.py | 79 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/tildewiki/main.py b/tildewiki/main.py index a5792b0..65ece55 100644 --- a/tildewiki/main.py +++ b/tildewiki/main.py @@ -7,6 +7,7 @@ from os.path import join as path_join import click import pygit2 +from click import ClickException from click.types import Path # TODO support reading from env @@ -16,7 +17,7 @@ PREVIEW_PATH = expanduser('~/public_html/wiki') LOCAL_REPOSITORY_PATH = expanduser('~/wiki') REPOSITORY_PATH = '/wiki' -PATH_TYPE_KWARGS = dict( +DEFAULT_PATH_KWARGS = dict( exists=True, writable=True, readable=True, @@ -63,45 +64,71 @@ pass_config = click.make_pass_decorator(Config, ensure=True) @click.option('--publish-path', default=PUBLISH_PATH, help='System level path to wiki for publishing.', - type=Path(**PATH_TYPE_KWARGS)) -@click.option('--preview-path', - default=PREVIEW_PATH, - help='Local path to wiki for previewing.', - type=Path(**PATH_TYPE_KWARGS)) -@click.option('--local-repo-path', - default=LOCAL_REPOSITORY_PATH, - help='Path to shared wiki git repository.', - type=WikiRepo(**PATH_TYPE_KWARGS)) + type=Path(**DEFAULT_PATH_KWARGS)) @click.option('--repo-path', default=REPOSITORY_PATH, help='Path to your clone of the shared git repository.', - type=WikiRepo(**PATH_TYPE_KWARGS)) + type=WikiRepo(**DEFAULT_PATH_KWARGS)) @pass_config -def main(config, site_name, publish_path, preview_path, - local_repo_path, repo_path): +def main(config, site_name, publish_path, repo_path): # TODO click does not appear to call expanduser on things. it'd be nice to # opt into that with the Path type. Should click be patched? Or should we # use a custom Path type? config.site_name = site_name config.publish_path = publish_path - config.preview_path = preview_path - config.local_repo_path = local_repo_path config.repo_path = repo_path @main.command() +@click.option('--local-repo-path', + default=LOCAL_REPOSITORY_PATH, + help='Path to shared wiki git repository.', + type=Path(file_okay=False)) +@click.option('--preview-path', + default=PREVIEW_PATH, + help='Local path to wiki for previewing.', + type=Path(file_okay=False)) @pass_config -def init(config): +def init(config, local_repo_path, preview_path): + """ + This command, `wiki init`, does the following: + - clones REPOSITORY_PATH to LOCAL_REPOSITORY_PATH + - creates PREVIEW_PATH + - calls the preview command + """ + if path_exists(path_join(local_repo_path)): + raise ClickException( + '{} already exists. Have you already run wiki init?'.format( + local_repo_path)) + + if path_exists(path_join(preview_path)): + raise ClickException( + '{} already exists. Have you already run wiki init?'.format( + preview_path)) + pygit2.clone_repository( + config.repo_path, + local_repo_path + ) + +@main.command() +@click.option('--local-repo-path', + default=LOCAL_REPOSITORY_PATH, + help='Path to shared wiki git repository.', + type=WikiRepo(**DEFAULT_PATH_KWARGS)) +@click.option('--preview-path', + default=PREVIEW_PATH, + help='Local path to wiki for previewing.', + type=Path(**DEFAULT_PATH_KWARGS)) +@pass_config +def preview(config, local_repo_path): raise NotImplementedError() @main.command() +@click.option('--local-repo-path', + default=LOCAL_REPOSITORY_PATH, + help='Path to shared wiki git repository.', + type=WikiRepo(**DEFAULT_PATH_KWARGS)) @pass_config -def preview(config): - click.echo('SUP') - raise NotImplementedError() - -@main.command() -@pass_config -def publish(config): +def publish(config, local_repo_path): raise NotImplementedError() @main.command() @@ -110,6 +137,10 @@ def get(config): raise NotImplementedError() @main.command() +@click.option('--local-repo-path', + default=LOCAL_REPOSITORY_PATH, + help='Path to shared wiki git repository.', + type=WikiRepo(**DEFAULT_PATH_KWARGS)) @pass_config -def reset(config): +def reset(config, local_repo_path): raise NotImplementedError()