idcfcloud 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -0
  3. data/.gitignore +5 -3
  4. data/.rubocop.yml +16 -1
  5. data/README.md +61 -6
  6. data/exe/idcfcloud +9 -1
  7. data/idcfcloud.gemspec +9 -7
  8. data/lib/idcf/cli/conf/conf.yml +39 -5
  9. data/lib/idcf/cli/conf/const.rb +42 -17
  10. data/lib/idcf/cli/controller/base.rb +130 -60
  11. data/lib/idcf/cli/controller/extend/command.rb +18 -16
  12. data/lib/idcf/cli/controller/extend/init.rb +2 -0
  13. data/lib/idcf/cli/controller/extend/override.rb +1 -8
  14. data/lib/idcf/cli/controller/extend/search_module.rb +14 -0
  15. data/lib/idcf/cli/controller/ilb.rb +5 -34
  16. data/lib/idcf/cli/controller/include/command.rb +7 -80
  17. data/lib/idcf/cli/controller/include/init.rb +2 -4
  18. data/lib/idcf/cli/controller/include/util.rb +20 -63
  19. data/lib/idcf/cli/controller/include/validate.rb +7 -1
  20. data/lib/idcf/cli/controller/your.rb +0 -33
  21. data/lib/idcf/cli/error/cli_error.rb +7 -5
  22. data/lib/idcf/cli/extend/configure.rb +38 -33
  23. data/lib/idcf/cli/extend/update.rb +139 -54
  24. data/lib/idcf/cli/extend/update_file.rb +113 -18
  25. data/lib/idcf/cli/gem_ext/thor/init_util.rb +20 -7
  26. data/lib/idcf/cli/index.rb +21 -11
  27. data/lib/idcf/cli/lib/convert/formatter/base.rb +1 -1
  28. data/lib/idcf/cli/lib/convert/formatter/xml_format.rb +1 -1
  29. data/lib/idcf/cli/lib/convert/helper.rb +43 -7
  30. data/lib/idcf/cli/lib/util/ini_conf.rb +30 -8
  31. data/lib/idcf/cli/lib/util/yml_conf.rb +13 -2
  32. data/lib/idcf/cli/service/base.rb +121 -21
  33. data/lib/idcf/cli/service/ilb/add_server_for_protocol.rb +8 -3
  34. data/lib/idcf/cli/service/ilb/base_server_for_protocol.rb +21 -35
  35. data/lib/idcf/cli/service/ilb/delete_server_for_protocol.rb +9 -4
  36. data/lib/idcf/cli/validate/define/base.rb +3 -1
  37. data/lib/idcf/cli/validate/define/ilb/base.rb +0 -5
  38. data/lib/idcf/cli/version.rb +1 -1
  39. metadata +62 -23
  40. data/lib/idcf/cli/conf/service/.gitkeep +0 -0
  41. data/lib/idcf/cli/controller/include/override.rb +0 -30
  42. data/lib/idcf/cli/controller/include/sdk.rb +0 -94
@@ -3,6 +3,7 @@ require 'active_support/core_ext'
3
3
  require 'idcf/cli/conf/const'
4
4
  require 'idcf/cli/lib/util/template'
5
5
  require_relative 'update_file'
6
+ require 'idcf/cli/lib/util/input'
6
7
  module Idcf
7
8
  module Cli
8
9
  module Extend
@@ -15,83 +16,150 @@ module Idcf
15
16
  COMP_N = 'complement'.freeze
16
17
 
17
18
  def do_update(_o)
18
- clients = make_clients
19
- writable_setting_file clients.keys
20
- infos = {}
21
-
22
- clients.each do |s_name, client|
23
- info = make_module_info(client)
24
- output_yaml(s_name, info)
25
- infos[s_name] = info
26
- end
19
+ s_schemas = schema_data_acquisition
20
+ output_schema(s_schemas)
27
21
  output_complement
28
22
  end
29
23
 
