Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default config command #67

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/gel/command/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

class Gel::Command::Config < Gel::Command
def run(command_line)
if command_line.size == 1
if command_line.empty?
Gel::Environment.config.all.each do |key,_|
value = Gel::Environment.config[key]
puts "#{key}=#{value}"
end
elsif command_line.size == 1
puts Gel::Environment.config[command_line.first]
else
Gel::Environment.config[command_line.shift] = command_line.join(" ")
Expand Down
50 changes: 13 additions & 37 deletions lib/gel/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Gel::Config
def initialize
@root = File.expand_path("~/.config/gel")
@root = ENV.fetch('GEL_CONFIG') { File.expand_path("~/.config/gel") }
@path = File.join(@root, "config")
@config = nil
end
Expand All @@ -11,21 +11,18 @@ def config
@config ||= read
end

def [](group = nil, key)
if group.nil?
group, key = key.split(".", 2)
end
def all
config
end

(group ? (config[group.to_s] || {}) : config)[key.to_s]
def [](group = nil, key)
key = "#{group}.#{key}" unless group.nil?
config[key.downcase]
end

def []=(group = nil, key, value)
if group.nil?
group, key = key.split(".", 2)
end

(group ? (config[group.to_s] ||= {}) : config)[key.to_s] = value.to_s

key = "#{group}.#{key}" unless group.nil?
config[key] = value.to_s
write(config)
end

Expand All @@ -34,20 +31,11 @@ def []=(group = nil, key, value)
def read
result = {}
if File.exist?(@path)
context = nil
File.read(@path).each_line do |line|
line.chomp!
if line =~ /\A(\S[^:]*):\z/
context = result[$1] = {}
elsif line =~ /\A ([^:]*): (.*)\z/
context[$1] = $2
elsif line =~ /\A([^:]*): (.*)\z/
result[$1] = $2
elsif line =~ /\A\s*(?:#|\z)/
# comment / empty
else
raise Gel::Error::UnexpectedConfigError.new(line: line)
end
key, value = line.split(':')
next unless key
result[key.downcase] = value&.strip
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewd I downcased to keep all settings consistent, also strip the value too. Without the null check & some tests fail, I think it's because somewhere we are using the default -- bundler version for the config where we run all our tests, is that how it's supposed to work?

end
end
result
Expand All @@ -58,19 +46,7 @@ def write(data)

tempfile = File.join(@root, ".config.#{Process.pid}")
File.open(tempfile, "w", 0644) do |f|
data.each do |key, value|
next if value.is_a?(Hash)
f.puts("#{key}: #{value}")
end

data.each do |key, value|
next unless value.is_a?(Hash)
f.puts
f.puts("#{key}:")
value.each do |subkey, subvalue|
f.puts(" #{subkey}: #{subvalue}")
end
end
data.each { |key, value| f.puts("#{key}: #{value}") }
end

File.rename(tempfile, @path)
Expand Down
Empty file added test/config/gel/config
Empty file.
36 changes: 36 additions & 0 deletions test/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "test_helper"

class ConfigTest < Minitest::Test
def setup
@old_gel_config_env = ENV['GEL_CONFIG']
ENV['GEL_CONFIG'] = fixture_file('')
end

def teardown
ENV['GEL_CONFIG'] = @old_gel_config_env
end

def test_reading_key_from_config_successully
klass = Gel::Config.new
assert_equal '316429e', klass['packages.example.io']
end

def test_reading_group_key_from_config_successully
klass = Gel::Config.new
assert_equal '316429e', klass['packages', 'example.io']
end

def test_reading_uppercase_key_from_config_successully
klass = Gel::Config.new
assert_equal 'true', klass['gem.MIT']
end

def test_reading_all_from_config_successully
settings = Gel::Config.new.all
assert_equal '316429e', settings['packages.example.io']
assert_equal 'true', settings['gem.mit']
assert_equal '10', settings['timeout']
end
end
3 changes: 3 additions & 0 deletions test/fixtures/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages.example.io: 316429e
gem.mit: true
timeout: 10