Skip to content
This repository has been archived by the owner on May 2, 2021. It is now read-only.

Commit

Permalink
Add -v flag
Browse files Browse the repository at this point in the history
  • Loading branch information
omarroth committed Oct 8, 2019
1 parent ce002cb commit 496a5f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ targets:
protodec:
main: src/protodec.cr

crystal: 0.30.1
crystal: 0.31.1

license: GPLv3
33 changes: 25 additions & 8 deletions src/protodec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ require "json"
require "option_parser"
require "uri"

CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }}
CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }}
CURRENT_VERSION = {{ "#{`git describe --tags --abbrev=0`.strip}" }}

SOFTWARE = {
"name" => "protodec",
"version" => "#{CURRENT_VERSION}-#{CURRENT_COMMIT}",
"branch" => "#{CURRENT_BRANCH}",
}

struct VarLong
def self.from_io(io : IO, format = IO::ByteFormat::NetworkEndian) : Int64
result = 0_i64
Expand Down Expand Up @@ -146,7 +156,7 @@ struct ProtoBuf::Any
!{0x09, 0x0a, 0x0d}.includes?(codepoint)
}
begin
value = from_io(IO::Memory.new(Base64.decode(URI.unescape(URI.unescape(value))))).raw
value = from_io(IO::Memory.new(Base64.decode(URI.decode_www_form(URI.decode_www_form(value))))).raw
key = "#{field}:#{index}:base64"
rescue ex
key = "#{field}:#{index}:string"
Expand Down Expand Up @@ -204,15 +214,15 @@ struct ProtoBuf::Any

case type
when "varint"
VarLong.to_io(io, value.as_i64)
VarLong.to_io(io, value.raw.as(Number).to_i64!)
when "int32"
value.as_i64.to_i32.to_io(io, IO::ByteFormat::LittleEndian)
value.raw.as(Number).to_i32!.to_io(io, IO::ByteFormat::LittleEndian)
when "float32"
value.as_f32.to_f32.to_io(io, IO::ByteFormat::LittleEndian)
value.raw.as(Number).to_f32!.to_io(io, IO::ByteFormat::LittleEndian)
when "int64"
value.as_i64.to_io(io, IO::ByteFormat::LittleEndian)
value.raw.as(Number).to_i64!.to_io(io, IO::ByteFormat::LittleEndian)
when "float64"
value.as_f32.to_f64.to_io(io, IO::ByteFormat::LittleEndian)
value.raw.as(Number).to_f64!.to_io(io, IO::ByteFormat::LittleEndian)
when "string"
VarLong.to_io(io, value.as_s.bytesize.to_i64)
io.print value.as_s
Expand Down Expand Up @@ -254,7 +264,7 @@ input_type = nil
output_type = nil
flags = [] of String

OptionParser.parse! do |parser|
OptionParser.parse do |parser|
parser.banner = <<-'END_USAGE'
Usage: protodec [arguments]
Command-line encoder and decoder for arbitrary protobuf data. Reads from standard input.
Expand Down Expand Up @@ -284,6 +294,13 @@ flags.each do |flag|
when "p"
output_type = IOType::JsonPretty
when "e", "d"
when "v"
if flags.includes? "p"
STDOUT.puts SOFTWARE.to_pretty_json
else
STDOUT.puts SOFTWARE.to_json
end
exit(0)
else
STDERR.puts "ERROR: #{flag} is not a valid option."
exit(1)
Expand All @@ -304,7 +321,7 @@ end

case input_type
when IOType::Base64
output = ProtoBuf::Any.parse(IO::Memory.new(Base64.decode(URI.unescape(URI.unescape(STDIN.gets_to_end.strip)))))
output = ProtoBuf::Any.parse(IO::Memory.new(Base64.decode(URI.decode_www_form(URI.decode_www_form(STDIN.gets_to_end.strip)))))
when IOType::Hex
array = STDIN.gets_to_end.strip.split(/[- ,]+/).map &.to_i(16).to_u8
output = ProtoBuf::Any.parse(IO::Memory.new(Slice.new(array.size) { |i| array[i] }))
Expand Down

0 comments on commit 496a5f3

Please sign in to comment.