Class: MDQT::CLI::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mdqt/cli/base.rb

Direct Known Subclasses

Check, Get, Reset, Transform, Version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options) ⇒ Base

Returns a new instance of Base.



63
64
65
66
# File 'lib/mdqt/cli/base.rb', line 63

def initialize(args, options)
  @args = args
  @options = options
end

Class Method Details

.absorb_piped_args(args) ⇒ Object



50
51
52
53
# File 'lib/mdqt/cli/base.rb', line 50

def self.absorb_piped_args(args)
  piped = piped? ? parse_stdin : []
  args + piped
end

.check_requirements(args, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mdqt/cli/base.rb', line 20

def self.check_requirements(args, options)
  abort "Error: No MDQ service URL has been specified." unless options.service
  if options.save_to
    dir = options.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(args, options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mdqt/cli/base.rb', line 35

def self.introduce(args, options)
  if options.verbose
    STDERR.puts "MDQT version #{MDQT::VERSION}"
    STDERR.puts "Using #{options.service}" unless options.service == :not_required
    STDERR.puts "Caching is #{options.cache ? 'on' : 'off'}"
    STDERR.print "XML validation is #{MDQT::Client.verification_available? ? 'available' : 'not available'}"
    STDERR.puts  " #{options.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  " #{options.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(options.save_to)}" if options.save_to
    STDERR.puts("Warning! TLS certificate verification has been disabled!") if options.tls_risky
    STDERR.puts
  end
end

.parse_stdinObject



59
60
61
# File 'lib/mdqt/cli/base.rb', line 59

def self.parse_stdin
  STDIN.gsub("\s", "\n").each_line.collect {|l| l.strip}
end

.piped?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/mdqt/cli/base.rb', line 55

def self.piped?
  !STDIN.tty? && !STDIN.closed?
end

.run(args, options) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/mdqt/cli/base.rb', line 10

def self.run(args, options)

  #args = options.stdin ? absorb_piped_args(args) : args

  check_requirements(args, options)
  introduce(args, options)

  self.new(args, options).run
end

Instance Method Details

#advise_on_xml_signing_supportObject



110
111
112
# File 'lib/mdqt/cli/base.rb', line 110

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

#argsObject



72
73
74
# File 'lib/mdqt/cli/base.rb', line 72

def args
  @args
end

#args=(new_args) ⇒ Object



68
69
70
# File 'lib/mdqt/cli/base.rb', line 68

def args=(new_args)
  @args = new_args
end

#btw(comment) ⇒ Object



138
139
140
# File 'lib/mdqt/cli/base.rb', line 138

def btw(comment)
  STDERR.puts(comment) if options.verbose
end

#colour_shell?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/mdqt/cli/base.rb', line 126

def colour_shell?
  TTY::Color.color?
end

#explain(response) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mdqt/cli/base.rb', line 96

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



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mdqt/cli/base.rb', line 114

def extract_certificate_paths(cert_paths = options.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

#halt!(comment) ⇒ Object



146
147
148
# File 'lib/mdqt/cli/base.rb', line 146

def halt!(comment)
  abort pastel.red("Error: #{comment}")
end

#hey(comment) ⇒ Object



134
135
136
# File 'lib/mdqt/cli/base.rb', line 134

def hey(comment)
  STDERR.puts(comment)
end

#optionsObject



80
81
82
# File 'lib/mdqt/cli/base.rb', line 80

def options
  @options
end

#options=(new_options) ⇒ Object



76
77
78
# File 'lib/mdqt/cli/base.rb', line 76

def options=(new_options)
  @options = new_options
end

#output(response) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mdqt/cli/base.rb', line 84

def output(response)
  if response.ok?
    yay response.message
    hey explain(response) if options.explain
    trailer = response.data[-1] == "\n" ? "" : "\n"
    response.data + trailer
  else
    hey response.message
  end

end

#pastelObject



130
131
132
# File 'lib/mdqt/cli/base.rb', line 130

def pastel
  @pastel ||= Pastel.new
end

#runObject



149
150
151
# File 'lib/mdqt/cli/base.rb', line 149

def run
  halt! "No action has been defined for this command!"
end

#yay(comment) ⇒ Object



142
143
144
# File 'lib/mdqt/cli/base.rb', line 142

def yay(comment)
  btw pastel.green(comment)
end