Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the CLI to override options set in the config file #117

Merged
merged 2 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Puppet Labs module engineers manage the zoo of Puppet modules on GitHub, and
has now been restructured and generalized to be used within other
organizations. Puppet modules within an organization tend to have a number of
meta-files that are identical or very similar between modules, such as the
`Gemfile`, `.travis.yml`, `.gitignore`, or `spec\_helper.rb`. If a file needs to
`Gemfile`, `.travis.yml`, `.gitignore`, or `spec_helper.rb`. If a file needs to
change in one module, it likely needs to change in the same way in every other
module that the organization manages.

Expand Down Expand Up @@ -57,12 +57,12 @@ ModuleSync is a gem that uses the GitHub workflow to clone, update, and push mod
repositories. It expects to be activated from a directory containing
configuration for modulesync and the modules, or you can pass it the location
of this configuration directory. [The configuration for the Puppet Labs
modules](https://github.com/puppetlabs/modulesync\_configs), can be used as an
modules](https://github.com/puppetlabs/modulesync_configs), can be used as an
example for your own configuration. The configuration directory contains a
directory called moduleroot which mirrors the structure of a module. The files
in the moduleroot are ERB templates, and MUST be named after the target file,
with `.erb.` appended. The templates are
rendered using values from a file called `config\_defaults.yml` in the root (not
rendered using values from a file called `config_defaults.yml` in the root (not
moduleroot) of the configuration directory. The default values can be
overridden or extended by adding a file called `.sync.yml` to the module itself.
This allows us to, for example, have a set of "required" gems that are added
Expand All @@ -72,7 +72,7 @@ Within the templates, values can be accessed in the `@configs` hash, which is
merged from the values under the keys `:global` and the target file name (no
`.erb` suffix).

The list of modules to manage is in `managed\_modules.yml` in the configuration
The list of modules to manage is in `managed_modules.yml` in the configuration
directory. This lists just the names of the modules to be managed.

ModuleSync can be called from the command line with parameters to change the
Expand Down Expand Up @@ -234,7 +234,8 @@ Available parameters for modulesync.yml
* namespace : Namespace of the projects to manage (Default: 'puppetlabs')
* branch : Branch to push to (Default: 'master')
* remote_branch : Remote branch to push to (Default: Same value as branch)
* pre_commit_script : A script to be run before commiting (ie. contrib/myfooscript.sh)
* message : Commit message to apply to updated modules.
* pre_commit_script : A script to be run before commiting (e.g. 'contrib/myfooscript.sh')

##### Example

Expand Down
17 changes: 17 additions & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ Feature: CLI
Scenario: When running the help subcommand
When I run `msync help`
And the output should match /Commands:/

Scenario: When overriding a setting from the config file on the command line
Given a file named "managed_modules.yml" with:
"""
---
- puppet-test
"""
And a file named "modulesync.yml" with:
"""
---
namespace: maestrodev
git_base: 'git@github.com:'
"""
And a directory named "moduleroot"
When I run `msync update --noop --git-base https://github.com/`
Then the exit status should be 0
And the output should not match /git@github.com:/
104 changes: 81 additions & 23 deletions lib/modulesync/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@

module ModuleSync
class CLI
def self.defaults
@defaults ||= Util.symbolize_keys(Util.parse_config(Constants::MODULESYNC_CONF_FILE))
end

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'
class_option :project_root,
:aliases => '-c',
:desc => 'Path used by git to clone modules into. Defaults to "modules"',
:default => CLI.defaults[:project_root] || 'modules'
class_option :hook_args,
:aliases => '-a',
:desc => 'Arguments to pass to msync in the git hook'

desc 'activate', 'Activate the git hook.'
def activate
Expand All @@ -25,32 +34,81 @@ def deactivate
end

class Base < Thor
include Constants

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'
class_option :project_root,
:aliases => '-c',
:desc => 'Path used by git to clone modules into. Defaults to "modules"',
:default => CLI.defaults[:project_root] || 'modules'
class_option :git_base,
:desc => 'Specify the base part of a git URL to pull from',
:default => CLI.defaults[:git_base] || '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 => CLI.defaults[:namespace] || '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 => CLI.defaults[:branch] || 'master'

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.'
option :message,
:aliases => '-m',
:desc => 'Commit message to apply to updated modules. Required unless running in noop mode.',
:default => CLI.defaults[:message]
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.',
:default => CLI.defaults[:remote_branch]
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.'
option :pre_commit_script,
:desc => 'A script to be run before commiting',
:default => CLI.defaults[:pre_commit_script]

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] }
Expand Down