Sha256: 3c23a4cccce1d8f51127f84dad4df18efb8b69a89c593ed1812f1dd1c942e70c

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8

module GithubCLI
  class Config
    COMMAND_KEY = 'commands'
    COMMAND_HELP = 'help'

    def initialize(config_filename=nil)
      @filename = config_filename || local_options_file
    end

    def []=(key, value)
      set_key(key, value)
      @data = nil
    end

    def [](key)
      data[key] || data[COMMAND_KEY][key]
    end

    def fetch(key, default=nil)
      data[key] || default || raise(IndexError.new("key #{key} not found"))
    end

    def delete(key)
      @data.delete(key)
    end

    def data
      @data ||= self.load
    end

    def save(config)
      config[COMMAND_KEY] = {}
      Command.all.each do |cmd|
        if !cmd.namespace.empty? && cmd.name != COMMAND_HELP
          config[COMMAND_KEY]["#{cmd.namespace}-#{cmd.name}"] = { }
        end
      end
      File.open(path, 'w', 0600) do |file|
        YAML.dump(config, file)
      end
    end

    def load
      yaml = {}
      if File.exists? path
        yaml = File.open(path, 'r') do |file|
          YAML.load(file)
        end
      end
      yaml
    end

    def path
      require 'pathname'
      if Pathname.new(@filename).absolute?
        @filename
      else
        File.join Thor::Util.user_home, "/#{@filename}"
      end
    end

    def custom_options_file
    end

    def local_options_file
      '.githubrc'
    end

    def global_options_file
      begin
        File.join Thor::Util.user_home, ".githubrc"
      rescue ArgumentError
        GithubCLI.ui.warn "Unable to find ~/.githubrc because the HOME environment variable is not set"
        nil
      end
    end

    def set_key(key, value)
      unless data[key] == value
        data[key] = value
        data.delete(key) if value.nil?
        save data.to_yaml
      end
      value
    end

  end # Config
end # GithubCLI

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
github_cli-0.1.2 lib/github_cli/config.rb
github_cli-0.1.1 lib/github_cli/config.rb