Class: Webbynode::Command

Inherits:
Object show all
Defined in:
lib/webbynode/command.rb

Constant Summary collapse

Aliases =
{}
Settings =
{}
InvalidOption =
Class.new(StandardError)
InvalidCommand =
Class.new(StandardError)
CommandError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Command

Returns a new instance of Command.



102
103
104
105
106
# File 'lib/webbynode/command.rb', line 102

def initialize(*args)
  parse_args(args)
rescue InvalidCommand, CommandError
  @parse_error = $!.message
end

Class Method Details

.add_alias(alias_name) ⇒ Object



92
93
94
# File 'lib/webbynode/command.rb', line 92

def add_alias(alias_name)
  Aliases[alias_name] = self.name.split("::").last
end

.class_for(command) ⇒ Object



64
65
66
# File 'lib/webbynode/command.rb', line 64

def class_for(command)
  Webbynode::Commands.const_get command_class_name(command)
end

.commandObject



108
109
110
111
112
113
114
115
# File 'lib/webbynode/command.rb', line 108

def self.command
  str = ""
  self.name.split("::").last.each_char do |ch| 
    str << "_" if ch.match(/[A-Z]/) and !str.empty?
    str << ch.downcase
  end
  str
end

.command_class_name(command) ⇒ Object



96
97
98
99
# File 'lib/webbynode/command.rb', line 96

def command_class_name(command)
  return Aliases[command] if Aliases[command]
  class_name = command.split("_").inject([]) { |arr, item| arr << item.capitalize }.join("")
end

.description(s) ⇒ Object



40
41
42
# File 'lib/webbynode/command.rb', line 40

def description(s)
  Settings[self][:description] = s
end

.for(command) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/webbynode/command.rb', line 68

def for(command)
  begin
    class_for(command)
  rescue NameError
    
    # Assumes Command Not Found
    # Will attempt to find/read possible aliases that might
    # have been set up by the user
    if File.directory?('.webbynode')
      a = Webbynode::Commands::Alias.new 
      a.read_aliases_file
      if a.exists?(command)
        remote_command = a.extract_command(command)
        r = Webbynode::Commands::Remote.new(remote_command)
        r.execute
        return
      end
    end  
    
    # If no aliases:
    puts "Command \"#{command}\" doesn't exist"
  end
end

.helpObject



180
181
182
183
184
185
186
187
188
# File 'lib/webbynode/command.rb', line 180

def self.help
  help = []
  help << summary_help if summary_help
  help << usage
  help << params_help if Settings[self][:parameters].any?
  help << options_help if (Settings[self][:options] || []).any?
  
  help.join("\n")
end

.inherited(child) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/webbynode/command.rb', line 15

def Command.inherited(child)
  Settings[child] ||= { 
    :parameters_hash => {}, 
    :options_hash    => {}, 
    :parameters      => [], 
    :options         => [] 
  }
end

.option(*args) ⇒ Object



58
59
60
61
62
# File 'lib/webbynode/command.rb', line 58

def option(*args)
  option = Option.new(*args)
  Settings[self][:options] << option
  Settings[self][:options_hash][option.name] = option
end

.options_helpObject



169
170
171
172
173
174
175
176
177
178
# File 'lib/webbynode/command.rb', line 169

def self.options_help
  help = []
  if (options = Settings[self][:options] || []).any?
    help << "Options:"
    options.each do |p|
      help << "    #{p.to_s.ljust(20)}#{p.desc}#{p.required? ? "" : ", optional"}"
    end
  end
  help.join("\n")
end

.parameter(*args) ⇒ Object



52
53
54
55
56
# File 'lib/webbynode/command.rb', line 52

def parameter(*args)
  param = Parameter.new(*args)
  Settings[self][:parameters] << param
  Settings[self][:parameters_hash][param.name] = param
end

.params_helpObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/webbynode/command.rb', line 158

def self.params_help
  help = []
  if (params = Settings[self][:parameters])
    help << "Parameters:"
    params.each do |p|
      help << "    #{p.name.to_s.ljust(20)}#{p.desc}#{p.required? ? "" : ", optional"}"
    end
  end
  help.join("\n")
end

.requires_initialization!Object

classes that requires checking for webbynode init must call this method



28
29
30
# File 'lib/webbynode/command.rb', line 28

def requires_initialization!
  Settings[self][:requires_initialization!] = true
end

.requires_options!Object



32
33
34
# File 'lib/webbynode/command.rb', line 32

def requires_options!
  Settings[self][:requires_options!] = true
end

.requires_pushed_application!Object



36
37
38
# File 'lib/webbynode/command.rb', line 36

def requires_pushed_application!
  Settings[self][:requires_pushed_application!] = true
end

.setting(s) ⇒ Object



48
49
50
# File 'lib/webbynode/command.rb', line 48

def setting(s)
  Settings[self][s]
end

.summary(s) ⇒ Object



44
45
46
# File 'lib/webbynode/command.rb', line 44

