require 'ostruct' require 'yaml' require 'fileutils' 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(transform_keys(self.to_h){|k| k.to_s}) 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 $ nv config password EOD exit end end private def transform_keys(hs) result = {} hs.each_key do |key| result[yield(key)] = hs[key] end result end end end