30
- def make_clients
31
- {}.tap do |result|
32
- self.class.subcommand_classes.each do |k, v|
33
- next unless v.ancestors.include?(Idcf::Cli::Controller::Base)
34
- client = v.make_blank_client
35
- result[k] = client unless client.nil?
24
+ def output_schema(s_schemas)
25
+ s_schemas.each do |service, infos|
26
+ infos.each do |version, regions|
27
+ next if regions.nil? || regions.empty?
28
+ regions.each do |region, schema|
29
+ path = make_schema_file_path(service, version, region)
30
+ output_complement_file(JSON.pretty_generate(schema), path)
31
+ end
32
+ output_default_schema(service, version, regions)
36
33
  end
37
34
  end
38
35
  end
39
36
 
40
- def make_module_info(client)
41
- {}.tap do |result|
42
- module_methods = []
43
- client.class.included_modules.each do |m|
44
- next unless m.to_s =~ /\AIdcf::.*::ClientExtensions/
45
- module_methods.concat(module_instance_methods(m))
46
- end
47
- module_methods.each do |method|
48
- result[method] = make_method_info(client, method)
49
- end
37
+ def output_default_schema(service, version, regions)
38
+ return nil if regions.keys.include?('default')
39
+ latist = latest_schema(regions.values)
40
+ path = make_schema_file_path(service, version, 'default')
41
+ output_complement_file(JSON.pretty_generate(latist), path)
42
+ end
43
+
44
+ # get latest schema
45
+ #
46
+ # @param schemas [Array]
47
+ # @return Hash or nil
48
+ def latest_schema(schemas)
49
+ result = nil
50
+ v_str = '$version'
51
+ schemas.each do |data|
52
+ result ||= data
53
+ v1 = Gem::Version.create(data[v_str])
54
+ v2 = Gem::Version.create(result[v_str])
55
+ next unless v1 > v2
56
+ result = data
57
+ end
58
+ result
59
+ end
60
+
61
+ def output_complement
62
+ view = Idcf::Cli::Lib::Util::Template.new
63
+ view.set('variables', make_script_variables)
64
+ paths = {}
65
+ Idcf::Cli::Conf::Const::COMP_PATHS.each do |k, _v|
66
+ str = view.fetch("#{k}/#{COMP_N}.erb")
67
+ path = "#{Idcf::Cli::Conf::Const::OUT_DIR}/#{COMP_N}.#{k}"
68
+ paths[k] = File.expand_path(path)
69
+ output_complement_file(str, paths[k])
70
+ end
71
+
72
+ output_notification(paths)
73
+ end
74
+
75
+ # output notification
76
+ #
77
+ # @param paths [Hash]
78
+ def output_notification(paths)
79
+ script = ENV['SHELL'].split('/').pop.to_sym
80
+ return output_manual_writing(paths) if paths[script].nil?
81
+ startup_script_qa(paths, script)
82
+ end
83
+
84
+ def startup_script_qa(paths, script)
85
+ list = %w(y N)
86
+ puts "Do you want to edit the startup script?(#{list.join('/')})"
87
+ puts "[#{startup_script_path(script)}]"
88
+ ans = Idcf::Cli::Lib::Util::Input.qa_answer_input(list)
89
+ if ans == 'y'
90
+ output_startup_script(script, paths)
91
+ else
92
+ output_manual_writing(paths)
50
93
  end
51
94
  end
52
95
 
53
- def module_instance_methods(mc)
54
- [].tap do |result|
55
- base_name = mc.to_s.underscore.split('/').pop
56
- mc.instance_methods.each do |method|
57
- result << method unless Regexp.new("\\A#{base_name}s?\\Z") =~ method.to_s
96
+ # startup script path
97
+ #
98
+ # @param script [String]
99
+ # @return String
100
+ def startup_script_path(script)
101
+ File.expand_path(Idcf::Cli::Conf::Const::COMP_PATHS[script])
102
+ end
103
+
104
+ # output start script
105
+ #
106
+ # @param paths [Hash]
107
+ def output_startup_script(script, paths)
108
+ s_script_path = startup_script_path(script)
109
+ write_str = "source #{paths[script]}"
110
+
111
+ if write_path?(s_script_path, write_str)
112
+ File.open(s_script_path, 'a') do |f|
113
+ f.puts write_str, ''
58
114
  end
