Class: Onering::CLI::Devices

Inherits:
Plugin
  • Object
show all
Defined in:
lib/onering/cli/devices.rb

Class Method Summary collapse

Methods inherited from Plugin

default_format, inherited, registered_plugins

Class Method Details

.configure(global = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/onering/cli/devices.rb', line 4

def self.configure(global={})
  @api = Onering::CLI.connect(global).assets

  @opts = ::Trollop::options do
    banner <<-EOS
[DEPRECATED in 0.1.3] Use the 'assets' plugin from now on.

Usage:
    onering [global] report [options]

Options:
EOS

    opt :query, "The Onering urlquery to filter devices by", :short => '-f', :type => :string
    opt :id,    "The node ID of the device to operate on", :short => '-i', :type => :string

  # subcommands
    stop_on %w{show get set list find save}
  end
end

.run(args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/onering/cli/devices.rb', line 25

def self.run(args)
  sc = args.shift

  case (sc.downcase.to_sym rescue nil)
# -----------------------------------------------------------------------------
  when :show
    return @api.devices.show(args[0])

# -----------------------------------------------------------------------------
  when :get
    Onering::Logger.fatal!("Expected 1 parameter, got #{args.length}", "Onering::CLI::Devices") unless args.length == 1

    if @opts[:query_given]
    # doing this until a bulk field query endpoint is built
      return @api.list('id', {
        :filter => @opts[:query]
      }).collect{|i| @api.get_field(i, args[0])}

    elsif @opts[:id_given]
      return @api.get_field(@opts[:id], args[0])

    end

# -----------------------------------------------------------------------------
  when :set
    Onering::Logger.fatal!("Expected 2 parameters, got #{args.length}", "Onering::CLI::Devices") unless args.length == 2

    if @opts[:query]
    # doing this until a bulk field set endpoint is built
      return @api.list('id', {
        :filter => @opts[:query]
      }).collect{|i| @api.set_field(i, args[0])}

    elsif @opts[:id]
      return @api.set_field(@opts[:id], args[0], args[1])

    end

# -----------------------------------------------------------------------------
  when :list
    Onering::Logger.fatal!("Expected 1 parameter, got #{args.length}", "Onering::CLI::Devices") unless args.length >= 1
    return @api.list(args[0], {
      :filter => [@opts[:query], args[1]].compact.join('/')
    }.compact)

# -----------------------------------------------------------------------------
  when :find
    Onering::Logger.fatal!("Expected 1 parameter, got #{args.length}", "Onering::CLI::Devices") unless args.length == 1
    return @api.find(args[0])

# -----------------------------------------------------------------------------
  when :save
    rv = @api.save(args[0] || @opts[:id]) do
    # read from pipe
      if not STDIN.tty?
        STDIN.read()

    # read from specified file
      elsif (File.readable?(args[1]) rescue false)
        File.read(args[1])

      else
        Onering::Logger.fatal!("Cannot save data, no input data specified", "Onering::CLI::Devices")
      end
    end

    rv.parsed_response
  else
    Onering::Logger.fatal!("Unknown subcommand #{sc.inspect}", "Onering::CLI::Devices")
  end
end