require 'commander' require_relative './environments' class ConfigureSemaphore attr_accessor :project_name, :environment def initialize @project_name = File.basename(Dir.getwd) end # rubocop:disable Metrics/MethodLength def call return unless semaphore_cli_installed? FileUtils.mkdir_p(%w[.semaphore .semaphore/bin]) write_or_warn('.semaphore/semaphore.yml', render('templates/semaphore/semaphore.yml.erb')) %w[master develop testing].each do |environment| @environment = environment write_or_warn(".semaphore/#{environment}-deploy.yml", render('templates/semaphore/semaphore-deploy.yml.erb')) end write_or_warn('.semaphore/bin/cache_restore', render('templates/semaphore/bin/cache_restore.erb')) write_or_warn('.semaphore/bin/cache_store', render('templates/semaphore/bin/cache_store.erb')) create_semaphore_notification create_semaphore_secrets end # rubocop:enable Metrics/MethodLength private def semaphore_cli_installed? semaphore_cli_installed = `sem context | grep '*'`.strip == '* renuo_semaphoreci_com' warn('You need to install and configure Semaphore CLI to run this command.') unless semaphore_cli_installed semaphore_cli_installed end def create_semaphore_notification system("sem create notifications #{project_name} "\ "--projects #{project_name} "\ '--branches "master,develop,testing" '\ "--slack-channels \"#project-#{project_name}\" "\ '--slack-endpoint "https://hooks.slack.com/services/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"') end def create_semaphore_secrets system("sem create secret #{project_name}") end def render(template_file) file_path = File.join(File.dirname(__FILE__), template_file) semaphore_template = File.read(file_path) renderer = ERB.new(semaphore_template) renderer.result(binding) end def write_or_warn(file_path, content) if File.exist?(file_path) warn("#{file_path} exists already. I will not overwrite it.") else File.write(file_path, content) say("#{file_path} added.") end end end