Class: MDQT::CLI::Base

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options)
  piped_input = get_stdin
  @args = cli_args.concat(piped_input)
  @options = 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(options)

  unless options.service == :not_required
    abort "No MDQ service URL has been specified. Please use --service, MDQT_SERVICE or MDQ_BASE_URL" unless service_url(options).to_s.start_with?("http")
  end

  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(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(options)
  if options.verbose
    STDERR.puts "MDQT version #{MDQT::VERSION}"
    STDERR.puts "Using #{service_url(options)}" unless options.service == :not_required
    STDERR.puts "Caching is #{MDQT::CLI::CacheControl.caching_on?(options) ? '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

.run(args, options) ⇒ Object



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

def self.run(args, options)

  check_requirements(options)
  introduce(options)

  self.new(args, options).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(options)

  return nil if options.service == :not_required

  choice = options.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_supportObject



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

#argsObject



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 options.verbose
end

#colour_shell?Boolean

Returns:

  • (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 = 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

#get_stdinObject



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

#optionsObject



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

def options
  @options
end

#options=(new_options) ⇒ Object



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

def options=(new_options)
  @options = new_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.message
    hey explain(response) if options.explain
    trailer = response.data[-1] == "\n" ? "" : "\n"
    response.data + trailer
  else
    hey response.message
  end

end

#pastelObject



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

def pastel
  @pastel ||= Pastel.new
end

#pipeable?Boolean

Returns:

  • (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

#runObject



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(options)
  self.class.service_url(options)
end

#yay(comment) ⇒ Object



161
162
163
# File 'lib/mdqt/cli/base.rb', line 161

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