@@ -3,17 +3,23 @@ require "option_parser"
33require " colorize"
44require " ./fx-calculator"
55
6+ DEFAULT_CONFIG_PATH =
7+ Path [" ~" , " .config" , " fx-calculator" , " config.yml" ].expand(home: true )
8+
69Log .setup_from_env
710
811clear_currency_rates_cache = false
912config_path =
10- if File .exists?(FXCalculator :: Config :: DEFAULT_CONFIG_PATH )
11- FXCalculator :: Config :: DEFAULT_CONFIG_PATH
13+ if File .exists?(DEFAULT_CONFIG_PATH )
14+ DEFAULT_CONFIG_PATH
1215 end
1316
1417currency_code = ENV [" FX_CALCULATOR_CURRENCY" ]?.presence
1518currency_rates_ttl = ENV [" FX_CALCULATOR_CURRENCY_RATES_TTL" ]?.presence
1619
20+ rate_store_name = ENV [" FX_CALCULATOR_RATE_STORE" ]?.presence
21+ rate_store_opts = ENV [" FX_CALCULATOR_RATE_STORE_OPTIONS" ]?.presence
22+
1723rate_provider_name = ENV [" FX_CALCULATOR_RATE_PROVIDER" ]?.presence
1824rate_provider_opts = ENV [" FX_CALCULATOR_RATE_PROVIDER_OPTIONS" ]?.presence
1925
@@ -26,19 +32,25 @@ option_parser = OptionParser.new do |parser|
2632 clear_currency_rates_cache = true
2733 end
2834 parser.on(" -c PATH" , " --config=PATH" , " Path to configuration file" ) do |path |
29- config_path = Path [path] if path.presence
35+ config_path = Path [path].expand( home: true ) if path.presence
3036 end
3137 parser.on(" -C CURRENCY" , " --currency=CODE" , " Default target currency" ) do |code |
32- currency_code = code.presence
38+ currency_code = code if code .presence
3339 end
3440 parser.on(" -t TTL" , " --currency-rates-ttl=TIME_SPAN" , " Currency rates TTL" ) do |ttl |
35- currency_rates_ttl = ttl.presence
41+ currency_rates_ttl = ttl if ttl.presence
42+ end
43+ parser.on(" -s RATE_STORE" , " --store=NAME" , " Currency store to use" ) do |name |
44+ rate_store_name = name if name.presence
45+ end
46+ parser.on(" -S RATE_STORE_OPTIONS" , " --store-options=JSON" , " Currency store options" ) do |opts |
47+ rate_store_opts = opts if opts.presence
3648 end
3749 parser.on(" -p RATE_PROVIDER" , " --provider=NAME" , " Currency provider to use" ) do |name |
38- rate_provider_name = name.presence
50+ rate_provider_name = name if name .presence
3951 end
40- parser.on(" -o RATE_PROVIDER_OPTIONS" , " --provider-options=JSON" , " Currency provider options" ) do |opts |
41- rate_provider_opts = opts.presence
52+ parser.on(" -P RATE_PROVIDER_OPTIONS" , " --provider-options=JSON" , " Currency provider options" ) do |opts |
53+ rate_provider_opts = opts if opts .presence
4254 end
4355 parser.on(" -v" , " --version" , " Print version" ) do
4456 puts FXCalculator ::VERSION
@@ -48,63 +60,35 @@ option_parser = OptionParser.new do |parser|
4860 puts parser
4961 exit(0 )
5062 end
51- parser.unknown_args do |args |
52- values.concat(args)
53- end
5463 parser.invalid_option do |flag |
5564 STDERR .puts " ERROR: #{ flag } is not a valid option" .colorize(:red )
5665 STDERR .puts parser
5766 exit(1 )
5867 end
68+ parser.unknown_args do |args |
69+ values.concat(args)
70+ end
5971end
6072
6173option_parser.parse
6274
63- begin
64- if clear_currency_rates_cache
65- FXCalculator ::Config .clear_currency_rates_cache!
66- end
67-
68- if values.empty?
69- if clear_currency_rates_cache
70- exit(0 )
71- else
72- STDERR .puts option_parser
73- exit(1 )
74- end
75- end
76-
77- config =
78- if path = config_path
79- File .open(path) do |file |
80- FXCalculator ::Config .from_yaml(file)
81- end
82- else
83- FXCalculator ::Config .from_yaml(" {}" )
84- end
85-
86- if code = currency_code
87- config.currency = Money ::Currency .find(code)
88- end
89-
90- if name = rate_provider_name
91- config.rate_provider = begin
92- klass = Money ::Currency ::RateProvider .find(name)
93-
94- if opts = rate_provider_opts
95- klass.from_json(opts)
96- else
97- klass.from_json(" {}" )
98- end
99- end
100- end
75+ if values.empty?
76+ abort option_parser
77+ end
10178
102- if ttl = currency_rates_ttl
103- config.currency_rates_ttl = Time ::Span .parse(ttl)
104- end
79+ begin
80+ config = FXCalculator ::Utils .config_from_opts(
81+ config_path,
82+ rate_store_name,
83+ rate_store_opts,
84+ rate_provider_name,
85+ rate_provider_opts,
86+ currency_code,
87+ currency_rates_ttl,
88+ )
10589
106- unless config.rate_provider?
107- raise ArgumentError .new( " Currency rate provider is required " )
90+ if clear_currency_rates_cache
91+ config.rate_store.clear
10892 end
10993
11094 Money .configure do |context |
@@ -120,6 +104,5 @@ begin
120104 renderer = FXCalculator ::Renderer .new
121105 renderer.render(moneys)
122106rescue ex
123- STDERR .puts " ERROR: #{ ex.message } " .colorize(:red )
124- exit(1 )
107+ abort " ERROR: #{ ex.message } " .colorize(:red )
125108end
0 commit comments