Skip to content

Commit

Permalink
add --config to specify config file location (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
krtschmr authored Oct 28, 2021
1 parent 265b80d commit 134c660
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ en:
desc:
all_locales: Do not expect key patterns to start with a locale, instead apply them to all
locales implicitly.
config: Override config location
confirm: Confirm automatically
data_format: 'Data format: %{valid_text}.'
keep_order: Keep the order of the keys
Expand Down
3 changes: 2 additions & 1 deletion lib/i18n/tasks/base_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class BaseTask
include Data
include Stats

def initialize(config = {})
def initialize(config = {}, config_file: nil)
@config_override = config_file
self.config = config || {}
end

Expand Down
17 changes: 14 additions & 3 deletions lib/i18n/tasks/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ def start(argv)
end

def run(argv)
I18n.with_locale(base_task.internal_locale) do
argv.each_with_index do |arg, i|
if ["--config", "-c"].include?(arg)
if File.exist?(argv[i+1])
@config_file = argv[i+1]
break
else
error "Config file doesn't exist: #{argv[i+1]}", 128
end
end
end

I18n.with_locale(base_task(config_file: @config_file).internal_locale) do
name, *options = parse!(argv.dup)
context.run(name, *options)
end
Expand All @@ -52,8 +63,8 @@ def commands

private

def base_task
@base_task ||= I18n::Tasks::BaseTask.new
def base_task(config_file: nil)
@base_task ||= I18n::Tasks::BaseTask.new(config_file: config_file)
end

def parse!(argv)
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/command/commands/health.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Health
cmd :health,
pos: '[locale ...]',
desc: t('i18n_tasks.cmd.desc.health'),
args: %i[locales out_format]
args: %i[locales out_format config]

def health(opt = {})
forest = i18n.data_forest(opt[:locales])
Expand Down
6 changes: 6 additions & 0 deletions lib/i18n/tasks/command/options/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ module Common
'--value VALUE',
t('i18n_tasks.cmd.args.desc.value')

arg :config,
'-c',
'--config FILE',
t('i18n_tasks.cmd.args.desc.config')


def arg_or_pos!(key, opts)
opts[key] ||= opts[:arguments].try(:shift)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def config
].freeze

def file_config
file = CONFIG_FILES.detect { |f| File.exist?(f) }
file = @config_override || CONFIG_FILES.detect { |f| File.exist?(f) }
# rubocop:disable Security/Eval
config = file && YAML.load(eval(Erubi::Engine.new(File.read(file, encoding: 'UTF-8')).src))
# rubocop:enable Security/Eval
Expand Down

0 comments on commit 134c660

Please sign in to comment.