Class: GitPusshuTen::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gitpusshuten/commands/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli, configuration, hooks, environment) ⇒ Base

Initialize a new command



190
191
192
193
194
195
196
# File 'lib/gitpusshuten/commands/base.rb', line 190

def initialize(cli, configuration, hooks, environment)
  @cli           = cli
  @configuration = configuration
  @hooks         = hooks
  @environment   = environment
  @perform_hooks = false
end

Instance Attribute Details

#cliObject

Command-line Interface



7
8
9
# File 'lib/gitpusshuten/commands/base.rb', line 7

def cli
  @cli
end

#commandObject

Returns the underscored version of the command if it's a string otherwise just return the unmodified value (probably nil)



28
29
30
31
# File 'lib/gitpusshuten/commands/base.rb', line 28

def command
  return @command.underscore if @command.is_a?(String)
  @command
end

#configurationObject

Configuration (Environment)



11
12
13
# File 'lib/gitpusshuten/commands/base.rb', line 11

def configuration
  @configuration
end

#environmentObject

Environment connection



19
20
21
# File 'lib/gitpusshuten/commands/base.rb', line 19

def environment
  @environment
end

#hooksObject

Stores the pre and post deploy hooks



15
16
17
# File 'lib/gitpusshuten/commands/base.rb', line 15

def hooks
  @hooks
end

#perform_hooksObject Also known as: perform_hooks?

This is a flag, that, when set to true, will invoke the potentially specified deployment hooks. When "perform_hooks" is set to false, deployment hooks will not be invoked. This is the default behavior. If hooks should be enabled for a specific command, invoke the "perform_hooks!" command from within the "initialize" method of that particular command



39
40
41
# File 'lib/gitpusshuten/commands/base.rb', line 39

def perform_hooks
  @perform_hooks
end

Class Method Details

.description(value = nil) ⇒ Object

This is used by the "help" command to display the description of the command in the CLI



45
46
47
48
49
50
51
# File 'lib/gitpusshuten/commands/base.rb', line 45

def self.description(value = nil)
  if value.nil?
    @description
  else
    @description = value
  end
end

.example(value = nil) ⇒ Object

This is used by the "help" command to display an example of the command in the CLI



78
79
80
81
82
83
84
85
# File 'lib/gitpusshuten/commands/base.rb', line 78

def self.example(value = nil)
  if value.nil?
    @example
  else
    @example ||= ""
    @example << "\n\s\s#{value}"
  end
end

.long_description(value = nil) ⇒ Object

This is used by the "help" command to display the long version of the description



56
57
58
59
60
61
62
# File 'lib/gitpusshuten/commands/base.rb', line 56

def self.long_description(value = nil)
  if value.nil?
    @long_description
  else
    @long_description = value
  end
end

.usage(value = nil) ⇒ Object

This is used by the "help" command to display the usage of the command in the CLI



67
68
69
70
71
72
73
# File 'lib/gitpusshuten/commands/base.rb', line 67

def self.usage(value = nil)
  if value.nil?
    @usage
  else
    @usage = value
  end
end

Instance Method Details

#cObject

Wrapper for the configuration object



120
121
122
# File 'lib/gitpusshuten/commands/base.rb', line 120

def c
  configuration
end

#command_objectObject

Contains an instance of the command object



102
103
104
# File 'lib/gitpusshuten/commands/base.rb', line 102

def command_object
  @command_object ||= GitPusshuTen::Command.new(cli, configuration, hooks, environment)
end

#eObject

Wrapper for the environment object



126
127
128
# File 'lib/gitpusshuten/commands/base.rb', line 126

def e
  environment
end

#error(text) ⇒ Object

Shorthand for creating error messages



178
179
180
# File 'lib/gitpusshuten/commands/base.rb', line 178

def error(text)
  GitPusshuTen::Log.error(text)
end

#g(value) ⇒ Object

Wrapper for coloring ANSI/CLI Green



132
133
134
# File 'lib/gitpusshuten/commands/base.rb', line 132

def g(value)
  value.to_s.color(:green)
end

#gitObject

Git object wrapper



108
109
110
# File 'lib/gitpusshuten/commands/base.rb', line 108

def git
  @git ||= GitPusshuTen::Git.new
end

#helpObject

Displays the help screen for the current command



95
96
97
98
# File 'lib/gitpusshuten/commands/base.rb', line 95

def help
  command_object.display_usage(cli.command)
  exit
end

#localObject

Local object wrapper



114
115
116
# File 'lib/gitpusshuten/commands/base.rb', line 114

def local
  @local ||= GitPusshuTen::Local.new
end

#message(text) ⇒ Object

Shorthand for creating normal messages



