Class: Cli
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
- .buildable_distributions ⇒ Object
- .handle_error(e) ⇒ Object
- .parse_scopes(scope_string) ⇒ Object
- .process_scope_option(scopes, exclude_scopes) ⇒ Object
- .shift_arg(args, name) ⇒ Object
- .show_filter_note(scopes, filter) ⇒ Object
- .supports_filtering(command) ⇒ Object
- .system_description_store ⇒ Object
- .validate_number_of_arguments(defined, parsed) ⇒ Object
Class Method Details
.buildable_distributions ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/cli.rb', line 72 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
82 83 84 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 |
# File 'lib/cli.rb', line 82 def self.handle_error(e) Machinery::Ui.kill_pager case e when GLI::MissingRequiredArgumentsException Machinery::Ui.error("Option --" + e.) 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.) Machinery::Ui.error e. 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.) 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.}\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.) Machinery.logger.error(e.backtrace.join("\n")) raise end end true end |
.parse_scopes(scope_string) ⇒ Object
185 186 187 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 |
# File 'lib/cli.rb', line 185 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/cli.rb', line 161 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
154 155 156 157 158 159 |
# File 'lib/cli.rb', line 154 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
147 148 149 150 151 152 |
# File 'lib/cli.rb', line 147 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 |
.supports_filtering(command) ⇒ Object
228 229 230 231 232 |
# File 'lib/cli.rb', line 228 def self.supports_filtering(command) if @config.experimental_features command.flag :exclude, negatable: false, desc: "Exclude elements matching the filter criteria" end end |
.system_description_store ⇒ Object
772 773 774 775 776 777 778 |
# File 'lib/cli.rb', line 772 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
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cli.rb', line 58 def self.validate_number_of_arguments(defined, parsed) if defined.any?(&:multiple?) && parsed.empty? = "No arguments given. Nothing to do." raise GLI::BadCommandLine.new() 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(", ")}" = "Too many arguments: got #{parsed_arguments}, expected #{defined_arguments}" raise GLI::BadCommandLine.new() end true end |