lib/terragov/terraform.rb in terragov-0.3.3 vs lib/terragov/terraform.rb in terragov-0.4.0

- old
+ new

@@ -9,44 +9,64 @@ abort('Error: cannot find brew') end end end - def execute(command, vars, backend, directory, dryrun = false, verbose = false) + def execute(_args = {}) + # Schema: + # { + # command: which command to run + # test: set to true to output commands to run, used for testing + # verbose: set to true for more output + # directory: main repository directory + # backend: backend file + # vars: data directory + # } + default = { + test: false, + verbose: false, + } + + args = default.merge(_args) + packages = %w[terraform sops] - packages.each do |pkg| - package_check(pkg) + unless args[:test] + packages.each do |pkg| + package_check(pkg) + end end - if command == 'init' + if args[:command] == 'init' puts "Running 'init' is not required as it is applied for each command" exit 1 end current_dir = Dir.pwd - Dir.chdir directory - init(backend, dryrun, verbose) + Dir.chdir args[:directory] unless args[:test] + init(args[:backend], args[:test], args[:verbose]) - if command == 'plan' + if args[:command] == 'plan' command = 'plan -detailed-exitcode' + else + command = args[:command] end - full_command = "bash -c 'terraform #{command} #{vars}'" + full_command = "bash -c 'terraform #{command} #{args[:vars]}'" - run(full_command, dryrun, verbose) + run(full_command, args[:test], args[:verbose]) - Dir.chdir current_dir + Dir.chdir current_dir unless args[:test] end - def init(backend_file, dryrun = false, verbose = false) + def init(backend_file, test = false, verbose = false) init_cmd = "terraform init -backend-config #{backend_file}" - run(init_cmd, dryrun, verbose) + run(init_cmd, test, verbose) end - def run(command, dryrun = false, verbose = false) - if dryrun + def run(command, test = false, verbose = false) + if test puts command else puts command if verbose system(command)