lib/modulesync/cli.rb in modulesync-0.6.1 vs lib/modulesync/cli.rb in modulesync-0.7.2

- old
+ new

@@ -1,124 +1,64 @@ -require 'optparse' +require 'thor' +require 'modulesync' require 'modulesync/constants' require 'modulesync/util' module ModuleSync class CLI - include Constants + class Hook < Thor + class_option :project_root, :aliases => '-c', :desc => 'Path used by git to clone modules into. Defaults to "modules"', :default => 'modules' + class_option :hook_args, :aliases => '-a', :desc => 'Arguments to pass to msync in the git hook' - def defaults - { - :namespace => 'puppetlabs', - :branch => 'master', - :git_base => 'git@github.com:', - :managed_modules_conf => 'managed_modules.yml', - :configs => '.', - :tag_pattern => '%s', - :project_root => './modules', - } - end + desc 'activate', 'Activate the git hook.' + def activate + config = { :command => 'hook' }.merge(options) + config[:hook] = 'activate' + ModuleSync.hook(config) + end - def commands_available - [ - 'update', - 'hook', - ] + desc 'deactivate', 'Deactivate the git hook.' + def deactivate + config = { :command => 'hook' }.merge(options) + config[:hook] = 'deactivate' + ModuleSync.hook(config) + end end - def fail(message) - puts @options[:help] - puts message - exit - end + class Base < Thor + include Constants - def parse_opts(args) - @options = defaults - @options.merge!(Hash.transform_keys_to_symbols(Util.parse_config(MODULESYNC_CONF_FILE))) - @options[:command] = args[0] if commands_available.include?(args[0]) - opt_parser = OptionParser.new do |opts| - opts.banner = "Usage: msync update [-m <commit message>] [-c <directory> ] [--offline] [--noop] [--bump] [--changelog] [--tag] [--tag-pattern <tag_pattern>] [-p <project_root> [-n <namespace>] [-b <branch>] [-r <branch>] [-f <filter>] | hook activate|deactivate [-c <directory> ] [-n <namespace>] [-b <branch>]" - opts.on('-m', '--message <msg>', - 'Commit message to apply to updated modules') do |msg| - @options[:message] = msg - end - opts.on('-n', '--namespace <url>', - 'Remote github namespace (user or organization) to clone from and push to. Defaults to puppetlabs') do |namespace| - @options[:namespace] = namespace - end - opts.on('-c', '--configs <directory>', - 'The local directory or remote repository to define the list of managed modules, the file templates, and the default values for template variables.') do |configs| - @options[:configs] = configs - end - opts.on('-b', '--branch <branch>', - 'Branch name to make the changes in. Defaults to "master"') do |branch| - @options[:branch] = branch - end - opts.on('-p', '--project-root <path>', - 'Path used by git to clone modules into. Defaults to "modules"') do |project_root| - @options[:project_root] = project_root - end - opts.on('-r', '--remote-branch <branch>', - 'Remote branch name to push the changes to. Defaults to the branch name') do |branch| - @options[:remote_branch] = branch - end - opts.on('-f', '--filter <filter>', - 'A regular expression to filter repositories to update.') do |filter| - @options[:filter] = filter - end - opts.on('--amend', - 'Amend previous commit') do |msg| - @options[:amend] = true - end - opts.on('--force', - 'Force push amended commit') do |msg| - @options[:force] = true - end - opts.on('--noop', - 'No-op mode') do |msg| - @options[:noop] = true - end - opts.on('--offline', - 'Do not run git command. Helpful if you have existing repositories locally.') do |msg| - @options[:offline] = true - end - opts.on('--bump', - 'Bump module version to the next minor') do |msg| - @options[:bump] = true - end - opts.on('--changelog', - 'Update CHANGELOG.md if version was bumped') do |msg| - @options[:changelog] = true - end - opts.on('--tag', - 'Git tag with the current module version') do |msg| - @options[:tag] = true - end - opts.on('--tag-pattern', - 'The pattern to use when tagging releases.') do |pattern| - @options[:tag_pattern] = pattern - end - @options[:help] = opts.help - end.parse! + class_option :project_root, :aliases => '-c', :desc => 'Path used by git to clone modules into. Defaults to "modules"', :default => 'modules' + class_option :git_base, :desc => 'Specify the base part of a git URL to pull from', :default => 'git@github.com:' + class_option :namespace, :aliases => '-n', :desc => 'Remote github namespace (user or organization) to clone from and push to. Defaults to puppetlabs', :default => 'puppetlabs' + class_option :filter, :aliases => '-f', :desc => 'A regular expression to select repositories to update.' + class_option :negative_filter, :aliases => '-x', :desc => 'A regular expression to skip repositories.' + class_option :branch, :aliases => '-b', :desc => 'Branch name to make the changes in. Defaults to master.', :default => 'master' - @options.fetch(:message) do - if @options[:command] == 'update' && ! @options[:noop] && ! @options[:amend] && ! @options[:offline] - fail("A commit message is required unless using noop or offline.") - end - end + desc 'update', 'Update the modules in managed_modules.yml' + option :message, :aliases => '-m', :desc => 'Commit message to apply to updated modules. Required unless running in noop mode.' + option :configs, :aliases => '-c', :desc => 'The local directory or remote repository to define the list of managed modules, the file templates, and the default values for template variables.' + option :remote_branch, :aliases => '-r', :desc => 'Remote branch name to push the changes to. Defaults to the branch name.' + option :skip_broken, :type => :boolean, :aliases => '-s', :desc => 'Process remaining modules if an error is found', :default => false + option :amend, :type => :boolean, :desc => 'Amend previous commit', :default => false + option :force, :type => :boolean, :desc => 'Force push amended commit', :default => false + option :noop, :type => :boolean, :desc => 'No-op mode', :default => false + option :offline, :type => :boolean, :desc => 'Do not run any Git commands. Allows the user to manage Git outside of ModuleSync.', :default => false + option :bump, :type => :boolean, :desc => 'Bump module version to the next minor', :default => false + option :changelog, :type => :boolean, :desc => 'Update CHANGELOG.md if version was bumped', :default => false + option :tag, :type => :boolean, :desc => 'Git tag with the current module version', :default => false + option :tag_pattern, :desc => 'The pattern to use when tagging releases.' - @options.fetch(:command) do - fail("A command is required.") + def update + config = { :command => 'update' }.merge(options) + config.merge!(Util.parse_config(MODULESYNC_CONF_FILE)) + config = Util.symbolize_keys(config) + raise Thor::Error, 'No value provided for required option "--message"' unless config[:noop] || config[:message] || config[:offline] + config[:git_opts] = { 'amend' => config[:amend], 'force' => config[:force] } + ModuleSync.update(config) end - if @options[:command] == 'hook' && - (! args.include?('activate') && ! args.include?('deactivate')) - fail("You must activate or deactivate the hook.") - end - - end - - def options - @options + desc 'hook', 'Activate or deactivate a git hook.' + subcommand 'hook', ModuleSync::CLI::Hook end end end