diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7f4f5e9 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gemspec diff --git a/README.md b/README.md index 4d615df..adbc6bf 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,84 @@ # proto-convert [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://github.com/iamAzeem/proto-convert/blob/master/LICENSE) +[![RubyGems Downloads](https://img.shields.io/gem/dt/proto-convert?color=blue&style=flat-square&label=Downloads)](https://rubygems.org/gems/proto-convert) A command line tool to convert protobuf messages from binary to JSON and vice versa. +## Installation + +Please make sure that the Protocol Buffers Compiler `protoc` is installed. + +### RubyGems + +``` +$ gem install proto-convert +``` + +### Bundler + +Add following line to your Gemfile: +```ruby +gem "proto-convert" +``` + +And then execute: +``` +$ bundle +``` + +## Usage + +```shell +Usage: proto-convert -m [mode] -p [proto] -t [msgtype] -i [input] -o [output] + + -m, --mode [MODE] conversion mode ["binary2json", "b2j", "json2binary", "j2b"] + -p, --proto [FILENAME] protobuf schema (.proto) + -t, --msgtype [TYPE] fully-qualified message type + -i, --input [FILENAME] source file (JSON/binary) + -o, --output [FILENAME] destination file (binary/JSON) + -v, --version prints version information + -h, --help prints help +``` + +### Test Run + +Consider this simple .proto file ([`test.proto`](test/test.proto)): + +``` +syntax = "proto3"; + +package test; + +message Message { + int32 id = 1; + string body = 2; +} +``` + +These test files (`test.proto`, `test.json`, and `test.bin`) are located in [`test`](test) folder. + +**JSON to Binary Conversion** +``` +$ proto-convert -m json2binary -p test.proto -t test.Message -i test.json -o test.bin +>> [J] test.json (24 bytes) +<< [B] test.bin (8 bytes) +``` + +**Binary to JSON Conversion** +``` +$ proto-convert -m binary2json -p test.proto -t test.Message -i test.bin -o test.json +>> [B] test.bin (8 bytes) +<< [J] test.json (24 bytes) +``` + +and, + +``` +$ cat test.json +{"id":123,"body":"test"} +``` + ## Contact Email: azeem.sajid@gmail.com diff --git a/proto-convert b/bin/proto-convert similarity index 80% rename from proto-convert rename to bin/proto-convert index 8010a60..e32a2a0 100755 --- a/proto-convert +++ b/bin/proto-convert @@ -1,6 +1,28 @@ #!/usr/bin/env ruby # frozen_string_literal: true +# MIT License +# +# Copyright (c) 2020 Azeem Sajid +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + # proto-convert # A command-line tool to convert protobuf messages from binary to JSON and vice versa # GitHub: https://github.com/iamAzeem/proto-convert @@ -10,7 +32,7 @@ require 'optparse' require 'English' -VERSION = "#{$PROGRAM_NAME} 1.0.0" +VERSION = "#{$PROGRAM_NAME} 0.1.0" AUTHOR_NAME = 'Azeem Sajid' AUTHOR_EMAIL = 'azeem.sajid@gmail.com' AUTHOR_INFO = "Developed by #{AUTHOR_NAME} <#{AUTHOR_EMAIL}>" @@ -20,8 +42,8 @@ def compile_proto(filename) file_path = File.expand_path(filename) file_dir = File.dirname(file_path) - `protoc --ruby_out=#{file_dir} --proto_path=#{file_dir} #{file_path}` - raise StandardError, "Invalid schema! [#{filename}] Resolve the error(s)." unless $CHILD_STATUS.success? + `protoc --ruby_out.=#{file_dir} --proto_path=#{file_dir} #{file_path}` + raise StandardError, "Invalid schema! [#{filename}] Resolve error(s)." unless $CHILD_STATUS.success? compiled_proto = file_dir + '/' + File.basename(file_path, '.proto') + '_pb.rb' raise StandardError, "Compiled schema not found! [#{compiled_proto}]" unless File.file?(compiled_proto) diff --git a/proto-convert.gemspec b/proto-convert.gemspec new file mode 100644 index 0000000..1721d10 --- /dev/null +++ b/proto-convert.gemspec @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +lib = File.expand_path('../lib', __dir__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) + +Gem::Specification.new do |spec| + spec.name = 'proto-convert' + spec.version = '0.1.0' + spec.date = '2020-07-07' + spec.authors = ['Azeem Sajid'] + spec.email = ['azeem.sajid@gmail.com'] + + spec.summary = 'Protobuf Message Converter [Binary <-> JSON]' + spec.description = 'A command line tool to convert protobuf messages from binary to JSON and vice versa' + spec.homepage = 'https://github.com/iamAzeem/proto-convert' + spec.license = 'MIT' + + spec.executables = ['proto-convert'] + + spec.add_development_dependency 'bundler', '~> 1.14' + spec.add_runtime_dependency 'google-protobuf', '~> 3.12', '>= 3.12.2' +end