Class: Samovar::Command
- Inherits:
-
Object
- Object
- Samovar::Command
- Defined in:
- lib/samovar/command.rb,
lib/samovar/command/system.rb,
lib/samovar/command/track_time.rb
Class Attribute Summary collapse
-
.description ⇒ Object
Returns the value of attribute description.
Class Method Summary collapse
- .append(row) ⇒ Object
- .command_line(name) ⇒ Object
- .many(*args, **options) ⇒ Object
- .nested(*args, **options) ⇒ Object
- .one(*args, **options) ⇒ Object
- .options(*args, **options, &block) ⇒ Object
- .parse(input) ⇒ Object
- .split(*args, **options) ⇒ Object
- .table ⇒ Object
- .usage(rows, name) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(input = nil) ⇒ Command
constructor
A new instance of Command.
- #print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter) ⇒ Object
- #system(*args, **options) ⇒ Object
- #system!(*args, **options) ⇒ Object
- #track_time ⇒ Object
Constructor Details
#initialize(input = nil) ⇒ Command
Returns a new instance of Command.
46 47 48 49 50 |
# File 'lib/samovar/command.rb', line 46 def initialize(input = nil) self.class.table.parse(input) do |key, value| self.send("#{key}=", value) end if input end |
Class Attribute Details
.description ⇒ Object
Returns the value of attribute description.
57 58 59 |
# File 'lib/samovar/command.rb', line 57 def description @description end |
Class Method Details
.append(row) ⇒ Object
64 65 66 67 68 |
# File 'lib/samovar/command.rb', line 64 def self.append(row) attr_accessor(row.key) if row.respond_to?(:key) self.table << row end |
.command_line(name) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/samovar/command.rb', line 104 def self.command_line(name) if @table "#{name} #{@table.usage}" else name end end |
.many(*args, **options) ⇒ Object
82 83 84 |
# File 'lib/samovar/command.rb', line 82 def self.many(*args, **) append Many.new(*args, **) end |
.nested(*args, **options) ⇒ Object
74 75 76 |
# File 'lib/samovar/command.rb', line 74 def self.nested(*args, **) append Nested.new(*args, **) end |
.one(*args, **options) ⇒ Object
78 79 80 |
# File 'lib/samovar/command.rb', line 78 def self.one(*args, **) append One.new(*args, **) end |
.options(*args, **options, &block) ⇒ Object
70 71 72 |
# File 'lib/samovar/command.rb', line 70 def self.(*args, **, &block) append Options.parse(*args, **, &block) end |
.parse(input) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/samovar/command.rb', line 38 def self.parse(input) command = self.new(input) raise IncompleteParse.new("Could not parse #{input}") unless input.empty? return command end |
.split(*args, **options) ⇒ Object
86 87 88 |
# File 'lib/samovar/command.rb', line 86 def self.split(*args, **) append Split.new(*args, **) end |
.table ⇒ Object
60 61 62 |
# File 'lib/samovar/command.rb', line 60 def self.table @table ||= Table.new end |
.usage(rows, name) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/samovar/command.rb', line 90 def self.usage(rows, name) rows.nested(name, self) do |rows| return unless @table @table.rows.each do |row| if row.respond_to?(:usage) row.usage(rows) else rows << row end end end end |
Instance Method Details
#[](key) ⇒ Object
52 53 54 |
# File 'lib/samovar/command.rb', line 52 def [] key @attributes[key] end |
#print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/samovar/command.rb', line 112 def print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter) rows = Output::Rows.new self.class.usage(rows, *args) formatter.print(rows, output) end |
#system(*args, **options) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/samovar/command/system.rb', line 28 def system(*args, **) command_line = args.join(' ') pid = Process.spawn(*args, **) puts Rainbow(command_line).color(:blue) status = Process.waitpid2(pid).last return status.success? rescue Errno::ENOENT return false end |
#system!(*args, **options) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/samovar/command/system.rb', line 42 def system!(*args, **) command_line = args.join(' ') pid = Process.spawn(*args, **) puts Rainbow(command_line).color(:blue) status = Process.waitpid2(pid).last if status.success? return true else raise SystemError.new("Command #{command_line.dump} failed: #{status.to_s}") end rescue Errno::ENOENT raise SystemError.new("Command #{command_line.dump} failed: #{$!}") end |
#track_time ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/samovar/command/track_time.rb', line 25 def track_time start_time = Time.now yield ensure end_time = Time.now elapsed_time = end_time - start_time $stdout.flush $stderr.puts Rainbow("Elapsed Time: %0.3fs" % elapsed_time).magenta end |