Class: Hivonic::Commands::Subcommand

Inherits:
Object
  • Object
show all
Defined in:
lib/hivonic/commands.rb

Direct Known Subclasses

HiveQueryCommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, args) ⇒ Subcommand

Returns a new instance of Subcommand.



47
48
49
50
51
52
53
54
# File 'lib/hivonic/commands.rb', line 47

def initialize(opts, args)
  @regexp           = Regexp.new opts['regexp']
  @time_format      = opts['time-format']
  @time_group_index = opts['time-group-index'].to_i
  @ttl              = opts['ttl'].to_i
  @dry_run          = opts['dry-run']
  @exitstatus       = 1
end

Instance Attribute Details

#dry_runObject (readonly)

Returns the value of attribute dry_run.



44
45
46
# File 'lib/hivonic/commands.rb', line 44

def dry_run
  @dry_run
end

#exitstatusObject (readonly)

Returns the value of attribute exitstatus.



45
46
47
# File 'lib/hivonic/commands.rb', line 45

def exitstatus
  @exitstatus
end

#queryObject (readonly)

Returns the value of attribute query.



42
43
44
# File 'lib/hivonic/commands.rb', line 42

def query
  @query
end

#regexpObject (readonly)

Returns the value of attribute regexp.



38
39
40
# File 'lib/hivonic/commands.rb', line 38

def regexp
  @regexp
end

#subcommandObject (readonly)

Returns the value of attribute subcommand.



43
44
45
# File 'lib/hivonic/commands.rb', line 43

def subcommand
  @subcommand
end

#time_formatObject (readonly)

Returns the value of attribute time_format.



39
40
41
# File 'lib/hivonic/commands.rb', line 39

def time_format
  @time_format
end

#time_group_indexObject (readonly)

Returns the value of attribute time_group_index.



40
41
42
# File 'lib/hivonic/commands.rb', line 40

def time_group_index
  @time_group_index
end

#ttlObject (readonly)

Returns the value of attribute ttl.



41
42
43
# File 'lib/hivonic/commands.rb', line 41

def ttl
  @ttl
end

Class Method Details

.run(opts, args) ⇒ Object



56
57
58
59
# File 'lib/hivonic/commands.rb', line 56

def self.run(opts, args)
  output, exitstatus = self.new(opts, args).run
  return output, exitstatus
end

Instance Method Details

#is_successful?(status) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/hivonic/commands.rb', line 88

def is_successful?(status)
  status == 0
end

#output(stdout) ⇒ Object



65
66
67
# File 'lib/hivonic/commands.rb', line 65

def output(stdout)
  stdout
end

#runObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hivonic/commands.rb', line 69

def run
  raise "subcommand can't be nil" if subcommand.nil?

  if dry_run
    stdout      = "DRY RUN: Subcommand => #{subcommand}"
    @exitstatus = 0
  else
    puts "Running: Subcommand => #{subcommand}"
    stdout      = `#{subcommand}`
    @exitstatus = $?.exitstatus
  end

  return output(stdout), self.exitstatus
end

#successful?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/hivonic/commands.rb', line 84

def successful?
  self.exitstatus == 0
end