Module: PI::Cli::ChooseHelper

Constant Summary collapse

YES_SET =
Set.new(["y", "Y", "yes", "YES"])
NO_SET =
Set.new(["n", "N", "no", "NO"])
DEFAULTS =
{
  "mem"               => "64M",
  "instances"         => 1,
  "action_for_change" => "notify",
  "graceful"          => true,
  "lazy"              => true,
  "isDebug"           => false,
  "needMonitor"       => false
}

Instance Method Summary collapse

Instance Method Details

#check_envname_valid?(name) ⇒ Boolean

Env name can’t start with ‘vcap_’, ‘VCAP_’, ‘vmc_’ & ‘VMC_’, and can include ‘a-zA-Z`~!@#$%^&*()_+-{}’ only. The max length of name is 18

Returns:

  • (Boolean)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/cli/choose_helper.rb', line 247

def check_envname_valid?(name)
  #(name =~ /\b(?!vcap_|vmc_|VCAP_|VMC_)\D+\b/ &&  name.size <= 18) ? true : false
  if name =~ /[0-9]/
    display "Digital cannot be included in Env name".red
    return false
  end
  if name =~ /^vcap_/i
    display "Env name cannot be started with \"vcap_\" or \"VCAP_\"".red
    return false
  end
  if name =~ /^vmc_/i
    display "Env name cannot be started with \"vmc_\" or \"VMC_\"".red
    return false
  end
  # if not name =~ /^[a-zA-Z\`\~\!\@\#\$\%\^\&\*\(\)\_\+\-\{\}]{0,18}$/
    # display "the input is illegal!".red
    # return false
  # end
  if name.size > 18
    display "Invalid name length. The max length of name is 18"
    return false
  end
  return true
end

#check_envvalue_valid?(value) ⇒ Boolean

not include number, ‘, “, , and size <= 18

Returns:

  • (Boolean)


273
274
275
276
# File 'lib/cli/choose_helper.rb', line 273

def check_envvalue_valid?(value)
  #(value =~ /^[^\'\"\\]+$/ && value.size <= 18) ? true : false
  (value =~ /^[^\'\"\\=]{1,18}$/) ? true : false
end

#check_name_valid?(name) ⇒ Boolean

only [a-zA-Z0-9] for project name and app name (size <=18)

Returns:

  • (Boolean)


242
243
244
# File 'lib/cli/choose_helper.rb', line 242

def check_name_valid?(name)
  (name =~ /^[a-zA-Z0-9]{1,18}$/) ? true : false
end

#check_status(uuid) ⇒ Object

end



227
228
229
230
231
232
233
234
235
# File 'lib/cli/choose_helper.rb', line 227

def check_status(uuid)
  result = client.app_message(uuid)
  if not result[:text].empty?
    display "#{result[:text]}"
  else
    sleep(10)
    check_status(uuid)
  end
end

#check_version_name_valid?(version) ⇒ Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/cli/choose_helper.rb', line 237

def check_version_name_valid?(version)
  version.size > 30 ? false : true
end

#choose_appObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cli/choose_helper.rb', line 97

def choose_app
  apps = select_apps
  err "No application!" if apps.nil? || apps.empty?
  app_choices = Array.new
  apps.each do |a|
    app_choices << a[:name]
  end
  loop{
    appname = ask "Select Application", :choices => app_choices, :indexed => true
    flag = false
    apps.each do |a|
      if a[:name] == appname
        flag = true
        return a 
      end
    end
    if flag == false
      display "Please input the right choice!"
      next
    end
  }
  
end

#choose_app_help(appid_or_appname) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cli/choose_helper.rb', line 121

def choose_app_help(appid_or_appname)
  target = @options[:target] 
  target_not_all = true
  appid = nil
  if appid_or_appname =~ /^[1-9]\d*$/
    appid = appid_or_appname
    app = client.app_get_byid(appid)
    err"The application is not found!" if app.nil?
    return target_not_all, app, appid
  elsif appid_or_appname == nil 
    if target.nil?
      app = choose_app
      return target_not_all, app, appid
    else 
      err "Not enough arguments" 
    end
  else 
    appname = appid_or_appname
    err "Not enough arguments" if target == nil
    if target != "all"
      # targets = client.targets
      # targets = targets.collect { |t| t[:name] }
      # err "Invalid target" unless targets.include?(target)
      app = client.app_search(target, appname)
      err "The application '#{appname}' is not found!" if app.nil?
      return target_not_all, app, appid
    else
      apps = client.app_search_target_is_all(appname)
      err "The application '#{appname}' is not found!" if apps.size == 0
      target_not_all = false 
      return target_not_all, apps, appid        
    end
  end   
end

#choose_app_help_target_not_all(appid_or_appname) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cli/choose_helper.rb', line 156

def choose_app_help_target_not_all(appid_or_appname)
  target = @options[:target]  
  appid = nil     
  if appid_or_appname =~ /^[1-9]\d*$/
    appid = appid_or_appname
    app = client.app_get_byid(appid)
    err"The application is not found!" if app.nil?
    return app, appid
  elsif appid_or_appname == nil 
    if target.nil?
      app = choose_app
      return app, appid
    else 
      err "Not enough arguments" 
    end
  else 
    appname = appid_or_appname
    err "Not enough arguments" if target == nil
    # targets = client.targets
    # targets = targets.collect { |t| t[:name] }
    # err "Invalid target" unless targets.include?(target)
    app = client.app_search(target, appname)
    err "The application '#{appname}' is not found!" if app.nil?
    return app, appid       
  end   
end

#choose_dnsidObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cli/choose_helper.rb', line 198

def choose_dnsid
  dns = client.dns
  err "No DNS" if dns.nil? || dns.empty?
  choices = Array.new
  dns.each do |d|
    choices.push(d[:name])
  end
  dnsname = ask"Select DNS", :choices => choices, :indexed => true
  dnsid = nil
  dns.each do |d|
    dnsid = d[:id] if dnsname == d[:name]
  end
  return dnsid
end

#choose_projectObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cli/choose_helper.rb', line 44

def choose_project
  projects = client.projects
  err "No Projects" if projects.nil? || projects.empty?
  projects.sort! {|a, b| a[:name] <=> b[:name] }
  project_choices = Array.new
  projects.each do |p|
    project_choices << p[:name]
  end
  useproject = ask "Select Project", :choices => project_choices, :indexed => true
  display "Selected Project: ",false
  display "#{useproject}"
  projects.each do |p|
    return p if p[:name] == useproject
  end     
end

#choose_serviceidObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cli/choose_helper.rb', line 183

def choose_serviceid
  services = client.services
  err "No services" if services.nil? || services.empty?
  choices = Array.new
  services.each do |s|
    choices.push(s[:name])
  end
  servicename = ask"Select service", :choices => choices, :indexed => true
  serviceid = nil
  services.each do |s|
    serviceid = s[:id] if s[:name] == servicename
  end
  return serviceid
end

#choose_targetObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cli/choose_helper.rb', line 60

def choose_target
  targets = client.targets
  err "No Targets" if targets.nil? || targets.empty?
  targets.sort! {|a, b| a[:name] <=> b[:name] }
  target_choices = Array.new
  targets.each do |p|
    target_choices << p[:name]
  end
  usetarget = ask "Select Target", :choices => target_choices, :indexed => true
  display "Selected Target: ",false
  display "#{usetarget}"
  targets.each do |p|
    return p if p[:name] == usetarget
  end     
end

#get_graceful(app) ⇒ Object



213
214
215
216
# File 'lib/cli/choose_helper.rb', line 213

def get_graceful(app)
  graceful = @options[:graceful]
  graceful = ask "Need graceful?", :default => DEFAULTS["graceful"] unless graceful
end

#select_appsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cli/choose_helper.rb', line 76

def select_apps
  choices = ["all","project","target"]
  select_type = ask"Select application by", :choices => choices, :indexed => true  
  display "Select application by #{select_type}"    
  case select_type
  when "all"
    app = client.apps
  when "project"
    useproject = choose_project
    app = client.apps(useproject[:id])
  when "target"
    usetarget = choose_target
    projectid = 0
    app = client.apps(projectid, usetarget[:name])
  else
    if select_type
    err "Unknown select type [#{select_type}]"
    end
  end
end

#total_optsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cli/choose_helper.rb', line 17

def total_opts
  all_opts = Hash.new
  all_opts = {
    :user         => @options[:user],
    :password     => @options[:password],
    :name         => @options[:name],
    :email        => @options[:email],
    :path         => @options[:path],
    :ver          => @options[:ver],
    :desc         => @options[:desc],
    :projectid    => @options[:projectid],
    :target       => @options[:target],
    :instance     => @options[:instance],
    :type         => @options[:type],
    :value        => @options[:value],
    :service      => @options[:service],
    :dns          => @options[:dns],
    :zoneid       => @options[:zoneid],
    :instanceid   => @options[:instanceid],
    :file         => @options[:file],
    :continent    => @options[:continent],
    :country      => @options[:country],
    :recordid     => @options[:recordid],
    :graceful     => @options[:graceful]
  }
end