lib/jenkins/builder/app.rb in jenkins-builder-0.2.1 vs lib/jenkins/builder/app.rb in jenkins-builder-0.2.2
- old
+ new
@@ -1,8 +1,7 @@
require 'jenkins/builder/cli'
require 'jenkins/builder/config'
-require 'jenkins/builder/secret'
require 'jenkins_api_client'
require 'pastel'
require 'tty-spinner'
require 'time'
require 'cgi'
@@ -22,61 +21,47 @@
CGI.escape(path.encode(Encoding::UTF_8))
end
end
end
-$is_mac = `uname`.chomp == 'Darwin'
-
module Jenkins
module Builder
class App
- attr_accessor :config, :secret, :client, :options
+ attr_accessor :config, :client, :options
def initialize(options={})
@options = options
- @config = Jenkins::Builder::Config.new
- @secret = ($is_mac ? Jenkins::Builder::Secret.new : @config)
+ @service = @options[:service]
+ @config = Jenkins::Builder::Config.new(@service)
- if @config.url && @config.username && @secret.password
+ if @config.url && @config.username && @config.password
@client = JenkinsApi::Client.new(server_url: @config.url,
username: @config.username,
- password: @secret.password)
+ password: @config.password)
end
end
- def main(args)
- # validate_os!
- validate_fzf!
- Jenkins::Builder::CLI.create_alias_commands(@config.aliases || [])
- Jenkins::Builder::CLI.start(args)
- rescue => e
- STDERR.puts(e.message)
- end
-
def setup(options)
validate_credentials!(options)
config.url = options[:url]
config.username = options[:username]
config.branches = options[:branches]
+ config.password = options[:password]
config.save!
- secret.username = options[:username]
- secret.password = options[:password]
- secret.save!
-
puts 'Credentials setup successfully.'
end
def print_info(options)
puts <<-INFO.gsub(/^\s*/, '')
URL: #{@config.url}
Username: #{@config.username}
INFO
- puts "Password: #{@secret.password}" if options[:password]
+ puts "Password: #{@config.password}" if options[:password]
end
def create_alias(name, command)
@config.aliases ||= {}
@config.aliases[name] = command
@@ -113,24 +98,24 @@
check_and_show_result(job_name, latest_build_no)
end
def fetch_all_jobs
refresh_jobs_cache unless validate_jobs_cache
- @config['jobs-cache']['jobs']
+ @config['services'][@service]['jobs-cache']['jobs']
end
def refresh_jobs_cache
- @config['jobs-cache'] = {
+ @config['services'][@service]['jobs-cache'] = {
'expire' => (Time.now + 86400*30).strftime('%F %T'),
'jobs' => all_jobs
}
@config.save!
end
def validate_jobs_cache
- @config['jobs-cache'] && !@config['jobs-cache'].empty? && \
- Time.parse(@config['jobs-cache']['expire']) > Time.now
+ @config['services'][@service]['jobs-cache'] && !@config['services'][@service]['jobs-cache'].empty? && \
+ Time.parse(@config['services'][@service]['jobs-cache']['expire']) > Time.now
end
def all_jobs
@client.job.list_all
end
@@ -171,11 +156,10 @@
end
all_console_output = ''
loop do
- # require 'pry'; binding.pry;
console_output = @client.job.get_console_output(job_name, build_no, 0, 'text')
all_console_output = console_output['output']
print console_output['output'][printed_size..-1] unless @options[:silent]
printed_size = console_output['output'].size
break unless console_output['more']
@@ -210,19 +194,9 @@
msg =~ /SUCCESS/
end
private
-
- def validate_os!
- raise 'Darwin is the only supported OS now.' unless `uname`.chomp == 'Darwin'
- end
-
- def validate_fzf!
- `fzf --version`
- rescue Errno::ENOENT
- raise 'Required command fzf is not installed.'
- end
def validate_credentials!(options)
@client = JenkinsApi::Client.new(server_url: options[:url],
username: options[:username],
password: options[:password])