From 21a91ae14ab62091588486d4c23c2080c0cbff49 Mon Sep 17 00:00:00 2001 From: Christopher Dell Date: Thu, 22 Nov 2012 12:14:51 +0100 Subject: [PATCH] Look for the api key in either the LOCALEAPP_API_KEY env variable or try and read it from a .env file --- bin/localeapp | 10 ++++++++++ features/add.feature | 27 ++++++++++++++++++++++++++ features/step_definitions/cli_steps.rb | 17 ++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/bin/localeapp b/bin/localeapp index c22c82f..96b7061 100755 --- a/bin/localeapp +++ b/bin/localeapp @@ -24,6 +24,16 @@ else end pre do |global_options, command, options, args| + global_options[:k] = if global_options[:k] + global_options[:k] + elsif ENV['LOCALEAPP_API_KEY'] + ENV['LOCALEAPP_API_KEY'] + elsif File.exist?('.env') && IO.read('.env') =~ /^LOCALEAPP_API_KEY=(\w+)$/ + $1 + else + nil + end + if Localeapp.has_config_file? || !global_options[:k].nil? true else diff --git a/features/add.feature b/features/add.feature index ea75da4..eea8311 100644 --- a/features/add.feature +++ b/features/add.feature @@ -44,3 +44,30 @@ Feature: Adding a translation from the command line Sending key: foo.baz Success! """ + + Scenario: Running add with no initializer file, passing the key via an ENV variable + In order to add a key and translation content + When I have a valid project on localeapp.com with api key "MYAPIKEY" + When I have a LOCALEAPP_API_KEY env variable set to "MYAPIKEY" + When I run `localeapp add foo.baz en:"test en content"` + Then the output should contain: + """ + Localeapp Add + + Sending key: foo.baz + Success! + """ + Then I clear the LOCALEAPP_API_KEY env variable + + Scenario: Running add with no initializer file, passing the key via a .env file + In order to add a key and translation content + When I have a valid project on localeapp.com with api key "MYAPIKEY" + When I have a .env file containing the api key "MYAPIKEY" + When I run `localeapp add foo.baz en:"test en content"` + Then the output should contain: + """ + Localeapp Add + + Sending key: foo.baz + Success! + """ diff --git a/features/step_definitions/cli_steps.rb b/features/step_definitions/cli_steps.rb index 57d2ec3..992d12a 100644 --- a/features/step_definitions/cli_steps.rb +++ b/features/step_definitions/cli_steps.rb @@ -41,6 +41,23 @@ add_fake_web_uri(:post, uri + '/rename', ['200', 'OK'], '') end +When /^I have a LOCALEAPP_API_KEY env variable set to "(.*?)"$/ do |api_key| + ENV['LOCALEAPP_API_KEY'] = api_key +end + +Then /^I clear the LOCALEAPP_API_KEY env variable$/ do + ENV['LOCALEAPP_API_KEY'] = nil +end + +When /^I have a \.env file containing the api key "(.*?)"$/ do |api_key| + steps %Q{ + And a file named ".env" with: + """ + LOCALEAPP_API_KEY=#{api_key} + """ + } +end + When /^an initializer file$/ do steps %Q{ And a file named "config/initializers/localeapp.rb" with: