Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix #2214 -- handle nikola init in existing dir nicely
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jan 10, 2016
1 parent b7b94ce commit 4e9d8b0
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions nikola/plugins/command/init.py
Expand Up @@ -220,6 +220,14 @@ def prepare_config(config):
p.update({k: str(v) for k, v in config.items() if isinstance(v, bool) or v is None})
return p

def test_destination(destination, demo=False):
if demo and os.path.exists(destination):
LOGGER.warning("The directory {0} already exists, and a new demo site cannot be initialized in an existing directory.".format(destination))
LOGGER.warning("Please remove the directory and try again, or use another directory.")
LOGGER.info("Hint: If you want to initialize a git repository in this directory, run `git init` in the directory after creating a Nikola site.")
return False
else:
return True

class CommandInit(Command):
"""Create a new site."""
Expand Down Expand Up @@ -277,7 +285,7 @@ def create_empty_site(cls, target):
makedirs(os.path.join(target, folder))

@staticmethod
def ask_questions(target):
def ask_questions(target, demo=False):
"""Ask some questions about Nikola."""
def urlhandler(default, toconf):
answer = ask('Site URL', 'https://example.com/')
Expand Down Expand Up @@ -442,7 +450,7 @@ def chandler(default, toconf):
print("If you do not want to answer and want to go with the defaults instead, simply restart with the `-q` parameter.")

for query, default, toconf, destination in questions:
if target and destination == '!target':
if target and destination == '!target' and test_destination(target, demo):
# Skip the destination question if we know it already
pass
else:
Expand All @@ -459,8 +467,9 @@ def chandler(default, toconf):
if toconf:
SAMPLE_CONF[destination] = answer
if destination == '!target':
while not answer:
print(' ERROR: you need to specify a target directory.\n')
while not answer or not test_destination(answer, demo):
if not answer:
print(' ERROR: you need to specify a target directory.\n')
answer = ask(query, default)
STORAGE['target'] = answer

Expand All @@ -476,7 +485,7 @@ def _execute(self, options={}, args=None):
except IndexError:
target = None
if not options.get('quiet'):
st = self.ask_questions(target=target)
st = self.ask_questions(target=target, demo=options.get('demo'))
try:
if not target:
target = st['target']
Expand All @@ -489,11 +498,13 @@ def _execute(self, options={}, args=None):
Options:
-q, --quiet Do not ask questions about config.
-d, --demo Create a site filled with example data.""")
return False
return 1
if not options.get('demo'):
self.create_empty_site(target)
LOGGER.info('Created empty site at {0}.'.format(target))
else:
if not test_destination(target, True):
return 2
self.copy_sample_site(target)
LOGGER.info("A new site with example data has been created at "
"{0}.".format(target))
Expand Down

0 comments on commit 4e9d8b0

Please sign in to comment.