Skip to content

Commit

Permalink
WIP fail_fast
Browse files Browse the repository at this point in the history
  • Loading branch information
neomilium committed Jan 24, 2022
1 parent 742ae17 commit bb88b48
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
25 changes: 23 additions & 2 deletions features/execute.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@ Feature: execute
Given a basic setup with a puppet module "puppet-test" from "awesome"
Then the file "modules/awesome/puppet-test/metadata.json" should not exist
When I successfully run `msync exec --verbose /bin/true`
Then the output should contain "Cloning from 'file://"
Then the stdout should contain "Cloning from 'file://"
And the file "modules/awesome/puppet-test/metadata.json" should exist

@no-clobber
Scenario: No clones before running command when sourcecode have already been cloned
Then the file "modules/awesome/puppet-test/metadata.json" should exist
When I successfully run `msync exec --verbose /bin/true`
Then the output should not contain "Cloning from 'file://"
Then the stdout should not contain "Cloning from 'file://"

Scenario: When command run fails, abort expect --no-fail-fast

@wip
Scenario: Show fail-fast default value in help
When I successfully run `msync help exec`
Then the stdout should contain:
"""
[--fail-fast], [--no-fail-fast] # Abort the run after a command execution failure
# Default: true
"""

@wip
Scenario: Override fail-fast default value using config file
Given the global option "fail_fast" sets to "false"
When I successfully run `msync help exec`
Then the stdout should contain:
"""
[--fail-fast], [--no-fail-fast] # Abort the run after a command execution failure
"""
# NOTE: It seems there is a Thor bug here: default value is missing in help when sets to 'false'
16 changes: 14 additions & 2 deletions lib/modulesync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def self.clone(cli_options)
def self.execute(cli_options)
@options = config_defaults.merge(cli_options)

errors = {}
managed_modules.each do |puppet_module|
$stdout.puts "#{puppet_module.given_name}:"

Expand All @@ -206,13 +207,24 @@ def self.execute(cli_options)
FileUtils.chdir(puppet_module.working_directory) do
result = system command_env, *command_args, unsetenv_others: true
unless result
raise Thor::Error,
"Error during script execution ('#{@options[:command_args].join ' '}': #{$CHILD_STATUS})"
message = "Command execution failed ('#{@options[:command_args].join ' '}': #{$CHILD_STATUS})"
raise Thor::Error, message if @options[:fail_fast]

errors.merge(
puppet_module.given_name => message
)
$stderr.puts message
end
end

$stdout.puts ''
end

errors.each do |name, message|
puts "#{name}: #{message}"
end

exit 1 unless errors.empty?
end

def self.reset(cli_options)
Expand Down
39 changes: 30 additions & 9 deletions lib/modulesync/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Base < Thor
class_option :project_root,
:aliases => '-c',
:desc => 'Path used by git to clone modules into.',
:default => CLI.defaults[:project_root] || 'modules'
:default => CLI.defaults[:project_root]
class_option :git_base,
:desc => 'Specify the base part of a git URL to pull from',
:default => CLI.defaults[:git_base] || '[email protected]:'
Expand Down Expand Up @@ -148,7 +148,29 @@ def update
ModuleSync.update(config)
end

desc 'execute COMMAND', 'Execute the command in each managed modules'
no_commands do
def merge_options(command, cli_options, **more_options)
options = CLI.defaults
puts "DEFAULTS"
pp options
# options.merge!(options[command.to_sym]) unless options[command.to_sym].nil?
# puts "DEFAULTS + COMMAND-DEFAULT"
# pp options
options.merge! Util.symbolize_keys cli_options
puts "DEFAULTS + COMMAND-DEFAULT + CLI"
pp options
options.merge! more_options
puts "DEFAULTS + COMMAND-DEFAULT + CLI + MORE OPTIONS"
pp options
options.merge! command: command # TODO Remove this
puts "DEFAULTS + COMMAND-DEFAULT + CLI + MORE OPTIONS + COMMAND NAME"
pp options

Util.symbolize_keys options
end
end

desc 'execute [OPTIONS] -- COMMAND..', 'Execute the command in each managed modules'
option :configs,
:aliases => '-c',
:desc => 'The local directory or remote repository to define the list of managed modules,' \
Expand All @@ -159,14 +181,13 @@ def update
:aliases => '-b',
:desc => 'Branch name to make the changes in.',
:default => CLI.defaults[:branch]

option :fail_fast,
:type => :boolean,
:desc => 'Abort the run after a command execution failure',
:default => CLI.defaults[:fail_fast].nil? ? true : CLI.defaults[:fail_fast]
def execute(*command_args)
config = {
:command => 'execute',
:command_args => command_args,
}.merge(options)
config = Util.symbolize_keys(config)
ModuleSync.execute(config)
#raise unless ARGV.include? '--'
ModuleSync.execute merge_options('execute', options, command_args: command_args)
end

desc 'reset', 'Reset all repositories'
Expand Down

0 comments on commit bb88b48

Please sign in to comment.