59
115
  end
116
+
117
+ puts 'Run the following command:', write_str
60
118
  end
61
119
 
62
- def make_method_info(client, method_sym)
63
- params = []
64
- client.method(method_sym).parameters.each do |param|
65
- next if param[0] == :block
66
- params << {
67
- name: param[1].to_s,
68
- arg_type: param[0].to_s
69
- }
120
+ # write path?
121
+ #
122
+ # @param path [String] setting path
123
+ # @param check_str
124
+ # @return Boolean
125
+ # @raise
126
+ def write_path?(path, check_str)
127
+ if File.exist?(path)
128
+ Idcf::Cli::Lib::Util::CliFile.writable(path)
129
+ return !write_line_str_exist?(path, check_str)
70
130
  end
71
131
 
72
- { desc: '', params: params }
132
+ Idcf::Cli::Lib::Util::CliFile.writable(File.expand_path('..', path))
133
+ true
73
134
  end
74
135
 
75
- def output_complement
76
- view = Idcf::Cli::Lib::Util::Template.new
77
- view.set('variables', make_script_variables)
78
- paths = []
79
- %w(bash zsh).each do |e|
80
- str = view.fetch("#{e}/#{COMP_N}.erb")
81
- path = "#{Idcf::Cli::Conf::Const::OUT_DIR}/#{COMP_N}.#{e}"
82
- paths << File.expand_path(path)
83
- output_complement_file(str, paths.last)
136
+ # write line str exist?
137
+ #
138
+ # @param path [String] setting path
139
+ # @param check_str
140
+ # @return Boolean
141
+ # @raise
142
+ def write_line_str_exist?(path, check_str)
143
+ File.open(path, 'r') do |f|
144
+ f.each_line do |line|
145
+ return true if line.strip == check_str
146
+ end
84
147
  end
85
- output_notification(paths)
148
+ false
86
149
  end
87
150
 
88
- def output_notification(paths)
89
- notification = Idcf::Cli::Conf::Const::COMP_NOTIFICATION
90
- puts format(notification, *paths)
151
+ # manual write
152
+ #
153
+ # @param paths [Hash]
154
+ def output_manual_writing(paths)
155
+ puts 'please write in'
156
+ Idcf::Cli::Conf::Const::COMP_PATHS.each do |k, v|
157
+ puts v, paths[k]
158
+ end
91
159
  end
92
160
 
93
161
  def make_script_variables
94
- self.class.init
162
+ self.class.init({})
95
163
  make_flat_variables(self.class.subcommand_structure)
96
164
  end
97
165
 
@@ -102,7 +170,7 @@ module Idcf
102
170
  # @return Hash
103
171
  def make_flat_variables(list, names = [])
104
172
  {}.tap do |result|
105
- name = names.empty? ? 'top' : names.join('_')
173
+ name = names.empty? ? 'top' : names.join('_')
106
174
  result[name] = list.keys
107
175
  list.each do |k, v|
108
176
  next unless v.class == Hash
@@ -112,6 +180,23 @@ module Idcf
112
180
  end
113
181
  end
114
182
  end
183
+
184
+ def extract_commands(thor_class, commands)
185
+ thor_class.commands.keys.each do |k_c|
186
+ commands << k_c if commands.index(k_c).nil?
187
+ end
188
+
189
+ commands.sort
190
+ end
191
+
192
+ def extract_command_infos(t_class, s_name, infos)
193
+ commands = []
194
+ return extract_commands(t_class, commands) if infos[s_name].nil?
195
+ infos[s_name].keys.each do |name|
196
+ commands << name.to_s
197
+ end
198
+ extract_commands(t_class, commands)
199
+ end
115
200
  end
