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



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

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



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

def args
  @args
end

#goptsObject (readonly)

Returns the value of attribute gopts



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

def gopts
  @gopts
end

#optsObject (readonly)

Returns the value of attribute opts



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

def opts
  @opts
end

Class Method Details

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



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

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)



87
88
89
# File 'lib/osctl/cli/command.rb', line 87

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

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



77
78
79
80
81
82
83
84
# File 'lib/osctl/cli/command.rb', line 77

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

  else
    OsCtl::Lib::Cli::OutputFormatter.print(data, cols, fmt_opts)
  end
end

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



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

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

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/osctl/cli/command.rb', line 59

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



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

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

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



51
52
53
54
55
56
57
# File 'lib/osctl/cli/command.rb', line 51

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

#require_args!(*v) ⇒ Object

Parameters:

  • v (Array)

    list of required arguments

Raises:

  • (GLI::BadCommandLine)


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

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