Skip to content

Commit

Permalink
config: Refactor default config reading from file (#58)
Browse files Browse the repository at this point in the history
* config: Refactor default config reading from file

* move config.yaml out of lib

* apply rubocop

* fix freeze
  • Loading branch information
Prnyself committed Feb 8, 2021
1 parent b53b1d2 commit 8877fc1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 37 deletions.
36 changes: 21 additions & 15 deletions lib/qingstor/sdk/general/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions lib/qingstor/sdk/general/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/qingstor/sdk/request/preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = "/<bucket-name>".freeze
URI_BUCKET_PREFIX = '/<bucket-name>'.freeze
end
end
end
14 changes: 7 additions & 7 deletions spec/qingstor/sdk/general/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions spec/qingstor/sdk/general/contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/qingstor-sdk.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
require 'qingstor/sdk'
require 'qingstor/sdk'

0 comments on commit 8877fc1

Please sign in to comment.