Skip to content

Commit d7defe9

Browse files
committed
Align msync exec more with msync update
Signed-off-by: Joris29 <joris.vanacoleyen@bryxx.eu>
1 parent 53013ac commit d7defe9

2 files changed

Lines changed: 76 additions & 64 deletions

File tree

‎lib/modulesync.rb‎

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,31 @@ def self.manage_module(puppet_module, module_files, defaults)
159159
end
160160
end
161161

162+
def self.execute_in_module(puppet_module, options)
163+
puts "Executing in '#{puppet_module.given_name}'"
164+
puppet_module.repository.prepare_workspace(branch: options[:branch], operate_offline: false, rebase: options[:rebase])
165+
166+
command_args = options[:command_args].dup
167+
env_whitelist = (options[:env] || '').split(',').map(&:strip)
168+
local_script = File.expand_path command_args[0]
169+
command_args[0] = local_script if File.exist?(local_script)
170+
171+
# Remove bundler-related env vars to allow the subprocess to run `bundle` unless explicitly whitelisted.
172+
command_env = ENV.reject { |k, _v| k.match?(/(^BUNDLE|^SOURCE_DATE_EPOCH$|^GEM_|RUBY)/) && !env_whitelist.include?(k) }
173+
174+
success = system(command_env, *command_args, unsetenv_others: true, chdir: puppet_module.working_directory)
175+
unless success
176+
message = "Command execution failed ('#{command_args.join ' '}': #{$CHILD_STATUS})"
177+
raise ModuleSync::Error, message
178+
end
179+
180+
status = puppet_module.repository.git.status
181+
changed_files = [status.changed, status.added, status.deleted, puppet_module.repository.untracked_unignored_files].flat_map(&:keys).uniq
182+
pushed = puppet_module.repository.submit_changes(changed_files, options)
183+
184+
options[:pr] && (pushed || puppet_module.pull_request_branch_ready?) && puppet_module.open_pull_request
185+
end
186+
162187
def self.config_path(file, options)
163188
return file if Pathname.new(file).absolute?
164189

@@ -214,55 +239,22 @@ def self.execute(cli_options)
214239
initialized_modules = managed_modules
215240
initialized_modules.each(&:git_service) if options[:pr]
216241

217-
errors = {}
242+
errors = false
218243
initialized_modules.each do |puppet_module|
219-
$stdout.puts "#{puppet_module.given_name}:"
220-
221-
puppet_module.repository.clone unless puppet_module.repository.cloned?
222-
if @options[:default_branch]
223-
puppet_module.repository.switch branch: false
224-
else
225-
puppet_module.repository.switch branch: @options[:branch]
226-
end
227-
228-
command_args = cli_options[:command_args]
229-
env_whitelist = (@options[:env] || '').split(',')
230-
local_script = File.expand_path command_args[0]
231-
command_args[0] = local_script if File.exist?(local_script)
232-
233-
# Remove bundler-related env vars to allow the subprocess to run `bundle` unless explicitly whitelisted.
234-
command_env = ENV.reject { |k, _v| k.match?(/(^BUNDLE|^SOURCE_DATE_EPOCH$|^GEM_|RUBY)/) && !env_whitelist.include?(k) }
235-
236-
result = system command_env, *command_args, unsetenv_others: true, chdir: puppet_module.working_directory
237-
unless result
238-
message = "Command execution failed ('#{@options[:command_args].join ' '}': #{$CHILD_STATUS})"
239-
raise Thor::Error, message if @options[:fail_fast]
240-
241-
errors[puppet_module.given_name] = message
242-
warn message
243-
end
244-
245-
if result
246-
if @options[:message]
247-
repo = puppet_module.repository
248-
status = repo.git.status
249-
changed_files = [status.changed, status.added, status.deleted, repo.untracked_unignored_files].flat_map(&:keys).uniq
250-
pushed = puppet_module.repository.submit_changes(changed_files, @options)
251-
end
252-
@options[:pr] && (pushed || puppet_module.pull_request_branch_ready?) && puppet_module.open_pull_request
253-
end
254-
255-
$stdout.puts ''
256-
end
244+
execute_in_module(puppet_module, options)
245+
rescue ModuleSync::Error, Git::Error => e
246+
message = e.message || 'Error during `execute`'
247+
warn "#{puppet_module.given_name}: #{message}"
248+
exit 1 unless options[:skip_broken]
249+
errors = true
250+
$stdout.puts "Skipping '#{puppet_module.given_name}' as execute process failed"
251+
rescue StandardError
252+
raise unless options[:skip_broken]
257253

