Class: Cli

Inherits:
Object
  • Object
show all
Extended by:
GLI::App
Defined in:
lib/cli.rb

Overview

Copyright © 2013-2015 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Constant Summary collapse

AVAILABLE_SCOPE_LIST =
Machinery::Ui.internal_scope_list_to_string(
  Inspector.all_scopes
)

Class Method Summary collapse

Class Method Details

.buildable_distributionsObject



75
76
77
78
79
80
81
82
83
# File 'lib/cli.rb', line 75

def self.buildable_distributions
  distribution_string = ""
  Os.supported_host_systems.each do |distribution|
    distribution_string += "* #{distribution.canonical_name}\n\n"
    distribution_string += distribution.buildable_systems.map(&:canonical_name).join(", ")
    distribution_string += "\n\n"
  end
  distribution_string
end

.handle_error(e) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cli.rb', line 85

def self.handle_error(e)
  Machinery::Ui.kill_pager

  case e
  when GLI::MissingRequiredArgumentsException
    Machinery::Ui.error("Option --" + e.message)
    exit 1 
  when GLI::UnknownCommandArgument, GLI::UnknownGlobalArgument,
      GLI::UnknownCommand, GLI::BadCommandLine, OptionParser::MissingArgument
    Machinery::Ui.error e.to_s + "\n\n"
    command = ARGV & @commands.keys.map(&:to_s)
    Machinery::Ui.error "Run '#{$0} #{command.first} --help' for more information."
    exit 1
  when Machinery::Errors::MachineryError
    Machinery.logger.error(e.message)
    Machinery::Ui.error e.message
    exit 1
  when SystemExit
    raise
  when SignalException
    Machinery.logger.info "Machinery was aborted with signal #{e.signo}."
    exit 1
  when Errno::ENOSPC
    Machinery::Ui.error("Error: " + e.message)
    exit 1
  else
    Machinery::Ui.error "Machinery experienced an unexpected error. Please file a " \
      "bug report at: https://github.com/SUSE/machinery/issues/new\n"
    if e.is_a?(Cheetah::ExecutionFailed)
      result = ""
      result << "#{e.message}\n"
      result << "\n"

      if e.stderr && !e.stderr.empty?
        result << "Error output:\n"
        result << "#{e.stderr}\n"
      end

      if e.stdout && !e.stdout.empty?
        result << "Standard output:\n"
        result << "#{e.stdout}\n\n"
      end

      if e.backtrace && !e.backtrace.empty?
        result << "Backtrace:\n"
        result << "#{e.backtrace.join("\n")}\n\n"
      end
      Machinery.logger.error(result)
      Machinery::Ui.error result
      exit 1
    else
      Machinery.logger.error("Machinery experienced an unexpected error:")
      Machinery.logger.error(e.message)
      Machinery.logger.error(e.backtrace.join("\n"))
      raise
    end
  end
  true
end

.parse_scopes(scope_string) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cli.rb', line 188

def self.parse_scopes(scope_string)
  unknown_scopes = []
  invalid_scopes = []
  scopes = []

  scope_string.split(",").each do |scope|
    if !(scope =~ /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/)
      invalid_scopes << scope
      next
    end

    # convert cli scope naming to internal one
    scope.tr!("-", "_")

    if Inspector.all_scopes.include?(scope) && Renderer.for(scope)
      scopes << scope
    else
      unknown_scopes << scope
    end
  end

  if invalid_scopes.length > 0
    form = invalid_scopes.length > 1 ? "scopes are" : "scope is"
    raise Machinery::Errors::UnknownScope.new(
      "The following #{form} not valid:" \
        " '#{invalid_scopes.join("', '")}'." \
        " Scope names must start with a letter and contain only lowercase" \
        " letters and digits separated by dashes ('-')."
    )
  end

  if unknown_scopes.length > 0
    form = unknown_scopes.length > 1 ? "scopes are" : "scope is"
    raise Machinery::Errors::UnknownScope.new(
      "The following #{form} not supported: " \
        "#{Machinery::Ui.internal_scope_list_to_string(unknown_scopes)}. " \
      "Valid scopes are: #{AVAILABLE_SCOPE_LIST}."
    )
  end

  Inspector.sort_scopes(scopes.uniq)
end

.process_scope_option(scopes, exclude_scopes) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cli.rb', line 164

def self.process_scope_option(scopes, exclude_scopes)
  if scopes
    if exclude_scopes
      # scope and exclude-scope
      raise Machinery::Errors::InvalidCommandLine.new( "You cannot provide the --scope and --exclude-scope option at the same time.")
    else
      # scope only
      scope_list = parse_scopes(scopes)
    end
  else
    if exclude_scopes
      # exclude-scope only
      scope_list = Inspector.all_scopes - parse_scopes(exclude_scopes)
    else
      # neither scope nor exclude-scope
      scope_list = Inspector.all_scopes
    end
  end
  if scope_list.empty?
    raise Machinery::Errors::InvalidCommandLine.new( "No scopes to process. Nothing to do.")
  end
  scope_list
end

.shift_arg(args, name) ⇒ Object



157
158
159
160
161
162
# File 'lib/cli.rb', line 157

def self.shift_arg(args, name)
  if !res = args.shift
    raise GLI::BadCommandLine.new("You need to provide the required argument #{name}.")
  end
  res
end

.show_filter_note(scopes, filter) ⇒ Object



150
151
152
153
154
155
# File 'lib/cli.rb', line 150

def self.show_filter_note(scopes, filter)
  if scopes.any? { |scope| !filter.element_filters_for_scope(scope).empty? }
    Machinery::Ui.puts "\nNote: There are filters being applied during inspection. " \
      "(Use `--verbose` option to show the filters)\n\n"
  end
end

.system_description_storeObject



761
762
763
764
765
766
767
# File 'lib/cli.rb', line 761

def self.system_description_store
  if ENV.has_key?("MACHINERY_DIR")
    SystemDescriptionStore.new(ENV["MACHINERY_DIR"])
  else
    SystemDescriptionStore.new
  end
end

.validate_number_of_arguments(defined, parsed) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cli.rb', line 61

def self.validate_number_of_arguments(defined, parsed)
  if defined.any?(&:multiple?) && parsed.empty?
    message = "No arguments given. Nothing to do."
    raise GLI::BadCommandLine.new(message)
  elsif !defined.any?(&:multiple?) && parsed.size > defined.size
    parsed_arguments = "#{parsed.size} #{Machinery.pluralize(parsed.size, "argument")}"
    defined_arguments = defined.empty? ? "none" : "only: #{defined.map(&:name).join(", ")}"
    message = "Too many arguments: got #{parsed_arguments}, expected #{defined_arguments}"
    raise GLI::BadCommandLine.new(message)
  end

  true
end