Class: HMap::Command::Writer

Inherits:
HMap::Command show all
Defined in:
lib/hmap/command/hmap_writer.rb

Overview

hmap file gen cmd

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Writer

Returns a new instance of Writer.



20
21
22
23
24
25
# File 'lib/hmap/command/hmap_writer.rb', line 20

def initialize(argv)
  super
  @json_path = argv.option('json-path') || ''
  output_path = argv.option('output-path')
  @output_path = output_path.nil? ? Pathname('.') : Pathname(output_path)
end

Class Method Details

.optionsObject

help



34
35
36
37
38
39
# File 'lib/hmap/command/hmap_writer.rb', line 34

def self.options
  [
    ['--json-path=/project/dir/json', 'The path to the hmap json data.'],
    ['--output-path=/project/dir/hmap file', 'The path json data to the hmap file.']
  ].concat(super)
end

Instance Method Details

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hmap/command/hmap_writer.rb', line 41

def run
  UserInterface.puts "\n[hmapfile-from-json] start"
  if File.exist?(@json_path)
    require 'json'
    json_file = File.read(@json_path)
    json = JSON.parse(json_file)
    path = @output_path
    path = path.join("#{File.basename(@json_path, '.*')}.hmap") if path.directory?
    HMapSaver.new_from_buckets(json).write_to(path)
    UserInterface.puts "[hmapfile-from-json] output path #{path}".green
  else
    unless File.exist?(@json_path)
      UserInterface.puts "\n[hmapfile-from-json] Error json path: #{@json_path} no such file!".red
    end
    unless File.exist?(@output_path)
      UserInterface.puts "\n[hmapfile-from-json] Error output path: #{@output_path} no such file!".red
    end
  end
  UserInterface.puts "\n[hmapfile-from-json] finish"
rescue Object => exception
    UserInterface.puts "#{exception}".red
end

#validate!Object



27
28
29
30
31
# File 'lib/hmap/command/hmap_writer.rb', line 27

def validate!
  super
  help! 'error: no input json files which to use with the `--json-path` option.' if @json_path.nil?
  help! 'error: no output path which to use the `--output-path`' if @output_path.nil?
end