Module: CollinsShell::ThorHelper
- Includes:
- Collins::Util
- Defined in:
- lib/collins_shell/thor.rb
Constant Summary collapse
- COLLINS_OPTIONS =
{ :config => {:type => :string, :desc => 'YAML configuration file'}, :debug => {:type => :boolean, :default => false, :desc => 'Debug output'}, :host => {:type => :string, :desc => 'Collins host (e.g. http://host:port)'}, :password => {:type => :string, :desc => 'Collins password'}, :quiet => {:type => :boolean, :default => false, :desc => 'Be quiet when appropriate'}, :timeout => {:type => :numeric, :default => 30, :desc => 'Collins client timeout'}, :username => {:type => :string, :desc => 'Collins username'} }
- PAGE_OPTIONS =
{ :page => {:type => :numeric, :default => 0, :desc => 'Page of results set.'}, :size => {:type => :numeric, :default => 20, :desc => 'Number of results to return.'}, :sort => {:type => :string, :default => 'DESC', :desc => 'Sort direction. ASC or DESC'} }
Class Attribute Summary collapse
-
.password ⇒ Object
Returns the value of attribute password.
Class Method Summary collapse
Instance Method Summary collapse
- #appropriate_answer?(a) ⇒ Boolean
- #batch_selector_operation(options = {}, &block) ⇒ Object
- #collins_config_from_file(file) ⇒ Object
- #ensure_password(opts = {}) ⇒ Object
- #get_collins_client(opts = {}) ⇒ Object
-
#get_collins_config ⇒ Object
–username –password –host is highest priority –config= second highest ~/.collins.yaml is lowest.
- #get_password(config) ⇒ Object
- #require_valid_collins_config(config) ⇒ Object
- #require_yes(message, color = nil, should_exit = true) ⇒ Object
- #say_error(message, options = {}) ⇒ Object
- #say_success(message) ⇒ Object
- #selector_or_tag ⇒ Object
- #try_config_merge(filename, config) ⇒ Object
- #use_collins_options ⇒ Object
- #use_nuke_option(required = false) ⇒ Object
- #use_page_options(default_size = 20) ⇒ Object
- #use_selector_option(required = false) ⇒ Object
- #use_tag_option(required = false) ⇒ Object
Class Attribute Details
.password ⇒ Object
Returns the value of attribute password.
31 32 33 |
# File 'lib/collins_shell/thor.rb', line 31 def password @password end |
Class Method Details
.included(base) ⇒ Object
26 27 28 |
# File 'lib/collins_shell/thor.rb', line 26 def self.included(base) base.extend(ThorHelper) end |
Instance Method Details
#appropriate_answer?(a) ⇒ Boolean
68 |
# File 'lib/collins_shell/thor.rb', line 68 def appropriate_answer?(a); na = a.to_s.downcase.strip; na == 'yes' || na == 'no'; end |
#batch_selector_operation(options = {}, &block) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/collins_shell/thor.rb', line 89 def batch_selector_operation = {}, &block = [:confirmation_message] = [:success_message] = [:error_message] operation = [:operation] require_non_empty(, "confirmation_message option not set") require_non_empty(, "success_message not set") require_non_empty(, "error_message not set") require_non_empty(operation, "operation not set") selector = get_selector selector_or_tag, [], [:size], [:remote] call_collins get_collins_client, operation do |client| assets = client.find selector if assets.length > 1 then require_yes .call(assets), :red end assets.each do |asset| if block.call(client, asset) then say_success .call(asset) else say_error .call(asset) end end end end |
#collins_config_from_file(file) ⇒ Object
205 206 207 |
# File 'lib/collins_shell/thor.rb', line 205 def collins_config_from_file file symbolize_hash(YAML::load(File.open(File.(file)))) end |
#ensure_password(opts = {}) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/collins_shell/thor.rb', line 142 def ensure_password opts = {} me = CollinsShell::ThorHelper if me.password.nil? then me.password = get_password(get_collins_config.merge(opts).merge(:strict => true)) end me.password end |
#get_collins_client(opts = {}) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/collins_shell/thor.rb', line 134 def get_collins_client opts = {} config = get_collins_config.merge(opts).merge(:strict => true) config = ensure_password opts require_valid_collins_config config config[:logger] = get_logger :trace => .debug, :progname => 'collins-shell' Collins::Client.new config end |
#get_collins_config ⇒ Object
–username –password –host is highest priority –config= second highest ~/.collins.yaml is lowest
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 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/collins_shell/thor.rb', line 165 def get_collins_config def try_config_merge filename, config file_config = collins_config_from_file filename if file_config[:collins] then file_config = file_config[:collins] end config.update(:host => file_config[:host]) unless .host? config.update(:username => file_config[:username]) unless .username? config.update(:password => file_config[:password]) unless .password? if .timeout == 30 and file_config[:timeout] then config.update(:timeout => file_config[:timeout].to_i) end end config = Hash[ :host => .host, :username => .username, :password => .password, :timeout => .timeout ] if ENV['COLLINS'] then try_config_merge ENV['COLLINS'], config end if .config? then try_config_merge .config, config end if File.exists?(File.("~/.collins.yaml")) then user_config = collins_config_from_file "~/.collins.yaml" if user_config[:collins] then user_config = user_config[:collins] end config.update(:host => user_config[:host]) if config[:host].nil? config.update(:username => user_config[:username]) if config[:username].nil? config.update(:password => user_config[:password]) if config[:password].nil? if config[:timeout] == 30 and user_config[:timeout] then config.update(:timeout => user_config[:timeout].to_i) end end config end |
#get_password(config) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/collins_shell/thor.rb', line 150 def get_password config if config[:password] and not config[:password].empty? and config[:password] != "password" then return config end highline = HighLine.new password = highline.ask("Enter your password: ") { |q| q.echo = "x" } config.update(:password => password) end |
#require_valid_collins_config(config) ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/collins_shell/thor.rb', line 209 def require_valid_collins_config config begin require_non_empty(config[:host], "collins.host is required") require_non_empty(config[:username], "collins.username is required") require_non_empty(config[:password], "collins.password is required") rescue Exception => e raise CollinsShell::ConfigurationError.new(e.) end end |
#require_yes(message, color = nil, should_exit = true) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/collins_shell/thor.rb', line 67 def require_yes , color = nil, should_exit = true def appropriate_answer?(a); na = a.to_s.downcase.strip; na == 'yes' || na == 'no'; end highline = HighLine.new = set_color(, color) answer = nil while !appropriate_answer?(answer) do unless answer.nil? then say_status "error", "Please type 'yes' or 'no'.", :red end answer = ask() end if answer.downcase.strip !~ /^yes$/ then if should_exit then exit(0) else false end else true end end |
#say_error(message, options = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/collins_shell/thor.rb', line 114 def say_error , = {} if [:exception] then say_status("error", "#{} - #{[:exception]}", :red) if [:debug] && [:debug] then pp [:exception].backtrace end else say_status("error", , :red) end if [:exit].is_a?(TrueClass) then exit(1) elsif not [:exit].nil? then exit([:exit]) end end |
#say_success(message) ⇒ Object
130 131 132 |
# File 'lib/collins_shell/thor.rb', line 130 def say_success say_status("success", , :green) end |
#selector_or_tag ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/collins_shell/thor.rb', line 57 def selector_or_tag if .selector? then .selector elsif .tag? then {:tag => .tag} else say_error "Either tag or selector must be specified", :exit => true end end |
#try_config_merge(filename, config) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/collins_shell/thor.rb', line 166 def try_config_merge filename, config file_config = collins_config_from_file filename if file_config[:collins] then file_config = file_config[:collins] end config.update(:host => file_config[:host]) unless .host? config.update(:username => file_config[:username]) unless .username? config.update(:password => file_config[:password]) unless .password? if .timeout == 30 and file_config[:timeout] then config.update(:timeout => file_config[:timeout].to_i) end end |
#use_collins_options ⇒ Object
34 35 36 37 38 |
# File 'lib/collins_shell/thor.rb', line 34 def COLLINS_OPTIONS.each do |name, | method_option name, end end |
#use_nuke_option(required = false) ⇒ Object
48 49 50 |
# File 'lib/collins_shell/thor.rb', line 48 def use_nuke_option required = false method_option :nuke, :required => required, :default => :false, :desc => 'Nuke to destroy' end |
#use_page_options(default_size = 20) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/collins_shell/thor.rb', line 39 def default_size = 20 PAGE_OPTIONS.each do |name, | .update(:default => default_size) if name == :size method_option name, end end |
#use_selector_option(required = false) ⇒ Object
51 52 53 54 55 |
# File 'lib/collins_shell/thor.rb', line 51 def use_selector_option required = false method_option :selector, :type => :hash, :required => required, :desc => 'Selector to query collins. Takes the form of --selector=key1:val1 key2:val2 etc' method_option :remote, :type => :boolean, :default => false, :desc => 'Search all collins instances, including remote ones' method_option :size, :type => :numeric, :default => 50, :desc => 'Number of results to find. Defaults to 50' end |
#use_tag_option(required = false) ⇒ Object
45 46 47 |
# File 'lib/collins_shell/thor.rb', line 45 def use_tag_option required = false method_option :tag, :type => :string, :required => required, :desc => 'Tag for asset' end |