def summary(s)
  Settings[self][:summary] = s
end

.summary_helpObject



142
143
144
# File 'lib/webbynode/command.rb', line 142

def self.summary_help
  Settings[self][:summary]
end

.usageObject



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/webbynode/command.rb', line 146

def self.usage
  help = "Usage: webbynode #{command}"
  if (params = Settings[self][:parameters])
    params.each do |p|
      help << " #{p.to_s}"
    end
  end
  
  help << " [options]" if (Settings[self][:options] || []).any?
  help
end

Instance Method Details

#apiObject



215
216
217
# File 'lib/webbynode/command.rb', line 215

def api
  @@api ||= Webbynode::ApiClient.new
end

#gemfileObject



190
191
192
# File 'lib/webbynode/command.rb', line 190

def gemfile
  @@gemfile ||= Webbynode::Gemfile.new
end

#gitObject



198
199
200
# File 'lib/webbynode/command.rb', line 198

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

#ioObject



194
195
196
# File 'lib/webbynode/command.rb', line 194

def io
  @@io ||= Webbynode::Io.new
end

#no?(question) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
233
234
235
# File 'lib/webbynode/command.rb', line 230

def no?(question)
  answer = ask(question).downcase
  return true   if %w[n no].include?(answer)
  return false  if %w[y yes].include?(answer)
  exit
end

#notify(msg) ⇒ Object



219
220
221
# File 'lib/webbynode/command.rb', line 219

def notify(msg)
  Webbynode::Notify.message(msg)
end

#option(p) ⇒ Object

Raises:



121
122
123
124
# File 'lib/webbynode/command.rb', line 121

def option(p)
  raise CommandError, "Unknown option: #{p}." unless options[p]
  options[p].value if options[p]
end

#optionsObject



130
131
132
# File 'lib/webbynode/command.rb', line 130

def options
  settings[:options_hash]
end

#param(p) ⇒ Object



117
118
119
# File 'lib/webbynode/command.rb', line 117

def param(p)
  params_hash[p].value if params_hash[p]
end

#param_valuesObject



138
139
140
# File 'lib/webbynode/command.rb', line 138

def param_values
  settings[:parameters].map { |p| p.value }
end

#paramsObject



134
135
136
# File 'lib/webbynode/command.rb', line 134

def params
  settings[:parameters]
end

#params_hashObject



126
127
128
# File 'lib/webbynode/command.rb', line 126

def params_hash
  settings[:parameters_hash]
end

#pushandObject



211
212
213
# File 'lib/webbynode/command.rb', line 211

def pushand
  @pushand ||= PushAnd.new
end

#remote_executorObject



206
207
208
209
# File 'lib/webbynode/command.rb', line 206

def remote_executor
  git.parse_remote_ip
  @remote_executor ||= Webbynode::RemoteExecutor.new(git.remote_ip, git.remote_user, git.remote_port)
end

#runObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/webbynode/command.rb', line 257

def run
  if @parse_error
    puts "#{@parse_error} Use \"webbynode help #{self.class.command}\" for more information."
    puts self.class.usage
    return
  end
  
  if @help
    puts self.class.help
    return
  end
  
  begin
    validate_initialization if settings[:requires_initialization!]
    validate_options        if settings[:requires_options!]
    execute
  rescue Webbynode::ApiClient::Unauthorized
    puts "Your credentials didn't match any Webbynode account."
    puts "For more information: http://wbno.de/credts."
  rescue CommandError
    # io.log $!
    puts $!
  end
end

#serverObject



202
203
204
# File 'lib/webbynode/command.rb', line 202

def server
  @server ||= Webbynode::Server.new(git.parse_remote_ip, git.remote_user, git.remote_port)
end

#settingsObject



253
254
255
# File 'lib/webbynode/command.rb', line 253

def settings
  Settings[self.class] || {}
end

#validate_initializationObject

Raises:



237
238
239
240
241
242
243
244
245
246
# File 'lib/webbynode/command.rb', line 237

def validate_initialization
  raise CommandError,
    "Ahn!? Hello, McFly, anybody home?" unless git.present?
  raise CommandError,
    "Webbynode has not been initialized for this git repository." unless git.remote_webbynode?
  raise CommandError,
    "Could not find .webbynode folder, has Webbynode been initialized for this repository?" unless io.directory?('.webbynode')
  raise CommandError,
    "Could not find .pushand file, has Webbynode been initialized for this repository?" unless pushand.present?
end

#validate_optionsObject



248
249
250
251
# File 'lib/webbynode/command.rb', line 248

def validate_options
  raise Webbynode::Commands::NoOptionsProvided,
    "No remote options were provided." if params.empty?
end

#yes?(question) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
226
227
228
# File 'lib/webbynode/command.rb', line 223

def yes?(question)
  answer = ask(question).downcase
  return true   if %w[y yes].include?(answer)
  return false  if %w[n no].include?(answer)
  exit
end