Class: PI::Cli::Command::Projects

Inherits:
Base show all
Includes:
PI::Cli::ChooseHelper
Defined in:
lib/cli/commands/projects.rb

Instance Method Summary collapse

Methods included from PI::Cli::ChooseHelper

#check_status, #choose_app, #choose_project

Methods inherited from Base

#auth_token, #client, #initialize, #target_url

Constructor Details

This class inherits a constructor from PI::Cli::Command::Base

Instance Method Details

#create_project(projectname = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
# File 'lib/cli/commands/projects.rb', line 45

def create_project(projectname=nil)            
  loop{
    unless projectname
      projectname = ask "Project Name"
      err "Project Name required." if projectname.nil? || projectname.empty?
    end
    if project_exists?(projectname)
      display "Project '#{projectname}' already exists." 
      projectname =nil 
      next
    else
      break
    end
  }  

  runtime = nil
  runtimes = client.runtimes
  choose do |menu|
     display "=============Runtime============"
     menu.prompt = "Select Runtime: "
     menu.select_by = :index_or_name
     runtimes.each do |rt|
       menu.choice(rt) { runtime = rt }
     end
  end
  display "Selected Runtime: ",false
  display "#{runtime}".green 

  framework = nil
  frameworks = client.frameworks(runtime)
  err "No Frameworks" if frameworks.nil? || frameworks.empty?
  choose do |menu|
     display "=============Frameworks============"
     menu.prompt = "Select Framework: "
     menu.select_by = :index_or_name
     frameworks.each do |fk|
       menu.choice(fk) { framework = fk }
     end
  end
  display "Selected Framework: ",false
  display "#{framework}".green
  
  description = ask "Please enter in the description"
   
  github_info = client.github_info
  
  if github_info[:name].empty?      
    manifest = {
    :name => "#{projectname}",
    :runtime     => "#{runtime}",
    :framework   => "#{framework}",
    :description => "#{description}"
  }
  else
    repositoryType = nil  
    repositoryTypes =["cloudpi","github"]
    choose do |menu|
      display "=============Repository Types============"
      menu.prompt = "Select Repository Type: "
      menu.select_by = :index_or_name
      repositoryTypes.each do |rt|
        menu.choice(rt) { repositoryType = rt }
      end
    end
    display "Selected Repository Type: ",false
    display "#{repositoryType}".green

    if repositoryType == "github"
      isNew =nil
      isNew = "yes"
      publishType = nil  
      publishTypes =["public","private"]
      choose do |menu|
        display "=============Publish Types============"
        menu.prompt = "Select publish Type: "
        menu.select_by = :index_or_name
        publishTypes.each do |pt|
          menu.choice(pt) { publishType = pt }
        end
    end
    display "Selected publish Type: ",false
    display "#{publishType}".green
  end
  manifest = {
    :name => "#{projectname}",
    :runtime     => "#{runtime}",
    :framework   => "#{framework}",
    :description => "#{description}",
    :repositoryType => "#{repositoryType}",
    :isNew       => "#{isNew}",
    :publishType => "#{publishType}"
    }
  end
 
  display "Creating project \"#{projectname}\": ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
        
  client.create_project(projectname, manifest)
  Thread.kill(t)
  display "OK".green
end

#delete_project(projectname = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cli/commands/projects.rb', line 154

def delete_project(projectname=nil)
  unless projectname
    useproject = choose_project
    projectname = useproject[:name]
  end
  display "Deleting project \"#{projectname}\": ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.delete_project(projectname)
  Thread.kill(t)
  display 'OK'.green
end

#project_apps(projectname = nil) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/cli/commands/projects.rb', line 297

def project_apps(projectname=nil)
  unless projectname
    useproject = choose_project
    projectname = useproject[:name]
  end      
  apps = client.project_apps(projectname)      
  return display JSON.pretty_generate(apps || []) if @options[:json]
  return display "No applications" if apps.nil? || apps.empty?
  
  apps.sort! {|a, b| a[:name] <=> b[:name] }
  apps_table = table do |t|
    t.headings = 'Target', 'AppName', 'URL', 'Tag', 'Status', 'Instances'
    apps.each do |app|
      t << [app[:targetName], app[:name], app[:url], app[:tag], app[:status], app[:instances]]
    end
  end
  display apps_table
end

#project_commits(projectname = nil) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/cli/commands/projects.rb', line 263

