Class: JenkinsPipelineBuilder::CLI::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins_pipeline_builder/cli/helper.rb

Overview

This is the helper class that sets up the credentials from the command line parameters given and initializes the Jenkins Pipeline Builder.

Constant Summary collapse

DEFAULT_FILE_FORMATS =
%w[rb json yml yaml].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.jenkins_api_credsObject

Returns the value of attribute jenkins_api_creds.



38
39
40
# File 'lib/jenkins_pipeline_builder/cli/helper.rb', line 38

def jenkins_api_creds
  @jenkins_api_creds
end

Class Method Details

.find_default_fileObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jenkins_pipeline_builder/cli/helper.rb', line 102

def self.find_default_file
  default_file_name = "#{ENV['HOME']}/.jenkins_api_client/login"

  found_suffix = nil
  DEFAULT_FILE_FORMATS.each do |suffix|
    next unless File.exist?("#{default_file_name}.#{suffix}")
    if !found_suffix
      found_suffix = suffix
    else
      logger.warn "Multiple default files found! Using '#{default_file_name}.#{found_suffix}' but \
'#{default_file_name}.#{suffix}' found."
    end
  end
  "#{ENV['HOME']}/.jenkins_api_client/login.#{found_suffix}" if found_suffix
end

.loggerObject



118
119
120
# File 'lib/jenkins_pipeline_builder/cli/helper.rb', line 118

def self.logger
  JenkinsPipelineBuilder.logger
end

.process_cli_creds(options) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jenkins_pipeline_builder/cli/helper.rb', line 86

def self.process_cli_creds(options)
  self.jenkins_api_creds = {}.with_indifferent_access.merge options
  if jenkins_api_creds[:server] =~ Resolv::AddressRegex
    jenkins_api_creds[:server_ip] = jenkins_api_creds.delete :server
  elsif jenkins_api_creds[:server] =~ URI::DEFAULT_PARSER.make_regexp
    jenkins_api_creds[:server_url] = jenkins_api_creds.delete :server
  else
    msg = "server given (#{jenkins_api_creds[:server]}) is neither a URL nor an IP."
    msg << ' Please pass either a valid IP address or valid URI'
    warn msg
    exit 1
  end
end

.process_creds(options) ⇒ Object



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

def self.process_creds(options)
  default_file = find_default_file
  if options[:debug]
    self.jenkins_api_creds = { username: :foo, password: :bar, server_ip: :baz }
  elsif valid_cli_creds? options
    process_cli_creds(options)
  elsif options[:creds_file]
    process_creds_file options[:creds_file]
  elsif default_file
    process_creds_file default_file
  else
    msg = 'Credentials are not set. Please pass them as parameters or'
    msg << ' set them in the default credentials file'
    warn msg
    exit 1
  end
end

.process_creds_file(file) ⇒ Object



80
81
82
83
84
# File 'lib/jenkins_pipeline_builder/cli/helper.rb', line 80

def self.process_creds_file(file)
  return load File.expand_path(file) if file.end_with? 'rb'
  return self.jenkins_api_creds = JSON.parse(IO.read(File.expand_path(file))) if file.end_with? 'json'
  self.jenkins_api_creds = YAML.load_file(File.expand_path(file))
end

.setup(options) ⇒ JenkinsPipelineBuilder::Generator

Sets up the credentials and initializes the Jenkins Pipeline Builder

Parameters:

  • options (Hash)

    Options obtained from the command line

Returns:



49
50
51
52
53
54
55
56
# File 'lib/jenkins_pipeline_builder/cli/helper.rb', line 49

def self.setup(options)
  process_creds options

  JenkinsPipelineBuilder.credentials = jenkins_api_creds
  generator = JenkinsPipelineBuilder.generator
  JenkinsPipelineBuilder.debug! if options[:debug]
  generator
end

.valid_cli_creds?(options) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/jenkins_pipeline_builder/cli/helper.rb', line 76

def self.valid_cli_creds?(options)
  options[:username] && options[:server] && (options[:password] || options[:password_base64])
end