convert-serialization 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env ruby
  2. # frozen_string_literal: true
  3. # sharable_constant_value: literal
  4. begin
  5. require 'optparse'
  6. input_format = nil
  7. output_format = nil
  8. input_file = nil
  9. output_file = nil
  10. OptionParser.new do |opts|
  11. opts.banner = 'Usage: convert-serialization [options] input_file output_file'
  12. opts.on('-s', '--inf INPUT_FORMAT', 'Specifies the input format (msgpack / json / yaml / toml).') do |format|
  13. input_format = format
  14. end
  15. opts.on('-d', '--outf OUTPUT_FORMAT', 'Specifies the output format (msgpack / json / yaml / toml).') do |format|
  16. output_format = format
  17. end
  18. opts.on('-i', '--in INPUT_FILE', 'Specifies the input file.') do |file|
  19. input_file = file
  20. end
  21. opts.on('-o', '--out OUTPUT_FILE', 'Specifies the output file.') do |file|
  22. output_file = file
  23. end
  24. end.parse!
  25. raise ArgumentError, 'Input format missing.' unless input_format
  26. raise ArgumentError, 'Output format missing.' unless output_format
  27. raise ArgumentError, 'Input file is missing.' unless input_file
  28. raise ArgumentError, 'Output file is missing.' unless output_file
  29. input_format.strip!
  30. input_format.downcase!
  31. output_format.strip!
  32. output_format.downcase!
  33. input_file.strip!
  34. input_file.downcase!
  35. output_file.strip!
  36. output_file.downcase!
  37. in_handler = case input_format
  38. when 'msgpack'
  39. lambda do |filename|
  40. require 'msgpack'
  41. file = File.new filename, 'rb'
  42. result = MessagePack.unpack file
  43. file.close
  44. result
  45. end
  46. when 'json'
  47. lambda do |filename|
  48. require 'psych'
  49. Psych.load_file filename
  50. end
  51. when 'yaml'
  52. lambda do |filename|
  53. require 'psych'
  54. Psych.load_file filename
  55. end
  56. when 'toml'
  57. lambda do |filename|
  58. require 'toml-rb'
  59. TomlRB.load_file filename
  60. end
  61. else
  62. raise ArgumentError, 'Unknown input format.'
  63. end
  64. out_handler = case output_format
  65. when 'msgpack'
  66. lambda do |object, filename|
  67. require 'msgpack'
  68. file = File.new filename, 'wb'
  69. MessagePack.pack object, file
  70. file.close
  71. end
  72. when 'json'
  73. lambda do |object, filename|
  74. require 'json'
  75. file = File.new filename, 'w'
  76. JSON.dump object, file
  77. file.close
  78. end
  79. when 'yaml'
  80. lambda do |object, filename|
  81. require 'psych'
  82. file = File.new filename, 'w'
  83. Psych.dump object, file
  84. file.close
  85. end
  86. when 'toml'
  87. lambda do |object, filename|
  88. raise 'The primary toml block must consist of key-value pairs.' unless object.is_a? Hash
  89. require 'toml-rb'
  90. file = File.new filename, 'w'
  91. file.write TomlRB.dump(object)
  92. file.close
  93. end
  94. else
  95. raise ArgumentError, 'Unknown output format.'
  96. end
  97. input_object = in_handler.call(input_file)
  98. out_handler.call(input_object, output_file)
  99. warn 'Conversion successful!'
  100. rescue StandardError => e
  101. warn 'Conversion failed!'
  102. warn "Error: #{e.message}"
  103. end