Class: Cf::CLI
- Inherits:
-
Thor
show all
- Includes:
- Config, Thor::Actions
- Defined in:
- lib/cf/cli.rb
Overview
Instance Method Summary
collapse
Methods included from Config
#config_file, #find_home, #get_api_key, #load_config, #save_config, #set_api_key, #set_target_uri
Instance Method Details
#generate(project_name) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/cf/cli.rb', line 165
def generate(project_name)
source = Dir.pwd
parent_dir = "#{source}/#{project_name}"
lines_dir = "#{parent_dir}/lines"
badges_dir = "#{parent_dir}/badges"
if options.force?
FileUtils.rm_rf( parent_dir, :verbose => true)
end
if File.exist?(parent_dir)
say "Skipping #{project_name} because it already exists.", :red
exit(1)
end
say "Generating #{project_name} project hierarchy"
FileUtils.mkdir(parent_dir)
say_status "create", "#{project_name}"
FileUtils.mkdir(badges_dir)
say_status "create", "#{project_name}/badges"
FileUtils.mkdir(lines_dir)
say_status "create", "#{project_name}/lines"
say "#{project_name.humanize} project hierarchy generated successfully.", :green
say "Go to the directory #{lines_dir} and run command 'cf line generate <line_name>' to create line.", :yellow
say "Go to the directory #{badges_dir} and run command 'cf badge generate <badge_name>' to create badge.", :yellow
end
|
#help(*args) ⇒ Object
38
39
40
41
42
|
# File 'lib/cf/cli.rb', line 38
def help(*args)
File.open(File.expand_path(File.dirname(__FILE__)+"/help.txt")).each_line{ |s|
say s
}
end
|
#login ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/cf/cli.rb', line 48
def login
email = ask("Enter your email:")
passwd = ask_password("Enter the password: ")
set_target_uri(false)
resp = CF::Account.login(email, passwd)
if resp['error'].blank? and resp['api_key'].present?
File.open(config_file, 'w') {|f| f.write("#Don't change this file unless you know what you're doing\n" + { :target_url => CF.api_url, :api_version => CF.api_version, :api_key => resp['api_key'], :account_name => resp['account_name'], :email => email.strip }.to_yaml) }
say("\nNow you're logged in.\nTo get started, run cf help\n", :green)
else
say("\n#{resp['error']['message']}\nTry again with valid one.\n", :red)
end
end
|
#output ⇒ Object
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
153
154
155
156
|
# File 'lib/cf/cli.rb', line 115
def output
run_title = options['run_title']
if run_title.present?
run_title = run_title.parameterize
line_source = Dir.pwd
yaml_source = "#{line_source}/line.yml"
set_target_uri(false)
set_api_key(yaml_source)
CF.account_name = CF::Account.info.name
run = CF::Run.find(run_title)
if run['errors'].blank? && run.class != NilClass
say("Fetching output for run: #{run_title}", :green)
if options[:station_index].present?
output = CF::Run.output(:title => run_title, :station => options[:station_index])
else
output = CF::Run.final_output(run_title)
end
if !output.empty?
output.each
csv_str = CSVHash(output,output.first.keys)
csv_str = csv_str.gsub("\"\"", '"')
FileUtils.mkdir("#{line_source}/output") unless Dir.exist?("#{line_source}/output") if RUBY_VERSION > "1.9"
FileUtils.mkdir("#{line_source}/output") unless File.directory?("#{line_source}/output") if RUBY_VERSION < "1.9"
csv_file_name = "#{line_source}/output/#{run_title}-#{Time.now.strftime("%e %b %Y %H:%m-%S%p").parameterize}.csv"
File.open(csv_file_name, 'w') {|f| f.write(csv_str) }
say("Output saved at #{csv_file_name}\n", :yellow)
else
say("Run not completed yet", :red)
end
else
say("Error: #{run['errors'].inspect}", :red)
end
else
say("\nThe run title must be provided to get the output.", :red)
say("\te.g. cf output my-run-title\n", :yellow)
end
end
|
#version ⇒ Object
159
160
161
|
# File 'lib/cf/cli.rb', line 159
def version
say("Version: #{CF::VERSION}")
end
|
#whoami ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/cf/cli.rb', line 72
def whoami
if File.exists?(config_file)
yp = YAML::load(File.open(config_file,'r'))
say("\nAccount => #{yp[:account_name]}", :green)
say("Email => #{yp[:email]}\n", :green)
else
say("\nPlease login with cf login\n")
end
end
|