From 8877fc1c2c3422d9ef5d445a2e40440debd5387d Mon Sep 17 00:00:00 2001 From: Lance <281239768@qq.com> Date: Mon, 8 Feb 2021 15:50:22 +0800 Subject: [PATCH] config: Refactor default config reading from file (#58) * config: Refactor default config reading from file * move config.yaml out of lib * apply rubocop * fix freeze --- lib/qingstor/sdk/general/config.rb | 36 +++++++++++-------- lib/qingstor/sdk/general/contract.rb | 3 -- lib/qingstor/sdk/request/preprocessor.rb | 6 ++-- spec/qingstor/sdk/general/config_spec.rb | 14 ++++---- spec/qingstor/sdk/general/contract_spec.rb | 8 ----- .../qingstor/sdk/general/default/config.yaml | 0 test/qingstor-sdk.rb | 2 +- 7 files changed, 32 insertions(+), 37 deletions(-) rename {lib => spec}/qingstor/sdk/general/default/config.yaml (100%) diff --git a/lib/qingstor/sdk/general/config.rb b/lib/qingstor/sdk/general/config.rb index c252c19..426e021 100644 --- a/lib/qingstor/sdk/general/config.rb +++ b/lib/qingstor/sdk/general/config.rb @@ -29,6 +29,15 @@ module SDK class Config < Hash attr_accessor :connection + DEFAULT_AS_HASH = { + host: 'qingstor.com'.freeze, + port: 443, + protocol: 'https'.freeze, + connection_retries: 3, + log_level: 'warn'.freeze, + enable_virtual_host_style: false + } + def self.init(access_key_id, secret_access_key) initial_config = { access_key_id: access_key_id, @@ -77,7 +86,7 @@ def check else # if endpoint set, host/port/protocol ignore, and warn %i[host port protocol].each do |x| - if self[x].present? + if self[x].present? && !Config.is_default?(x, self[x]) Logger.warn "Endpoint configured, #{x.to_sym} will be ignored" end end @@ -108,7 +117,7 @@ def check end def load_default_config - load_config_from_file Contract::DEFAULT_CONFIG_FILEPATH + update DEFAULT_AS_HASH end def load_user_config @@ -135,7 +144,7 @@ def load_env_config end unless ENV[Contract::ENV_ENDPOINT].nil? another_config[:endpoint] = - ENV[Contract::ENV_ENDPOINT] + ENV[Contract::ENV_ENDPOINT] end update another_config end @@ -147,12 +156,6 @@ def load_config_from_file(path) private - def install_default_user_config - Logger.warn "Installing default config file to #{Contract::USER_CONFIG_FILEPATH}" - FileUtils.mkdir_p Contract::USER_SUPPORT_DIRECTORY - FileUtils.copy Contract::DEFAULT_CONFIG_FILEPATH, Contract::USER_CONFIG_FILEPATH - end - # load user config if path exist, and skip check def load_user_config_path_exist # if env path configured, update from env; otherwise, if ~/.qingstor/config.yaml exists, update from this @@ -168,12 +171,15 @@ def parse_boolean(key) end def is_valid_ip?(ip) - begin - IPAddr.new ip - return true - rescue - return false - end + IPAddr.new ip + true + rescue + false + end + + # @return boolean + def self.is_default?(key, value) + DEFAULT_AS_HASH[key.to_sym].present? && DEFAULT_AS_HASH[key.to_sym] == value end end end diff --git a/lib/qingstor/sdk/general/contract.rb b/lib/qingstor/sdk/general/contract.rb index 92301dd..bc47e7e 100644 --- a/lib/qingstor/sdk/general/contract.rb +++ b/lib/qingstor/sdk/general/contract.rb @@ -23,9 +23,6 @@ module Contract # GEM_DIRECTORY = Gem::Specification.find_by_name('qingcloud-sdk').gem_dir # DEFAULT_SUPPORT_DIRECTORY = GEM_DIRECTORY + '/lib/qingcloud/sdk/commons/default' - DEFAULT_SUPPORT_DIRECTORY = File.expand_path(File.dirname(__FILE__) + '/./default') - DEFAULT_CONFIG_FILENAME = 'config.yaml'.freeze - DEFAULT_CONFIG_FILEPATH = "#{DEFAULT_SUPPORT_DIRECTORY}/#{DEFAULT_CONFIG_FILENAME}".freeze ENV_ACCESS_KEY_ID = 'QINGSTOR_ACCESS_KEY_ID'.freeze ENV_SECRET_ACCESS_KEY = 'QINGSTOR_SECRET_ACCESS_KEY'.freeze diff --git a/lib/qingstor/sdk/request/preprocessor.rb b/lib/qingstor/sdk/request/preprocessor.rb index f20ba53..d7460cf 100644 --- a/lib/qingstor/sdk/request/preprocessor.rb +++ b/lib/qingstor/sdk/request/preprocessor.rb @@ -182,7 +182,7 @@ def self.escape(origin) # both above will add default schema at the start, and parse again def self.parse_endpoint(endpoint) if endpoint.blank? - raise "endpoint should not be empty when parse" + raise 'endpoint should not be empty when parse' end begin @@ -195,12 +195,12 @@ def self.parse_endpoint(endpoint) end endpoint = "http://#{endpoint}" # add default scheme for endpoint - return URI.parse endpoint + URI.parse endpoint end private - URI_BUCKET_PREFIX = "/".freeze + URI_BUCKET_PREFIX = '/'.freeze end end end diff --git a/spec/qingstor/sdk/general/config_spec.rb b/spec/qingstor/sdk/general/config_spec.rb index dae7897..424de8a 100644 --- a/spec/qingstor/sdk/general/config_spec.rb +++ b/spec/qingstor/sdk/general/config_spec.rb @@ -57,7 +57,7 @@ module SDK end it 'can load config from file' do - config = Config.new.load_config_from_file Contract::DEFAULT_CONFIG_FILEPATH + config = Config.new.load_config_from_file File.expand_path('../default/config.yaml', __FILE__) expect(config[:access_key_id]).to be nil expect(config[:host]).to eq 'qingstor.com' expect(config[:log_level]).to eq 'warn' @@ -92,7 +92,7 @@ module SDK config = Config.new begin config.update(enable_virtual_host_style: true, - host: "192.168.0.1") + host: '192.168.0.1') rescue ConfigurationError expect(true).to be true end @@ -102,7 +102,7 @@ module SDK config = Config.new begin config.update(enable_virtual_host_style: true, - endpoint: "192.168.0.1:3000") + endpoint: '192.168.0.1:3000') rescue ConfigurationError expect(true).to be true end @@ -111,17 +111,17 @@ module SDK it 'can enable vhost with host' do config = Config.new config.update(enable_virtual_host_style: true, - host: "qingstor.dev") + host: 'qingstor.dev') expect(config[:enable_virtual_host_style]).to eq true - expect(config[:host]).to eq "qingstor.dev" + expect(config[:host]).to eq 'qingstor.dev' end it 'can enable vhost with host in endpoint' do config = Config.new config.update(enable_virtual_host_style: true, - endpoint: "http://qingstor.dev") + endpoint: 'http://qingstor.dev') expect(config[:enable_virtual_host_style]).to eq true - expect(config[:endpoint]).to eq "http://qingstor.dev" + expect(config[:endpoint]).to eq 'http://qingstor.dev' end it 'can check itself' do diff --git a/spec/qingstor/sdk/general/contract_spec.rb b/spec/qingstor/sdk/general/contract_spec.rb index 5d8336a..fecc98f 100644 --- a/spec/qingstor/sdk/general/contract_spec.rb +++ b/spec/qingstor/sdk/general/contract_spec.rb @@ -22,14 +22,6 @@ module SDK it 'should know user config file path' do expect(Contract::USER_CONFIG_FILEPATH).to eq "#{Dir.home}/.qingstor/config.yaml" end - - it 'should know default config file path' do - expect(Contract::DEFAULT_CONFIG_FILEPATH.end_with?('default/config.yaml')).to be true - end - - it 'should make sure default config file exists' do - expect(File.exist?(Contract::DEFAULT_CONFIG_FILEPATH)).to be true - end end end end diff --git a/lib/qingstor/sdk/general/default/config.yaml b/spec/qingstor/sdk/general/default/config.yaml similarity index 100% rename from lib/qingstor/sdk/general/default/config.yaml rename to spec/qingstor/sdk/general/default/config.yaml diff --git a/test/qingstor-sdk.rb b/test/qingstor-sdk.rb index 7eece21..f639544 100644 --- a/test/qingstor-sdk.rb +++ b/test/qingstor-sdk.rb @@ -1,2 +1,2 @@ $LOAD_PATH << File.expand_path('../../lib', __FILE__) -require 'qingstor/sdk' \ No newline at end of file +require 'qingstor/sdk'