def project_commits(projectname=nil)
  unless projectname
    useproject = choose_project
    projectname = useproject[:name]
  end
  tag =nil
  tags = client.project_tags(projectname)
  return display JSON.pretty_generate(tags || []) if @options[:json]
  display "\n"
  return display "No Commits" if tags.nil? || tags.empty?
  choose do |menu|
    display "=============Tag======="
    menu.prompt = "Select Tag: "
    menu.select_by = :index_or_name
    tags.each do |t|
      menu.choice(t) { tag = t}
    end
  end
  display "Selected Tag: ",false
  display "#{tag}".green

  commits = client.project_commits(projectname,tag)
  commits.sort! {|a, b| a[:date] <=> b[:date] }
  return display JSON.pretty_generate(commits || []) if @options[:json]
  return display "No Commits" if commits.nil? || commits.empty?
  commits_table = table do |t|
    t.headings = 'CommitId', 'Date', 'Comment'  
    commits.each do |commit|
      t << [commit[:commit], commit[:date], commit[:comment]]
    end
  end
  display commits_table
end

#project_events(projectname = nil) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cli/commands/projects.rb', line 230

def project_events(projectname=nil)
  unless projectname
    useproject = choose_project
    projectname = useproject[:name]
  end
  events = client.project_events(projectname)
  return display JSON.pretty_generate(events || []) if @options[:json]
  return display "No Events." if events.empty?
  events_table = table do |t|
    t.headings = 'Target', 'AppName','EventTime', 'EventInfo','EventDetail'
    events.each do |event|
      t << [event[:targetName], event[:applicationName], event[:createdAt], event[:eventType], event[:eventDetail]]
    end
  end
  display events_table
end

#project_tags(projectname = nil) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/cli/commands/projects.rb', line 247

def project_tags(projectname=nil)
  unless projectname
    useproject = choose_project
    projectname = useproject[:name]
  end
  tags ||= client.project_tags(projectname)
  return display JSON.pretty_generate(tags || []) if @options[:json]
  return display "No Tags." if tags.size == 0
  tags_table = table do |t|
    t.headings = ['Tag']
    tags.each { |f| t << [f]}
  end
  display "\n"
  display tags_table
end

#projectsObject



12
13
14
15
16
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
43
# File 'lib/cli/commands/projects.rb', line 12

def projects
  projects = client.projects      
  # size = projects.size-1
  # if size >= 100
    # new_range_projects = Array.new
    # puts "Total projects: #{size}"
    # start = ask("start number:", Integer) { |q| q.in = 0..size }
    # stop = ask("stop number:", Integer) { |q| q.in = 0..size }
    # start.upto(stop) do |i|
      # new_range_projects << projects[i]
      # i += 1
    # end
    # new_range_projects_table = table do |t|
      # t.headings = 'Name', 'Runtime', 'Framework', 'Git Type','GitUrl', 'Description'
      # new_range_projects.each do |project|
        # t << [project[:name], project[:runtime] ,project[:framework], project[:repositoryType], project[:gitUrl],project[:description]]
      # end
    # end
    # display new_range_projects_table
  # else  
  return display JSON.pretty_generate(projects) if @options[:json]
  return display "No Projects" if projects.nil? || projects.empty?       
  projects.sort! {|a, b| a[:name] <=> b[:name] }
  display "\n"        
  projects_table = table do |t|
    t.headings = 'Name', 'Runtime', 'Framework', 'Git Type','GitUrl', 'ID', 'Description'
    projects.each do |project|
      t << [project[:name], project[:runtime] ,project[:framework], project[:repositoryType], project[:gitUrl],project[:id],project[:description]]
    end
  end
  display projects_table
end

#upload(projectname = nil) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/cli/commands/projects.rb', line 174

def upload(projectname=nil)
  file, java_projects = nil, nil  
  unless projectname    
  projects = client.projects
  err "No Projects" if projects.nil? || projects.empty?
  java_projects = projects.delete_if{ |p| p[:runtime] =~ /^(ruby)/i}    
  java_projects.sort! {|a, b| a[:name] <=> b[:name] }
  choose do |menu|
     display "=============Project Name======="
     menu.prompt = "Select Project: "
     menu.select_by = :index_or_name
     projects.each do |project|
       menu.choice(project[:name]) { projectname = project[:name] }
     end
  end
  display "Selected Project: ",false
  display "#{projectname}".green
  end
     
  version = ask "Please enter the version"
  description = ask "Please enter in the description"       
  
  path = nil
  loop{
    path = ask "Please enter the path of upload war file"
    if not ((path =~ /\.(war)$/) && (File.exists? path))
      display "Not war file or path does not exist!" 
      path =nil
      next
    else
      break
    end
  }  

  upload_file = File.new(path, 'rb')
  manifest = {
    :projectName => "#{projectname}",
    :fileWAR     => upload_file,
    :versionName => "#{version.downcase}",
    :description => "#{description}"
  }     
  display "Uploading \"#{File.basename(path)}\": ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.upload(manifest)
  Thread.kill(t)
  display 'OK'.green
end