116
201
  end
117
202
  end
@@ -1,24 +1,126 @@
1
1
  require 'fileutils'
2
2
  require 'idcf/cli/conf/const'
3
+ require 'open_uri_redirections'
3
4
 
4
5
  module Idcf
5
6
  module Cli
6
7
  module Extend
7
8
  # update file
8
9
  module UpdateFile
10
+ BROKEN_UPDATE_FILE = 'The update file is damaged. '\
11
+ 'Please consult with the administrator.'.freeze
12
+
9
13
  protected
10
14
 
11
- def writable_setting_file(services)
12
- b_dir = File.expand_path(Idcf::Cli::Conf::Const::CMD_FILE_DIR)
13
- FileUtils.mkdir_p(b_dir) unless Dir.exist?(b_dir)
14
- writable?(File.dirname(b_dir))
15
+ def schema_data_acquisition
16
+ {}.tap do |result|
17
+ self.class.subcommand_classes.each do |s_name, cls|
18
+ result[s_name] = service_schema_data_acquisition(cls)
19
+ end
20
+ end
21
+ end
15
22
 
16
- services.each do |v|
17
- path = "#{b_dir}/#{v}.#{Idcf::Cli::Conf::Const::CMD_FILE_EXT}"
18
- writable?(File.dirname(path)) if File.exist?(path)
23
+ def service_schema_data_acquisition(cls)
24
+ {}.tap do |result|
25
+ schema_file_paths(cls).each do |version, infos|
26
+ result[version] = {}
27
+ infos.each do |region, path|
28
+ j = download_schema_file(path)
29
+ update_data_check(j)
30
+ result[version][region] = j
31
+ end
32
+ end
19
33
  end
20
34
  end
21
35
 
36
+ def download_schema_file(path)
37
+ d = file_load(path)
38
+ JSON.parse(d)
39
+ rescue
40
+ Idcf::Cli::Lib::Util::CliLogger.info("json format error:#{path}")
41
+ raise Idcf::Cli::Error::CliError, BROKEN_UPDATE_FILE
42
+ end
43
+
44
+ def update_data_check(j)
45
+ check_json_schema(j)
46
+ check_api_version_format(j['$version'])
47
+ end
48
+
49
+ def check_json_schema(j)
50
+ analyst = Idcf::JsonHyperSchema::Analyst.new
51
+ analyst.schema_links(j)
52
+ rescue => e
53
+ Idcf::Cli::Lib::Util::CliLogger.info('json-schema format error')
54
+ log_msg = "#{BROKEN_UPDATE_FILE}:#{e.message}"
55
+ Idcf::Cli::Lib::Util::CliLogger.error(log_msg)
56
+ raise Idcf::Cli::Error::CliError, BROKEN_UPDATE_FILE
57
+ end
58
+
59
+ def file_load(path)
60
+ open(path).read
61
+ rescue
62
+ nil
63
+ end
64
+
65
+ def schema_file_paths(cls)
66
+ {}.tap do |result|
67
+ Idcf::Cli::Lib::Configure.get_code_conf(cls.service_name).each do |k, v|
68
+ regions = {}
69
+ v['region'].each do |region, info|
70
+ regions[region] = create_schema_url(info['schema'])
71
+ end
72
+
73
+ result[k] = regions
74
+ end
75
+ end
76
+ end
77
+
78
+ def create_schema_url(path)
79
+ href_regexp = Idcf::Cli::Conf::Const::FULL_HREF_REGEXP
80
+ return path if path =~ href_regexp
81
+ path = File.expand_path("../#{path}", Idcf::Cli::Conf::Const::BASE_PATH)
82
+ return path if File.exist?(path)
83
+ File.expand_path(path)
84
+ end
85
+
86
+ # check api version format
87
+ #
88
+ # @param v_str [String] version string
89
+ # @return Boolean
90
+ # @raise
91
+ def check_api_version_format(v_str)
92
+ msg = 'Please inform an administrator.'
93
+ raise Idcf::Cli::Error::CliError, msg unless v_str =~ /\A\d+\.\d+\.\d+\Z/
94
+ true
95
+ end
96
+
97
+ # make schema file path
98
+ #
99
+ # @param service [String]
100
+ # @param version [String]
101
+ # @param region [String]
102
+ # @return String
103
+ # @raise At a writing in right error
104
+ def make_schema_file_path(service, version, region)
105
+ b_dir = schema_file_output_base_path
106
+ ext = Idcf::Cli::Conf::Const::CMD_FILE_EXT
107
+ path = "#{b_dir}/#{service}_#{version}_#{region}.#{ext}"
108
+ cli_file = Idcf::Cli::Lib::Util::CliFile
109
+ if File.exist?(path)
110
+ cli_file.writable(path)
111
+ else
112
+ cli_file.writable(File.dirname(path))
113
+ end
114
+ path
115
+ end
116
+
117
+ def schema_file_output_base_path
118
+ result = File.expand_path(Idcf::Cli::Conf::Const::CMD_FILE_DIR)
119
+ FileUtils.mkdir_p(result) unless Dir.exist?(result)
120
+ Idcf::Cli::Lib::Util::CliFile.writable(File.dirname(result))
121
+ result
122
+ end
123
+
22
124
  def output_yaml(s_name, data)
