From 5c4bc0653e601ddddde50e1337ffac2df1d48751 Mon Sep 17 00:00:00 2001 From: Olga Shilyagina Date: Tue, 2 Oct 2018 18:35:49 +0300 Subject: [PATCH] CLI version option Solution for Issue #15 --- README.md | 2 ++ snowsaw/cli.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 348276e..fb7868b 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,8 @@ A terminal help page can be shown by using the `-h`/`--help` option. | `-p`, `--plugin` | `PLUGIN` | No | Load `PLUGIN` as a plugin. | | `--disable-core-plugins` | - | No | Disable all core plugins. | | `--plugin-dir` | `PLUGIN_DIR` | No | Load all plugins in `PLUGIN_DIR`. | +| `--version` | | No | Prints current version of snowsaw | + ## Configuration snowsaw uses [JSON](http://json.org) configuration files to specify tasks on how to set up your dotfiles. diff --git a/snowsaw/cli.py b/snowsaw/cli.py index e52db47..df58ce0 100644 --- a/snowsaw/cli.py +++ b/snowsaw/cli.py @@ -8,6 +8,7 @@ from argparse import ArgumentParser import glob import os +import subprocess from .config import ConfigReader, ReadingError from .dispatcher import Dispatcher, DispatchError @@ -27,11 +28,12 @@ def add_options(parser): parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help='suppress most output') parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='enable verbose output') parser.add_argument('-s', '--snowblocks-directory', nargs=1, dest='snowblocks_directory', - help='base snowblock directory to run all tasks of', metavar='SNOWBLOCKSDIR', required=True) + help='base snowblock directory to run all tasks of', metavar='SNOWBLOCKSDIR') parser.add_argument('-c', '--config-file', nargs=1, dest='config_file', help='run tasks for the specified snowblock', metavar='CONFIGFILE') parser.add_argument('-p', '--plugin', action='append', dest='plugins', default=[], help='load PLUGIN as a plugin', metavar='PLUGIN') parser.add_argument('--disable-core-plugins', dest='disable_core_plugins', action='store_true', help='disable all core plugins') parser.add_argument('--plugin-dir', action='append', dest='plugin_dirs', default=[], metavar='PLUGIN_DIR', help='load all plugins in PLUGIN_DIR') + parser.add_argument('--version', action='store_true', help='print snowsaw version') def read_config(config_file): @@ -58,6 +60,28 @@ def main(): add_options(parser) options = parser.parse_args() + if options.version : + """ + Just print version end exit. Don't need to process other arguments. + Change directory to ensure git-related commands are called inside + snowsaw repository and than change it back + """ + caller_dir = os.getcwd() + snowsaw_dir = os.path.dirname(os.path.realpath(__file__)) + os.chdir(snowsaw_dir) + try: + current_version = subprocess.check_output(["git", "describe"]).strip() + except subprocess.CalledProcessError as Error : + current_version = "Not tagged (development) version" + print(current_version) + os.chdir(caller_dir) + return current_version + elif not options.snowblocks_directory : + """ + Since it's not a version request, check required arguments existance + """ + parser.error("argument -s/--snowblocks-directory is required") + if options.super_quiet: log.set_level(Level.WARNING) if options.quiet: