Sha256: 07db297a0118202833ceffdb01b81b0bbf6196156ca80457a5b0e8c8c167d30b

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require 'ostruct'
require 'yaml'
require 'fileutils'
require 'active_support/core_ext'

class String
  def undent
    gsub(/^.{#{slice(/^ +/).length}}/, '')
  end
end

module Nv
  class Config < OpenStruct
    def initialize(config_path)
      # Initialize config file
      @config_path = config_path
      config_dir = File.dirname(@config_path)

      Dir.mkdir(config_dir) unless Dir.exist?(config_dir)
      FileUtils.touch(@config_path) unless File.exist?(@config_path)

      @config = YAML.load_file(@config_path) || {}
      super(@config)
    end

    def save
      File.open(@config_path, 'w') do |f|
        f.print YAML.dump(self.to_h.stringify_keys)
      end
    end

    def verify_for_authentication
      self.email && self.password
    end

    def verify_for_authentication!(cmd)
      unless verify_for_authentication
        puts <<-EOD.undent
        `nv #{cmd}` should be given email and password.
        $ nv config email <email>
        $ nv config password <password>
        EOD
        exit
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nv-1.0.1 lib/nv/config.rb
nv-1.0.0 lib/nv/config.rb