23
125
  dir = Idcf::Cli::Conf::Const::CMD_FILE_DIR
24
126
  ext = Idcf::Cli::Conf::Const::CMD_FILE_EXT
@@ -28,25 +130,18 @@ module Idcf
28
130
 
29
131
  def output_complement_file(str, outpath)
30
132
  dir_path = File.dirname(outpath)
31
- unless Dir.exist?(dir_path)
32
- writable?(File.dirname(dir_path))
33
- Dir.mkdir(dir_path)
34
- end
35
- writable?(dir_path)
133
+ cli_file = Idcf::Cli::Lib::Util::CliFile
134
+ cli_file.mkdir(dir_path)
135
+ cli_file.writable(dir_path)
36
136
 
37
137
  write_file(outpath, str)
38
138
  end
39
139
 
40
140
  def write_file(path, str)
41
- File.open(path, 'w') do |f|
141
+ File.open(path, 'w', 0o755) do |f|
42
142
  f.puts(str)
43
143
  end
44
144
  end
45
-
46
- def writable?(path)
47
- msg = "Permission error (#{path})"
48
- raise Idcf::Cli::CliError, msg unless File.writable?(path)
49
- end
50
145
  end
51
146
  end
52
147
  end
@@ -1,3 +1,6 @@
1
+ require 'idcf/cli/lib/util/cli_logger'
2
+ require 'idcf/cli/lib/util/name'
3
+
1
4
  # Thor extention
2
5
  class Thor
3
6
  class << self
@@ -15,21 +18,22 @@ class Thor
15
18
  #
16
19
  # @return String
17
20
  def description
18
- raise Idcf::Cli::CliError, 'Required Override'
21
+ raise Idcf::Cli::Error::CliError, 'Required Override'
19
22
  end
20
23
 
21
24
  # register sub command
22
25
  #
23
26
  # @param dir [String] underlayer path
24
27
  # @param parent_dir_path [Stirng]
28
+ # @param arg [Hash] options
25
29
  # @return nil
26
- def sub_command_regist(under_path, parent_dir_path = '')
27
- b_path = module_path
30
+ def sub_command_regist(under_path, parent_dir_path = '', arg = {})
31
+ b_path = Idcf::Cli::Lib::Util::Name.namespace(name).underscore
28
32
  Dir.glob(parent_dir_path + "/#{under_path}/*.rb").each do |f|
