Class: OsCtl::Cli::Command

Inherits:
Object
  • Object
show all
Includes:
Utils::Humanize
Defined in:
lib/osctl/cli/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

#initialize(global_opts, opts, args) ⇒ Command

Returns a new instance of Command



16
17
18
19
20
# File 'lib/osctl/cli/command.rb', line 16

def initialize(global_opts, opts, args)
  @gopts = global_opts
  @opts = opts
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args



14
15
16
# File 'lib/osctl/cli/command.rb', line 14

def args
  @args
end

#goptsObject (readonly)

Returns the value of attribute gopts



14
15
16
# File 'lib/osctl/cli/command.rb', line 14

def gopts
  @gopts
end

#optsObject (readonly)

Returns the value of attribute opts



14
15
16
# File 'lib/osctl/cli/command.rb', line 14

def opts
  @opts
end

Class Method Details

.run(klass, method, method_args = []) ⇒ Object



7
8
9
10
11
12
# File 'lib/osctl/cli/command.rb', line 7

def self.run(klass, method, method_args = [])
  Proc.new do |global_opts, opts, args|
    cmd = klass.new(global_opts, opts, args)
    cmd.method(method).call(*method_args)
  end
end

Instance Method Details

#cli_optObject (protected)



82
83
84
# File 'lib/osctl/cli/command.rb', line 82

def cli_opt
  "#{File.basename($0)} #{ARGV.join(' ')}"
end

#format_output(data, cols = nil, fmt_opts = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/osctl/cli/command.rb', line 72

def format_output(data, cols = nil, fmt_opts = {})
  if gopts[:json]
    puts data.to_json

  else
    OutputFormatter.print(data, cols, fmt_opts)
  end
end

#osctld_call(cmd, opts = {}, &block) ⇒ Object



42
43
44
45
46
# File 'lib/osctl/cli/command.rb', line 42

def osctld_call(cmd, opts = {}, &block)
  c = osctld_open
  opts[:cli] ||= cli_opt
  c.cmd_data!(cmd, opts, &block)
end

#osctld_fmt(cmd, opts = {}, cols = nil, fmt_opts = {}, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/osctl/cli/command.rb', line 54

def osctld_fmt(cmd, opts = {}, cols = nil, fmt_opts = {}, &block)
  opts[:cli] ||= cli_opt

  if block
    ret = osctld_call(cmd, opts, &block)
  else
    ret = osctld_call(cmd, opts) { |msg| puts msg unless gopts[:quiet] }
  end

  if ret.is_a?(String)
    puts ret
  elsif ret
    format_output(ret, cols, fmt_opts)
  end

  ret
end

#osctld_openObject



36
37
38
39
40
# File 'lib/osctl/cli/command.rb', line 36

def osctld_open
  c = OsCtl::Client.new
  c.open
  c
end

#osctld_resp(cmd, opts = {}, &block) ⇒ Object



48
49
50
51
52
# File 'lib/osctl/cli/command.rb', line 48

def osctld_resp(cmd, opts = {}, &block)
  c = osctld_open
  opts[:cli] ||= cli_opt
  c.cmd_response(cmd, opts, &block)
end

#require_args!(*v) ⇒ Object

Parameters:

  • v (Array)

    list of required arguments

Raises:

  • (GLI::BadCommandLine)


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/osctl/cli/command.rb', line 23

def require_args!(*v)
  if v.count == 1 && v.first.is_a?(Array)
    required = v.first
  else
    required = v
  end

  return if args.count >= required.count

  arg = required[ args.count ]
  raise GLI::BadCommandLine, "missing argument <#{arg}>"
end