258-
unless errors.empty?
259-
raise Thor::Error, <<~MSG
260-
Error(s) during `execute` command:
261-
#{errors.map { |name, message| " * #{name}: #{message}" }.join "\n"}
262-
MSG
254+
errors = true
255+
$stdout.puts "Skipping '#{puppet_module.given_name}' as execute process failed"
263256
end
264-
265-
exit 1 unless errors.empty?
257+
exit 1 if errors
266258
end
267259

268260
def self.reset(cli_options)

‎lib/modulesync/cli.rb‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,46 +177,66 @@ def update
177177
\x5 `msync exec true` will run "$PWD/true", not `/bin/true` if "$PWD/true" exists.
178178
DESC
179179

180+
option :message,
181+
aliases: '-m',
182+
desc: 'Commit message to apply to changed modules.',
183+
default: CLI.defaults[:message]
180184
option :configs,
181185
aliases: '-c',
182186
desc: 'The local directory or remote repository to define the list of managed modules, ' \
183187
'the file templates, and the default values for template variables.'
184188
option :managed_modules_conf,
185189
desc: 'The file name to define the list of managed modules'
186-
option :branch,
187-
aliases: '-b',
188-
desc: 'Branch name to make the changes in.',
189-
default: CLI.defaults[:branch]
190-
option :default_branch,
191-
aliases: '-B',
190+
option :remote_branch,
191+
aliases: '-r',
192+
desc: 'Remote branch name to push the changes to. Defaults to the branch name.',
193+
default: CLI.defaults[:remote_branch]
194+
option :skip_broken,
192195
type: :boolean,
193-
desc: 'Work on the default branch (take precedence over --branch).',
196+
aliases: '-s',
197+
desc: 'Process remaining modules if an error is found',
194198
default: false
195-
option :fail_fast,
199+
option :amend,
196200
type: :boolean,
197-
desc: 'Abort the run after a command execution failure',
198-
default: CLI.defaults[:fail_fast].nil? || CLI.defaults[:fail_fast]
199-
option :env,
200-
aliases: '-e',
201-
desc: 'Comma-separated list of environment variables to preserve.'
201+
desc: 'Amend previous commit',
202+
default: false
203+
option :sign,
204+
type: :boolean,
205+
desc: 'Cryptographically sign commits and tags',
206+
default: false
207+
option :signoff,
208+
type: :boolean,
209+
desc: 'Add a Signed-off-by trailer to commits',
210+
default: false
211+
option :force,
212+
type: :boolean,
213+
desc: 'Force push amended commit',
214+
default: false
202215
option :pr,
203216
type: :boolean,
204217
desc: 'Submit pull/merge request',
205218
default: false
206219
option :pr_title,
207220
desc: 'Title of pull/merge request',
208-
default: CLI.defaults[:pr_title] || 'Update to module through executed command'
221+
default: CLI.defaults[:pr_title] || 'Update to module template files'
209222
option :pr_labels,
210223
type: :array,
211224
desc: 'Labels to add to the pull/merge request',
212225
default: CLI.defaults[:pr_labels] || []
213226
option :pr_target_branch,
214227
desc: 'Target branch for the pull/merge request',
215228
default: CLI.defaults[:pr_target_branch]
216-
option :message,
217-
aliases: '-m',
218-
desc: 'Commit message to apply to changed modules.',
219-
default: CLI.defaults[:message]
229+
option :rebase,
230+
type: :boolean,
231+
desc: 'Rebase the local branch onto the latest remote default branch before syncing.',
232+
default: false
233+
option :branch,
234+
aliases: '-b',
235+
desc: 'Branch name to make the changes in.',
236+
default: CLI.defaults[:branch]
237+
option :env,
238+
aliases: '-e',
239+
desc: 'Comma-separated list of environment variables to preserve.'
220240
def execute(*command_args)
221241
raise Thor::Error, 'COMMAND is a required argument' if command_args.empty?
222242

0 commit comments

Comments
 (0)