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 $ nv config password EOD exit end end end end