Sha256: dea85e3581cbe4b1ff80b8220d58c3662f718cbf9b9c5c91de6f4fe6060c936d

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'yaml/store'

module Pair
  HOST = ENV['HOST'] || "api.pairmill.com"

  def self.config(options = nil)
    config = Config.new(HOST, STDIN, STDOUT)
    config.update(options) if options
    config
  end

  class Config
    attr_accessor :host, :input, :output

    def initialize(host = HOST, input = STDIN, output = STDOUT)
      self.host   = host
      self.input  = input
      self.output = output
    end

#    def api_token
#      self[:api_token] ||= begin
#        print "Please input your API token for Pair: "
#        gets.chomp
#      end
#    end

    def method_missing(method, *args, &block)
      method = method.to_s

      if method =~ /=$/ # setter
        self[method.gsub(/=$/,'').to_sym] = [*args].first
      else
        self[method.to_sym] || super
      end
    end

    def api_token
      self[:api_token]
    end

    def enable_ssh
      self[:enable_ssh]
    end

    def ssh_enabled?
      if Pair::OS.x?
        `systemsetup -getremotelogin`.match("Remote Login: On")
      end
    end

    def update(options = {})
      if options.is_a? Hash
        options.each do |k,v|
          config.transaction do
            host_config[k] = v
          end
        end
      end
    end

    protected
    def [](key)
      config.transaction do
        host_config[key]
      end
    end

    def []=(key, value)
      config.transaction do
        host_config[key] = value
      end
    end

    # Must be called within a config transaction
    def host_config
      config[host] ||= {}
    end

    def config
      @config ||= YAML::Store.new(config_file)
    end

    def config_file
      File.join(ENV['HOME'], ".pair.yml")
    end

    def print(*args)
      output.print(*args)
    end

    def gets(*args)
      input.gets(*args)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pair-0.0.3 lib/pair/config.rb