29
33
  file_name = File.basename(f, '.*')
30
34
  next if file_name == 'base'
31
35
 
32
- command_regist file_name, "#{b_path}/#{under_path}/#{file_name}"
36
+ command_regist file_name, "#{b_path}/#{under_path}/#{file_name}", arg
33
37
  end
34
38
  end
35
39
 
@@ -37,11 +41,12 @@ class Thor
37
41
  #
38
42
  # @param command [String]
39
43
  # @param require_path [String]
40
- def command_regist(command, require_path)
44
+ # @param o [Hash] options
45
+ def command_regist(command, require_path, arg)
41
46
  require require_path
42
47
 
43
48
  class_const = require_path.classify.constantize
44
- class_const.init
49
+ class_const.init(arg)
45
50
  register class_const,
46
51
  command,
47
52
  "#{command_help_string(command)} [OPTION]",
@@ -78,11 +83,19 @@ class Thor
78
83
  end
79
84
  end
80
85
 
86
+ def error_exit(e)
87
+ STDERR.puts e.message
88
+ Idcf::Cli::Lib::Util::CliLogger.error(e.message)
89
+ Idcf::Cli::Lib::Util::CliLogger.cleaning(options)
90
+ STDERR.puts "log path: #{Idcf::Cli::Lib::Util::CliLogger.current_path}"
91
+ exit 1
92
+ end
93
+
81
94
  protected
82
95
 
83
96
  def subcommand_class_structure(result)
84
97
  subcommand_classes.each do |k, v|
85
- commands = v.subcommand_structure
98
+ commands = v.subcommand_structure
86
99
  result[k.to_s] = nil
87
100
  result[k.to_s] = commands unless commands.empty?
88
101
  end
@@ -1,3 +1,4 @@
1
+ require 'logger'
1
2
  require 'thor'
2
3
  require 'active_support'
3
4
  require 'active_support/core_ext'
@@ -7,21 +8,29 @@ require 'idcf/cli/error/cli_error'
7
8
  require_relative './validate/custom/init'
8
9
  require_relative './gem_ext/thor/init_util'
9
10
  require_relative './extend/init'
11
+ require 'idcf/cli/error/init'
12
+ require 'idcf/cli/lib/util/cli_file'
13
+ require 'idcf/cli/lib/util/cli_logger'
10
14
 
11
15
  module Idcf
12
16
  module Cli
13
17
  # Index
14
18
  class Index < Thor
15
- @variables = nil
19
+ @variables = nil
16
20
  # command alias [alias] => [command]
17
21
  COMMAND_MAPS = {}.freeze
18
22
 
19
23
  include Idcf::Cli::Extend::Init
20
24
 
21
25
  class << self
22
- def init
26
+ # init
27
+ #
28
+ # @param arg [Hash] options
29
+ def init(arg)
23
30
  map COMMAND_MAPS
24
- sub_command_regist('controller', File.dirname(__FILE__))
31
+ sub_command_regist('controller', File.dirname(__FILE__), arg)
32
+ rescue => e
33
+ error_exit(Idcf::Cli::Error::CliError.new(e.message))
25
34
  end
26
35
  end
27
36
 
@@ -31,14 +40,14 @@ module Idcf
31
40
  end
32
41
 
33
42
  desc 'init', 'initialize'
34
- options global: true,
43
+ options global: true,
35
44
  profile: 'default'
36
45
 
37
46
  def init
38
47
  configure
39
48
  update
40
49
  rescue => e
41
- puts e.message
50
+ self.class.error_exit(e)
42
51
  end
43
52
 
44
53
  desc 'update', 'list update'
@@ -46,25 +55,26 @@ module Idcf
46
55
  def update
47
56
  do_update(options)
48
57
  rescue => e
49
- puts e.message
58
+ self.class.error_exit(e)
50
59
  end
51
60
 
52
61
  desc 'configure', 'create configure'
