Class: Morpheus::Terminal
- Inherits:
-
Object
- Object
- Morpheus::Terminal
- Defined in:
- lib/morpheus/terminal.rb
Overview
Terminal is a class for executing morpheus commands The default IO is STDIN, STDOUT, STDERR The default home directory is $HOME/.morpheus
Example Usage
morph = Morpheus::Terminal.new
exit_code, err = morph.execute("instances list -m 10")
assert exit_code == 0
assert err == nil
morph = Morpheus::Terminal.new(STDIN, File.new("/tmp/morph.log", "w+"))
morph.execute("hosts get 23")
morph = Morpheus::Terminal.new(STDIN, File.new("/tmp/host23.json", "w"))
morph.execute("hosts get 23 --json")
puts File.read("/tmp/host23.json")
Defined Under Namespace
Classes: Blackhole
Instance Attribute Summary collapse
-
#home_directory ⇒ Object
readonly
Returns the value of attribute home_directory.
-
#prompt ⇒ Object
, :angry_prompt.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
- .angry_prompt ⇒ Object
- .custom_prompt ⇒ Object
-
.default_color ⇒ Object
DEFAULT_TERMINAL_WIDTH = 80.
-
.instance ⇒ Object
the global Morpheus::Terminal instance This should go away, but it needed for now…
-
.new(*args) ⇒ Object
hack alert! This should go away, but it needed for now…
- .prompt ⇒ Object
- .prompt=(v) ⇒ Object
Instance Method Summary collapse
- #angry_prompt ⇒ Object
- #coloring? ⇒ Boolean
-
#execute(input) ⇒ Object
protected.
-
#execute_profile_script(rerun = false) ⇒ Object
execute .morpheus_profile startup script.
-
#initialize(stdin = STDIN, stdout = STDOUT, stderr = STDERR, homedir = nil) ⇒ Terminal
constructor
Create a new instance of Morpheus::Terminal.
-
#set_coloring(enabled) ⇒ Object
def coloring=(v) set_coloring(enabled) end.
- #set_home_directory(homedir) ⇒ Object
- #set_stderr(io) ⇒ Object
- #set_stdin(io) ⇒ Object
- #set_stdout(io) ⇒ Object
-
#usage ⇒ Object
alias :‘coloring=’ :set_coloring.
Constructor Details
#initialize(stdin = STDIN, stdout = STDOUT, stderr = STDERR, homedir = nil) ⇒ Terminal
Create a new instance of Morpheus::Terminal
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 |
# File 'lib/morpheus/terminal.rb', line 102 def initialize(stdin=STDIN,stdout=STDOUT, stderr=STDERR, homedir=nil) # todo: establish config context for executing commands, # so you can run them in parallel within the same process # that means not using globals and class instance vars # just return an exit code / err, instead of raising SystemExit # establish IO # @stdin, @stdout, @stderr = stdin, stdout, stderr set_stdin(stdin) set_stdout(stdout) set_stderr(stderr) # establish home directory use_homedir = homedir || ENV['MORPHEUS_CLI_HOME'] || File.join(Dir.home, ".morpheus") set_home_directory(use_homedir) # use colors by default set_coloring(STDOUT.isatty) # Term::ANSIColor::coloring = STDOUT.isatty # @coloring = Term::ANSIColor::coloring? # startup script if File.exists? Morpheus::Cli::DotFile.morpheus_profile_filename @profile_dot_file = Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename) else @profile_dot_file = nil end # the string to prompt for input with @prompt ||= Morpheus::Terminal.prompt @angry_prompt ||= Morpheus::Terminal.angry_prompt end |
Instance Attribute Details
#home_directory ⇒ Object (readonly)
Returns the value of attribute home_directory.
93 94 95 |
# File 'lib/morpheus/terminal.rb', line 93 def home_directory @home_directory end |
#prompt ⇒ Object
, :angry_prompt
92 93 94 |
# File 'lib/morpheus/terminal.rb', line 92 def prompt @prompt end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
93 94 95 |
# File 'lib/morpheus/terminal.rb', line 93 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
93 94 95 |
# File 'lib/morpheus/terminal.rb', line 93 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
93 94 95 |
# File 'lib/morpheus/terminal.rb', line 93 def stdout @stdout end |
Class Method Details
.angry_prompt ⇒ Object
71 72 73 |
# File 'lib/morpheus/terminal.rb', line 71 def self.angry_prompt "#{Term::ANSIColor.red}morpheus:#{Term::ANSIColor.reset} " end |
.custom_prompt ⇒ Object
75 76 77 |
# File 'lib/morpheus/terminal.rb', line 75 def self.custom_prompt #export MORPHEUS_PS1='\[\e[1;32m\]\u@\h:\w${text}$\[\e[m\] ' end |
.default_color ⇒ Object
DEFAULT_TERMINAL_WIDTH = 80
51 52 53 |
# File 'lib/morpheus/terminal.rb', line 51 def self.default_color Term::ANSIColor.cyan end |
.instance ⇒ Object
the global Morpheus::Terminal instance This should go away, but it needed for now…
81 82 83 |
# File 'lib/morpheus/terminal.rb', line 81 def self.instance @morphterm end |
.new(*args) ⇒ Object
hack alert! This should go away, but it needed for now…
86 87 88 89 90 |
# File 'lib/morpheus/terminal.rb', line 86 def self.new(*args) obj = super(*args) @morphterm = obj obj end |
.prompt ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/morpheus/terminal.rb', line 55 def self.prompt if @prompt.nil? if ENV['MORPHEUS_PS1'] @prompt = ENV['MORPHEUS_PS1'].dup else #ENV['MORPHEUS_PS1'] = "#{Term::ANSIColor.cyan}morpheus>#{Term::ANSIColor.reset} " @prompt = "#{Term::ANSIColor.cyan}morpheus>#{Term::ANSIColor.reset} " end end @prompt end |
.prompt=(v) ⇒ Object
67 68 69 |
# File 'lib/morpheus/terminal.rb', line 67 def self.prompt=(v) @prompt = v end |
Instance Method Details
#angry_prompt ⇒ Object
254 255 256 |
# File 'lib/morpheus/terminal.rb', line 254 def angry_prompt @angry_prompt ||= Morpheus::Terminal.angry_prompt end |
#coloring? ⇒ Boolean
201 202 203 |
# File 'lib/morpheus/terminal.rb', line 201 def coloring? @coloring == true end |
#execute(input) ⇒ Object
protected
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/morpheus/terminal.rb', line 280 def execute(input) exit_code = 0 err = nil args = nil if input.is_a? String args = Shellwords.shellsplit(input) elsif input.is_a?(Array) args = input.dup else raise "terminal execute() expects a String to be split or an Array of String arguments and instead got (#{args.class}) #{args}" end # include Term::ANSIColor # tempting # short circuit version switch if args.length == 1 if args[0] == '-v' || args[0] == '--version' @stdout.puts Morpheus::Cli::VERSION return 0, nil end end # looking for global help? if args.length == 1 if args[0] == '-h' || args[0] == '--help' || args[0] == 'help' @stdout.puts usage return 0, nil end end # process global options # raise log level right away if args.find {|it| it == '-V' || it == '--debug'} @terminal_log_level = Morpheus::Logging::Logger::DEBUG Morpheus::Logging.set_log_level(Morpheus::Logging::Logger::DEBUG) ::RestClient.log = Morpheus::Logging.debug? ? Morpheus::Logging::DarkPrinter.instance : nil end # execute startup script .morpheus_profile unless --noprofile is passed # todo: this should happen in initialize.. noprofile = false if args.find {|it| it == '--noprofile' } noprofile = true args.delete_if {|it| it == '--noprofile' } end if @profile_dot_file && !@profile_dot_file_has_run if !noprofile && File.exists?(@profile_dot_file.filename) execute_profile_script() end end # not enough arguments? if args.count == 0 @stderr.puts "#{@angry_prompt}[command] argument is required." #@stderr.puts "No command given, here's some help:" @stderr.print usage return 2, nil # CommandError.new("morpheus requires a command") end cmd_name = args[0] cmd_args = args[1..-1] # unknown command? # all commands should be registered commands or aliases if !(Morpheus::Cli::CliRegistry.has_command?(cmd_name) || Morpheus::Cli::CliRegistry.has_alias?(cmd_name)) @stderr.puts "#{@angry_prompt}'#{cmd_name}' is not a morpheus command. See 'morpheus --help'." #@stderr.puts usage return 127, nil end # ok, execute the command (or alias) result = nil begin # shell is a Singleton command class if args[0] == "shell" result = Morpheus::Cli::Shell.instance.handle(args[1..-1]) else result = Morpheus::Cli::CliRegistry.exec(args[0], args[1..-1]) end # todo: clean up CliCommand return values, handle a few diff types for now if result == nil || result == true || result == 0 exit_code = 0 elsif result == false exit_code = 1 elsif result.is_a?(Array) # exit_code, err exit_code = result[0] #.to_i err = result[1] else exit_code = result #.to_i end rescue => e exit_code = Morpheus::Cli::ErrorHandler.new(@stderr).handle_error(e) err = e end return exit_code, err end |
#execute_profile_script(rerun = false) ⇒ Object
execute .morpheus_profile startup script
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/morpheus/terminal.rb', line 137 def execute_profile_script(rerun=false) if @profile_dot_file if rerun || !@profile_dot_file_has_run @profile_dot_file_has_run = true return @profile_dot_file.execute() # todo: pass io in here else return false # already run end else return nil end end |
#set_coloring(enabled) ⇒ Object
def coloring=(v)
set_coloring(enabled)
end
195 196 197 198 199 |
# File 'lib/morpheus/terminal.rb', line 195 def set_coloring(enabled) @coloring = !!enabled Term::ANSIColor::coloring = @coloring coloring? end |
#set_home_directory(homedir) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/morpheus/terminal.rb', line 177 def set_home_directory(homedir) full_homedir = File.(homedir) # if !Dir.exists?(full_homedir) # print_red_alert "Directory not found: #{full_homedir}" # exit 1 # end @home_directory = full_homedir # todo: deprecate this Morpheus::Cli.home_directory = full_homedir @home_directory end |
#set_stderr(io) ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/morpheus/terminal.rb', line 168 def set_stderr(io) if io.nil? || io == 'blackhole' || io == '/dev/null' @stderr = Morpheus::Terminal::Blackhole.new else @stderr = io end @stderr end |
#set_stdin(io) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/morpheus/terminal.rb', line 150 def set_stdin(io) # if io.nil? || io == 'blackhole' || io == '/dev/null' # @stdout = Morpheus::Terminal::Blackhole.new # else # @stdout = io # end @stdin = io end |
#set_stdout(io) ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/morpheus/terminal.rb', line 159 def set_stdout(io) if io.nil? || io == 'blackhole' || io == '/dev/null' @stdout = Morpheus::Terminal::Blackhole.new else @stdout = io end @stdout end |
#usage ⇒ Object
alias :‘coloring=’ :set_coloring
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/morpheus/terminal.rb', line 207 def usage out = "Usage: morpheus [command] [options]\n" # just for printing help. todo: start using this. maybe in class Cli::MainCommand # maybe OptionParser's recover() instance method will do the trick optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = "Options:" # hack alert opts.on('-v','--version', "Print the version.") do @stdout.puts Morpheus::Cli::VERSION # exit end opts.on('--noprofile','--noprofile', "Do not read and execute the personal initialization script .morpheus_profile") do @noprofile = true end opts.on('-C','--nocolor', "Disable ANSI coloring") do @coloring = false Term::ANSIColor::coloring = false end opts.on('-V','--debug', "Print extra output for debugging.") do |json| @terminal_log_level = Morpheus::Logging::Logger::DEBUG Morpheus::Logging.set_log_level(Morpheus::Logging::Logger::DEBUG) ::RestClient.log = Morpheus::Logging.debug? ? Morpheus::Logging::DarkPrinter.instance : nil end opts.on( '-h', '--help', "Prints this help" ) do @stdout.puts opts # exit end end out << "Commands:\n" Morpheus::Cli::CliRegistry.all.keys.sort.each {|cmd| out << "\t#{cmd.to_s}\n" } # out << "Options:\n" out << optparse.to_s out << "\n" out << "For more information, see https://github.com/gomorpheus/morpheus-cli/wiki" out << "\n" out end |