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 --config to specify config file location #394

Merged
merged 1 commit into from
Oct 28, 2021
Merged
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
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