Class: MDQT::CLI::Base
- Inherits:
-
Object
- Object
- MDQT::CLI::Base
- Defined in:
- lib/mdqt/cli/base.rb
Direct Known Subclasses
Check, Entities, Get, List, Ln, Ls, Rename, Reset, Services, Transform, URL, Version
Class Method Summary collapse
- .check_requirements(options) ⇒ Object
- .introduce(options) ⇒ Object
- .run(args, options) ⇒ Object
- .service_url(options) ⇒ Object
Instance Method Summary collapse
- #advise_on_xml_signing_support ⇒ Object
- #args ⇒ Object
- #btw(comment) ⇒ Object
- #colour_shell? ⇒ Boolean
- #explain(response) ⇒ Object
- #extract_certificate_paths(cert_paths = options.verify_with) ⇒ Object
- #get_stdin ⇒ Object
- #halt!(comment) ⇒ Object
- #hey(comment) ⇒ Object
-
#initialize(cli_args, options) ⇒ Base
constructor
A new instance of Base.
- #options ⇒ Object
- #options=(new_options) ⇒ Object
- #output(response) ⇒ Object
- #pastel ⇒ Object
- #pipeable? ⇒ Boolean
- #run ⇒ Object
- #say(text) ⇒ Object
- #service_url(options) ⇒ Object
- #yay(comment) ⇒ Object
Constructor Details
#initialize(cli_args, options) ⇒ Base
Returns a new instance of Base.
53 54 55 56 57 |
# File 'lib/mdqt/cli/base.rb', line 53 def initialize(cli_args, ) piped_input = get_stdin @args = cli_args.concat(piped_input) @options = end |
Class Method Details
.check_requirements(options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mdqt/cli/base.rb', line 19 def self.check_requirements() unless .service == :not_required abort "No MDQ service URL has been specified. Please use --service, MDQT_SERVICE or MDQ_BASE_URL" unless service_url().to_s.start_with?("http") end if .save_to dir = .save_to begin FileUtils.mkdir_p(dir) unless File.exist?(dir) rescue abort "Error: Directory #{dir} did not exist, and we can't create it" end abort "Error: '#{dir}' is not a writable directory!" if (File.directory?(dir) && !File.writable?(dir)) abort "Error: '#{dir}' is not a directory!" unless File.directory?(dir) end end |
.introduce(options) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mdqt/cli/base.rb', line 38 def self.introduce() if .verbose STDERR.puts "MDQT version #{MDQT::VERSION}" STDERR.puts "Using #{service_url()}" unless .service == :not_required STDERR.puts "Caching is #{MDQT::CLI::CacheControl.caching_on?() ? 'on' : 'off'}" STDERR.print "XML validation is #{MDQT::Client.verification_available? ? 'available' : 'not available'}" STDERR.puts " #{.validate ? "and active" : "but inactive"} for this request" if MDQT::Client.verification_available? STDERR.print "Signature verification is #{MDQT::Client.verification_available? ? 'available' : 'not available'}" STDERR.puts " #{.verify_with ? "and active" : "but inactive"} for this request" if MDQT::Client.verification_available? STDERR.puts "Output directory for saved files is: #{File.absolute_path(.save_to)}" if .save_to STDERR.puts("Warning! TLS certificate verification has been disabled!") if .tls_risky STDERR.puts end end |
.run(args, options) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/mdqt/cli/base.rb', line 11 def self.run(args, ) check_requirements() introduce() self.new(args, ).run end |
.service_url(options) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mdqt/cli/base.rb', line 81 def self.service_url() return nil if .service == :not_required choice = .service.to_s.strip if choice.downcase.start_with? "http" normalize_base_url(choice) else Defaults.lookup_service_alias(choice) end end |
Instance Method Details
#advise_on_xml_signing_support ⇒ Object
125 126 127 |
# File 'lib/mdqt/cli/base.rb', line 125 def advise_on_xml_signing_support hey "XML signature verification and XML validation are not available. Install the 'xmldsig' gem if you can." unless MDQT::Client.verification_available? end |
#args ⇒ Object
69 70 71 |
# File 'lib/mdqt/cli/base.rb', line 69 def args @args end |
#btw(comment) ⇒ Object
157 158 159 |
# File 'lib/mdqt/cli/base.rb', line 157 def btw(comment) STDERR.puts(comment) if .verbose end |
#colour_shell? ⇒ Boolean
141 142 143 |
# File 'lib/mdqt/cli/base.rb', line 141 def colour_shell? TTY::Color.color? end |
#explain(response) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mdqt/cli/base.rb', line 111 def explain(response) unless response.explanation.empty? require 'terminal-table' misc_rows = [['URL', response.explanation[:url]], ["Method", response.explanation[:method]], ['Status', response.explanation[:status]]] req_header_rows = response.explanation[:request_headers].map { |k, v| ['C', k, v] } resp_header_rows = response.explanation[:response_headers].map { |k, v| ['S', k, v] } btw Terminal::Table.new :title => "HTTP Misc", :rows => misc_rows btw Terminal::Table.new :title => "Client Request Headers", :headings => ['C/S', 'Header', 'Value'], :rows => req_header_rows btw Terminal::Table.new :title => "Server Response Headers", :headings => ['C/S', 'Header', 'Value'], :rows => resp_header_rows end end |
#extract_certificate_paths(cert_paths = options.verify_with) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/mdqt/cli/base.rb', line 129 def extract_certificate_paths(cert_paths = .verify_with) cert_paths.collect do |cert_path| begin halt! "Cannot read certificate at '#{cert_path}'!" unless File.readable?(cert_path) halt! "File at '#{cert_path}' does not seem to be a PEM format certificate" unless IO.binread(cert_path).include?("-----BEGIN CERTIFICATE-----") cert_path rescue halt! "Unable to validate the certificate at '#{cert_path}'" end end end |
#get_stdin ⇒ Object
59 60 61 62 |
# File 'lib/mdqt/cli/base.rb', line 59 def get_stdin return $stdin.readlines.map(&:split).flatten.map(&:strip) if pipeable? [] end |
#halt!(comment) ⇒ Object
165 166 167 |
# File 'lib/mdqt/cli/base.rb', line 165 def halt!(comment) abort pastel.red("Error: #{comment}") end |
#hey(comment) ⇒ Object
153 154 155 |
# File 'lib/mdqt/cli/base.rb', line 153 def hey(comment) STDERR.puts(comment) end |
#options ⇒ Object
77 78 79 |
# File 'lib/mdqt/cli/base.rb', line 77 def @options end |
#options=(new_options) ⇒ Object
73 74 75 |
# File 'lib/mdqt/cli/base.rb', line 73 def () @options = end |
#output(response) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mdqt/cli/base.rb', line 99 def output(response) if response.ok? yay response. hey explain(response) if .explain trailer = response.data[-1] == "\n" ? "" : "\n" response.data + trailer else hey response. end end |
#pastel ⇒ Object
145 146 147 |
# File 'lib/mdqt/cli/base.rb', line 145 def pastel @pastel ||= Pastel.new end |
#pipeable? ⇒ Boolean
64 65 66 67 |
# File 'lib/mdqt/cli/base.rb', line 64 def pipeable? return false if ENV["MDQT_STDIN"].to_s.strip.downcase == "off" # Workaround Aruba testing weirdness? !STDIN.tty? && !$stdin.closed? && $stdin.stat.pipe? end |
#run ⇒ Object
169 170 171 |
# File 'lib/mdqt/cli/base.rb', line 169 def run halt! "No action has been defined for this command!" end |
#say(text) ⇒ Object
149 150 151 |
# File 'lib/mdqt/cli/base.rb', line 149 def say(text) STDOUT.puts(text) end |
#service_url(options) ⇒ Object
95 96 97 |
# File 'lib/mdqt/cli/base.rb', line 95 def service_url() self.class.service_url() end |
#yay(comment) ⇒ Object
161 162 163 |
# File 'lib/mdqt/cli/base.rb', line 161 def yay(comment) btw pastel.green(comment) end |