Class: OsCtl::Cli::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



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

def self.run(klass, method, method_args = [])
  proc do |global_opts, opts, args|
    OsCtl::Lib::Logger.setup(:none)

    cmd = klass.new(global_opts, opts, args)
    cmd.method(method).call(*method_args)
  end
end

Instance Method Details

#cli_optObject (protected)



68
69
70
# File 'lib/osctl/cli/command.rb', line 68

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

#format_output(data, **fmt_opts) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/osctl/cli/command.rb', line 57

def format_output(data, **fmt_opts)
  if gopts[:json]
    puts data.to_json

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

#osctld_call(cmd, **opts) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/osctl/cli/command.rb', line 23

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

#osctld_fmt(cmd, cmd_opts: {}, fmt_opts: {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/osctl/cli/command.rb', line 39

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

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

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

  ret
end

#osctld_openObject



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

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

#osctld_resp(cmd, **opts) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/osctl/cli/command.rb', line 31

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