53
- options global: true,
62
+ options global: true,
54
63
  profile: 'default'
55
64
 
56
65
  def configure
57
- do_configure(options)
66
+ init_f = ARGV[0] == 'init'
67
+ do_configure(options, init_f)
58
68
  rescue => e
59
- puts e.message
69
+ self.class.error_exit(e)
60
70
  end
61
71
 
62
72
  desc 'version', 'version string'
63
73
 
64
74
  def version
65
- puts "idcfcloud version #{Idcf::Cli::VERSION}"
75
+ puts Idcf::Cli::Conf::Const::VERSION_STR
66
76
  rescue => e
67
- puts e.message
77
+ self.class.error_exit(e)
68
78
  end
69
79
  end
70
80
  end
@@ -11,7 +11,7 @@ module Idcf
11
11
  # @param _err_f [Boolean]
12
12
  # @return String
13
13
  def format(_data, _err_f)
14
- raise Idcf::Cli::CliError, 'override'
14
+ raise Idcf::Cli::Error::CliError, 'override'
15
15
  end
16
16
  end
17
17
  end
@@ -11,7 +11,7 @@ module Idcf
11
11
  # xml formatter
12
12
  class XmlFormat < Base
13
13
  def format(data, _err_f)
14
- data.to_xml
14
+ data.to_xml(dasherize: false)
15
15
  end
16
16
  end
17
17
  end
@@ -1,8 +1,7 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require 'idcf/cli/lib/util/name'
1
4
  require 'active_support/core_ext/string/inflections'
2
- require 'idcf/cli/lib/convert/formatter/xml_format'
3
- require 'idcf/cli/lib/convert/formatter/json_format'
4
- require 'idcf/cli/lib/convert/formatter/csv_format'
5
- require 'idcf/cli/lib/convert/formatter/table_format'
6
5
 
7
6
  module Idcf
8
7
  module Cli
@@ -10,6 +9,8 @@ module Idcf
10
9
  module Convert
11
10
  # format helper
12
11
  class Helper
12
+ FILTER_OPTION = [:json_path, :fields].freeze
13
+
13
14
  # data convert
14
15
  #
15
16
  # @param data [Hash]
@@ -17,9 +18,44 @@ module Idcf
17
18
  # @param f [String] format
18
19
  # @return String
19
20
  def format(data, err_f, f)
20
- base_name = 'Idcf::Cli::Lib::Convert::Formatter'
21
- cl = "#{base_name}::#{f.capitalize}Format"
22
- cl.constantize.new.format(data, err_f)
21
+ cls_load("Formatter::#{f.classify}Format").new.format(data, err_f)
22
+ end
23
+
24
+ # is filter target
25
+ # @param o [Hash]
26
+ # @return Boolean
27
+ def filter_target?(o)
28
+ FILTER_OPTION.each do |k|
29
+ return true if !o[k].nil? && !o[k].empty?
30
+ end
31
+ false
32
+ end
33
+
34
+ # data convert
35
+ #
36
+ # @param data [Hash]
37
+ # @param o [Hash] format
38
+ # @param table_flag [Boolean]
39
+ # @return Hash
40
+ def filter(data, o, table_flag)
41
+ return data unless [Hash, Array].include?(data.class)
42
+ result = data.deep_dup
43
+ FILTER_OPTION.each do |k|
44
+ next if o[k].nil? || o[k].empty?
45
+ fo = {
46
+ table_flag: table_flag
47
+ }
48
+ result = cls_load("Filter::#{k.to_s.classify}Filter").new(fo).filter(result, o[k])
49
+ end
50
+ result
51
+ end
52
+
53
+ protected
54
+
55
+ def cls_load(path)
56
+ cls = "#{Idcf::Cli::Lib::Util::Name.namespace(self.class.to_s)}::#{path}"
57
+ require cls.underscore
58
+ cls.constantize
23
59
  end
24
60
  end
25
61
  end