aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jon Bernard <jbernard@tuxion.com> 2016-01-17 05:55:56 -0500
committerGravatar Jon Bernard <jbernard@tuxion.com> 2016-01-17 06:48:44 -0500
commit8a1581d48702d0a096aa314043dcfd3098f64a93 (patch)
tree35c8963b77d0abd054d06abf1473d924c142a9d0
parentcf51dd817256cff106e170492ab678c7c1ab9d45 (diff)
downloaddotfiles-8a1581d48702d0a096aa314043dcfd3098f64a93.tar.gz
dotfiles-8a1581d48702d0a096aa314043dcfd3098f64a93.tar.bz2
dotfiles-8a1581d48702d0a096aa314043dcfd3098f64a93.zip
Remove some cli duplication
-rw-r--r--dotfiles/cli.py54
-rw-r--r--dotfiles/dotfile.py6
-rw-r--r--dotfiles/repository.py9
3 files changed, 27 insertions, 42 deletions
diff --git a/dotfiles/cli.py b/dotfiles/cli.py
index 1cdac89..b2b68a7 100644
--- a/dotfiles/cli.py
+++ b/dotfiles/cli.py
@@ -13,6 +13,17 @@ DEFAULT_REPO_IGNORE = ['.git']
pass_repo = click.make_pass_decorator(Repository)
+def perform(repo, debug, files, method):
+ for dotfile in repo.dotfiles(files):
+ try:
+ getattr(dotfile, method)(debug)
+ if not debug:
+ click.echo('%sed %s' % (method,
+ dotfile.short_name(repo.homedir)))
+ except DotfileException as err:
+ click.echo(err)
+
+
@click.group(context_settings=dict(help_option_names=['-h', '--help']))
@click.option('-r', '--repository', type=click.Path(), show_default=True,
default=DEFAULT_REPO_PATH)
@@ -34,15 +45,7 @@ def cli(ctx, repository):
@pass_repo
def add(repo, debug, files):
"""Replace file with symlink."""
- for dotfile in repo.dotfiles(files):
- dotfile.add(debug)
-
- # try:
- # repo.dotfile(py.path.local(filename)).add(debug)
- # if not debug:
- # click.echo('added \'%s\'' % filename)
- # except DotfileException as err:
- # click.echo(err)
+ perform(repo, debug, files, 'add')
@cli.command()
@@ -52,18 +55,7 @@ def add(repo, debug, files):
@pass_repo
def remove(repo, debug, files):
"""Replace symlink with file."""
- for filename in files:
- try:
- repo.dotfile(py.path.local(filename)).remove(debug)
- if not debug:
- click.echo('removed \'%s\'' % filename)
- except DotfileException as err:
- click.echo(err)
-
-
-def show_dotfile(homedir, char, dotfile, color):
- display_name = homedir.bestrelpath(dotfile.name)
- click.secho('%c %s' % (char, display_name), fg=color)
+ perform(repo, debug, files, 'remove')
@cli.command()
@@ -72,7 +64,6 @@ def show_dotfile(homedir, char, dotfile, color):
@pass_repo
def status(repo, all, color):
"""Show all dotfiles in a non-OK state."""
-
state_info = {
'error': {'char': 'E', 'color': None},
'missing': {'char': '?', 'color': None},
@@ -89,9 +80,10 @@ def status(repo, all, color):
for dotfile in repo.contents():
try:
+ name = dotfile.short_name(repo.homedir)
char = state_info[dotfile.state]['char']
color = state_info[dotfile.state]['color']
- show_dotfile(repo.homedir, char, dotfile, color)
+ click.secho('%c %s' % (char, name), fg=color)
except KeyError:
continue
@@ -102,13 +94,8 @@ def status(repo, all, color):
@pass_repo
def link(repo, verbose, files):
"""Create missing symlinks."""
- # TODO: no files should be interpreted as all files with confirmation
- for filename in files:
- try:
- repo.dotfile(py.path.local(filename)).link(verbose)
- click.echo('linked \'%s\'' % filename)
- except DotfileException as err:
- click.echo(err)
+ # TODO: no files should be interpreted as all files
+ raise RuntimeError('Not implemented yet')
@cli.command()
@@ -118,9 +105,4 @@ def link(repo, verbose, files):
def unlink(repo, verbose, files):
"""Remove existing symlinks."""
# TODO: no files should be interpreted as all files with confirmation
- for filename in files:
- try:
- repo.dotfile(py.path.local(filename)).unlink(verbose)
- click.echo('unlinked \'%s\'' % filename)
- except DotfileException as err:
- click.echo(err)
+ raise RuntimeError('Not implemented yet')
diff --git a/dotfiles/dotfile.py b/dotfiles/dotfile.py
index bf1e9ac..213323e 100644
--- a/dotfiles/dotfile.py
+++ b/dotfiles/dotfile.py
@@ -63,6 +63,9 @@ class Dotfile(object):
else:
self.name.remove()
+ def short_name(self, homedir):
+ return homedir.bestrelpath(self.name)
+
@property
def state(self):
if self.target.check(exists=0):
@@ -77,12 +80,15 @@ class Dotfile(object):
return 'ok'
def add(self, debug=False):
+ # these invariants are ensured in Repositry
if self.name.check(file=0):
raise DoesNotExist(self.name)
if self.name.check(dir=1):
raise IsDirectory(self.name)
if self.name.check(link=1):
raise IsSymlink(self.name)
+
+ # but not this one, it is valid
if self.target.check(exists=1):
raise TargetExists(self.name)
self._add(debug)
diff --git a/dotfiles/repository.py b/dotfiles/repository.py
index fef77e9..3444388 100644
--- a/dotfiles/repository.py
+++ b/dotfiles/repository.py
@@ -70,17 +70,14 @@ class Repository(object):
def dotfiles(self, paths):
"""Return a list of dotfiles given a path."""
-
+ dotfiles = []
paths = map(py.path.local, paths)
-
- rv = []
for path in paths:
try:
- rv.append(self.dotfile(path))
+ dotfiles.append(self.dotfile(path))
except DotfileException as err:
echo(err)
-
- return rv
+ return dotfiles
def contents(self):
"""Return a list of all dotfiles in the repository path."""