Class: GithubCLI::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/config.rb

Constant Summary collapse

COMMAND_KEY =
'commands'.freeze
COMMAND_HELP =
'help'.freeze
DEFAULT_NAME =
'.githubrc'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ Config

Initialize a Config


24
25
26
27
28
29
# File 'lib/github_cli/config.rb', line 24

def initialize(root, options={})
  @root          = root
  @filename      = options.fetch(:filename) { DEFAULT_NAME }
  @local_config  = local_options_file
  @global_config = global_options_file
end

Instance Attribute Details

#filenameObject

Name for the confiugration file


19
20
21
# File 'lib/github_cli/config.rb', line 19

def filename
  @filename
end

#locationObject

Location scope of currently used configuration file.


16
17
18
# File 'lib/github_cli/config.rb', line 16

def location
  @location
end

#rootObject (readonly)

Contains information of where the command is run from.


13
14
15
# File 'lib/github_cli/config.rb', line 13

def root
  @root
end

Instance Method Details

#[](key) ⇒ Object


36
37
38
# File 'lib/github_cli/config.rb', line 36

def [](key)
  data[key] || data["#{COMMAND_KEY}.#{key}"] rescue nil
end

#[]=(key, value) ⇒ Object


31
32
33
34
# File 'lib/github_cli/config.rb', line 31

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

#allObject


56
57
58
# File 'lib/github_cli/config.rb', line 56

def all
  data
end

#dataObject


48
49
50
# File 'lib/github_cli/config.rb', line 48

def data
  @data ||= self.load
end

#delete(key) ⇒ Object


44
45
46
# File 'lib/github_cli/config.rb', line 44

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

#fetch(key, default = nil) ⇒ Object


40
41
42
# File 'lib/github_cli/config.rb', line 40

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

#keysObject


52
53
54
# File 'lib/github_cli/config.rb', line 52

def keys
  data.keys
end

#loadObject


84
85
86
87
88
89
90
91
92
# File 'lib/github_cli/config.rb', line 84

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

#pathObject


94
95
96
97
98
99
100
# File 'lib/github_cli/config.rb', line 94

def path
  if location == 'local' || File.exists?(local_options_file)
    local_options_file
  else
    global_options_file
  end
end

#pretty(pattern = "") ⇒ Object


60
61
62
63
64
# File 'lib/github_cli/config.rb', line 60

def pretty(pattern="")
  all.keys.zip(all.values).map do |el|
    el.last.nil? ? [el.first, 'UNDEFINED'] : el
  end.select { |el| el.first =~ /^#{pattern}.*$/i }
end

#save(config) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/github_cli/config.rb', line 71

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