Module: Onering::CLI::Config

Defined in:
lib/onering/cli/config.rb

Class Method Summary collapse

Class Method Details

.configure(global = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/onering/cli/config.rb', line 4

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

  @opts = ::Trollop::options do
    banner <<-EOS
Options:
EOS

    opt :id,    "The node ID of the device to operate on", :short => '-i', :type => :string, :default => Onering::Util.fact('hardwareid')

  # subcommands
    stop_on %w{get set test}
  end
end

.run(args) ⇒ Object



20
21
22
23
24
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
# File 'lib/onering/cli/config.rb', line 20

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

  case (sc.downcase.to_sym rescue nil)
# -----------------------------------------------------------------------------
  when :get
    raise "Expected 1 parameter, got #{args.length}" unless args.length >= 1

    begin
      rv = @api.request(@opts[:method], "devices/#{@opts[:id]}/config/#{args[0].gsub('.','/')}")
      return (rv.parsed_response rescue rv.response.body)
    rescue Onering::API::Errors::NotFound
      if args[1].nil? and not STDIN.tty?
        args[1] = STDIN.read()
      end

      return (MultiJson.load(args[1]) rescue args[1])
    end

# -----------------------------------------------------------------------------
  when :set
    raise "Expected 2 parameters, got #{args.length}" unless args.length == 2

    return @api.set_field(@opts[:id], "config.#{args[0]}", args[1])

# -----------------------------------------------------------------------------
  when :test
    raise "Expected 1 parameter, got #{args.length}" unless args.length >= 1

  else
    raise "Unknown subcommand #{sc.inspect}"
  end
end