Module: Proteus::Helpers

Includes:
PathHelpers
Included in:
App, BackendInfo, Common, Generate, GlobalCommands::Validate, Init
Defined in:
lib/proteus/helpers.rb,
lib/proteus/helpers/path_helpers.rb,
lib/proteus/helpers/string_helpers.rb

Defined Under Namespace

Modules: PathHelpers, StringHelpers

Constant Summary collapse

ERROR =
:red
DEFAULT =
:green

Instance Method Summary collapse

Methods included from PathHelpers

#config_dir, #config_path, #context_path, #context_temp_directory, #contexts_path, #environments_path, #module_config_path, #module_data_path, #module_hooks_path, #module_io_file, #module_path, #module_templates_path, #modules_path, #plan_file, #root_path, #state_file, #var_file

Instance Method Details

#alert(message) ⇒ Object



15
16
17
# File 'lib/proteus/helpers.rb', line 15

def alert(message)
  say "#{message}", :on_red
end

#confirm(question:, color:, exit_on_no: true, exit_code: 1) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/proteus/helpers.rb', line 24

def confirm(question:, color:, exit_on_no: true, exit_code: 1)
  if ask(question, color, limited_to: ["yes", "no"]) == "yes"
    yield if block_given?
  else
    if exit_on_no
      say "Exiting.", ERROR
      exit exit_code
    end
  end
end

#current_userObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/proteus/helpers.rb', line 35

def current_user
   user = ENV['CURRENT_USER_NAME'] ? ENV['CURRENT_USER_NAME'] : Etc.getpwnam(Etc.getlogin).gecos
   if user.empty?
     puts "Please set your git username properly:"
     puts "git config --global user.name \"FIRST_NAME LAST_NAME\""
     puts "git config --global user.email \"[email protected]\""
     exit 1
   else
     user
   end
end

#limit(resources) ⇒ Object



19
20
21
22
# File 'lib/proteus/helpers.rb', line 19

def limit(resources)
  resources ? resources.inject("") {
    |memo, resource| "#{memo} -target=#{resource}" } : ""
end

#slack_notification(context:, environment:, message:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/proteus/helpers.rb', line 55

def slack_notification(context:, environment:, message:)
  webhook_url = slack_webhook

  if webhook_url
    time = DateTime.now.strftime("%Y/%m/%d - %H:%M")
    slack_payload = {
      text: "[#{context} - #{environment} - #{time}] #{current_user} #{message}"
    }.to_json

    uri = URI(webhook_url)

    request = Net::HTTP::Post.new(uri)
    request.body = slack_payload

    request_options = {
      use_ssl: uri.scheme == "https",
    }

    Net::HTTP::start(uri.hostname, uri.port, request_options) do |http|
      http.request(request)
    end
  end
end

#slack_webhookObject



47
48
49
50
51
52
53
# File 'lib/proteus/helpers.rb', line 47

def slack_webhook
  hook = config[:slack_webhooks].find do |h|
    environment =~ /#{h[:match]}/
  end

  hook ? hook[:url] : nil
end

#syscall(command, dryrun: false, capture: false, suppress: false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/proteus/helpers.rb', line 79

def syscall(command, dryrun: false, capture: false, suppress: false)
  say "Executing: #{command}\n\n", :green

  output = ""

  if not dryrun
    begin
      PTY.spawn(command) do |stdout, stdin, pid|
        stdout.each do |line|
          output << line
          puts line unless suppress
        end
      end
    rescue Errno::EIO # GNU/Linux raises EIO.
      nil
    end
  end
  return output
end