Sha256: b74b70bf13b436606aa562752649fa0feb6839d7c91fcfcb8320829c1173d5b1

Contents?: true

Size: 910 Bytes

Versions: 3

Compression:

Stored size: 910 Bytes

Contents

require 'nrser'
require 'nrser/exec'

using NRSER

module StateMate; end
module StateMate::Adapters; end

module StateMate::Adapters::NVRAM
  def self.read key, options = {}
    cmd = NRSER::Exec.sub "nvram %{key}", key: key

    begin
      output = NRSER::Exec.run cmd
    rescue SystemCallError => e
      if e.message.include? "nvram: Error getting variable"
        return nil
      else
        raise e
      end
    end

    if m = /^#{ key }\t(.*)\n$/.match(output)
      m[1]
    else
      raise tpl binding, <<-BLOCK
        can't parse output for key <%= key.inspect %>:

          cmd: <%= cmd.inspect %>

          output: <%= output.inspect %>
      BLOCK
    end
  end

  def self.write key, value, options = {}
    unless value.is_a? String 
      raise "value must be a String, not #{ value.inspect }"
    end

    cmd = "nvram #{ key }='#{ value }'"
    NRSER::Exec.run cmd
  end
end # NVRAM

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
state_mate-0.0.3 lib/state_mate/adapters/nvram.rb
state_mate-0.0.2 lib/state_mate/adapters/nvram.rb
state_mate-0.0.1 lib/state_mate/adapters/nvram.rb