166
167
168
# File 'lib/gitpusshuten/commands/base.rb', line 166

def message(text)
  GitPusshuTen::Log.message(text)
end

#perform!Object

Performs one of the commands inside a command class



200
201
202
# File 'lib/gitpusshuten/commands/base.rb', line 200

def perform!
  send("perform_#{command}!")
end

#perform_hooks!Object

Sets the "perform_hooks" flag to "true"



89
90
91
# File 'lib/gitpusshuten/commands/base.rb', line 89

def perform_hooks!
  @perform_hooks = true
end

#post_perform!Object

The Post-perform command It should be invoked after the #perform! command



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/gitpusshuten/commands/base.rb', line 238

def post_perform!
  return unless perform_hooks?
  unless hooks.post_hooks.any?
    GitPusshuTen::Log.message "No post deploy hooks to perform."
    return
  end
  
  ##
  # Connect to the remote environment and perform the post deploy hooks
  hooks.render_commands(hooks.post_hooks).each do |name, commands|
    adjusted_name = name.sub(/^\d+\)\s/, '')
    
    GitPusshuTen::Log.message("Performing post deploy hook: #{y(adjusted_name)}")
    standard environment.execute_as_user("cd '#{e.app_dir}'; #{commands}")
  end
end

#pre_perform!Object

The Pre-perform command It should be invoked before the #perform! command



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/gitpusshuten/commands/base.rb', line 218

def pre_perform!
  return unless perform_hooks?
  unless hooks.pre_hooks.any?
    GitPusshuTen::Log.message "No pre deploy hooks to perform."
    return
  end
  
  ##
  # Connect to the remote environment and perform the pre deploy hooks
  hooks.render_commands(hooks.pre_hooks).each do |name, commands|
    adjusted_name = name.sub(/^\d+\)\s/, '')
    
    GitPusshuTen::Log.message("Performing pre deploy hook: #{y(adjusted_name)}")
    standard environment.execute_as_user("cd '#{e.app_dir}'; #{commands}")
  end
end

#prompt_for_root_password!Object

Makes the user authenticate itself as root



257
258
259
# File 'lib/gitpusshuten/commands/base.rb', line 257

def prompt_for_root_password!
  e.execute_as_root('')
end

#prompt_for_user_password!Object

Makes the user authenticate itself as a user



263
264
265
# File 'lib/gitpusshuten/commands/base.rb', line 263

def prompt_for_user_password!
  e.execute_as_user('')
end

#r(value) ⇒ Object

Wrapper for coloring ANSI/CLI Red



144
145
146
# File 'lib/gitpusshuten/commands/base.rb', line 144

def r(value)
  value.to_s.color(:red)
end

#requires_user_existence!Object

Will check to see if the user exists, if it doesn't then it'll display an error telling the user to set up a user first



270
271
272
273
274
275
# File 'lib/gitpusshuten/commands/base.rb', line 270

def requires_user_existence!
  if not e.user_exists?
    error "You have to setup your user before you can perform this action."
    exit
  end
end

#silent(text) ⇒ Object

Shorthand for silently creating logs



184
185
186
# File 'lib/gitpusshuten/commands/base.rb', line 184

def silent(text)
  GitPusshuTen::Log.silent(text)
end

#standard(text) ⇒ Object

Shorthand for creating standard messages



160
161
162
# File 'lib/gitpusshuten/commands/base.rb', line 160

def standard(text)
  GitPusshuTen::Log.standard(text)
end

#validate!Object

Validates if the method that's about to be invoked actually exists



206
207
208
209
210
211
212
213
# File 'lib/gitpusshuten/commands/base.rb', line 206

def validate!
  if not respond_to?("perform_#{command}!")
    type = self.class.to_s.split("::").last.downcase
    error "Unknown #{y(type)} command: <#{r(command)}>"
    error "Run #{y("gitpusshuten help #{type}")} for a list #{y(type)} commands."
    exit
  end
end

#warning(text) ⇒ Object

Shorthand for creating warning messages



172
173
174
# File 'lib/gitpusshuten/commands/base.rb', line 172

def warning(text)
  GitPusshuTen::Log.warning(text)
end

#y(value) ⇒ Object

Wrapper for coloring ANSI/CLI Yellow



138
139
140
# File 'lib/gitpusshuten/commands/base.rb', line 138

def y(value)
  value.to_s.color(:yellow)
end

#yes?Boolean

Helper method for prompting the user

Returns:



150
151
152
153
154
155
156
# File 'lib/gitpusshuten/commands/base.rb', line 150

def yes?
  choose do |menu|
    menu.prompt = ''
    menu.choice('yes') { true  }
    menu.choice('no')  { false }
  end
end