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

.handle_error(e) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'lib/cli.rb', line 59

def self.handle_error(e)
  case e
  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)
    run(command << "--help")
    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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cli.rb', line 150

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

  scopes.uniq
end

.process_scope_option(scopes, exclude_scopes) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cli.rb', line 126

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



119
120
121
122
123
124
# File 'lib/cli.rb', line 119

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

.system_description_storeObject



677
678
679
680
681
682
683
# File 'lib/cli.rb', line 677

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