Skip to content

Commit

Permalink
Allow for file/directory (json)arg references (#79)
Browse files Browse the repository at this point in the history
* Simplify some specs that don't require input
* Bump versions
  • Loading branch information
Blacksmoke16 authored Jun 30, 2021
1 parent fbe81cb commit 517d24e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The built binary will be available as `./bin/oq`. This can be relocated elsewhe

```dockerfile
# Set an arg to store the oq version that should be installed.
ARG OQ_VERSION=1.2.0
ARG OQ_VERSION=1.2.1

# Grab the binary from the latest Github release and make it executable; placing it within /usr/local/bin. Can also put it elsewhere if you so desire.
RUN wget https://github.com/Blacksmoke16/oq/releases/download/v${OQ_VERSION}/oq-v${OQ_VERSION}-linux-x86_64 -O /usr/local/bin/oq && chmod +x /usr/local/bin/oq
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: oq
description: |
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
version: 1.2.0
version: 1.2.1

authors:
- George Dietrich <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: oq
version: '1.2.0'
version: '1.2.1'
summary: A performant, and portable jq wrapper to support formats other than JSON
description: |
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
Expand Down
36 changes: 24 additions & 12 deletions spec/oq_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ describe OQ do
describe "with null input option" do
describe "with a scalar value" do
it "should return the correct output" do
run_binary(input: nil, args: ["-n", "0"]) do |output|
run_binary(args: ["-n", "0"]) do |output|
output.should eq "0\n"
end
end

it "should return the correct output" do
run_binary(input: nil, args: ["--null-input", "0"]) do |output|
run_binary(args: ["--null-input", "0"]) do |output|
output.should eq "0\n"
end
end

describe "with a JSON object string" do
it "should return the correct output" do
run_binary(input: nil, args: ["-cn", %([{"foo":"bar"},{"foo":"baz"}])]) do |output|
run_binary(args: ["-cn", %([{"foo":"bar"},{"foo":"baz"}])]) do |output|
output.should eq %([{"foo":"bar"},{"foo":"baz"}]\n)
end
end
Expand Down Expand Up @@ -172,7 +172,7 @@ describe OQ do

describe "when using 'input'" do
it "should return the correct output" do
run_binary(args: ["-c", "-n", "-f", "spec/assets/stream-filter", "spec/assets/stream-data.json"]) do |output|
run_binary(args: ["-cn", "-f", "spec/assets/stream-filter", "spec/assets/stream-data.json"]) do |output|
output.should eq %({"possible_victim01":{"total":3,"evildoers":{"evil.com":2,"soevil.com":1}},"possible_victim02":{"total":1,"evildoers":{"bad.com":1}},"possible_victim03":{"total":1,"evildoers":{"soevil.com":1}}}\n)
end
end
Expand All @@ -188,59 +188,71 @@ describe OQ do

describe "--arg" do
it "single arg" do
run_binary(input: %({}), args: ["-c", "--arg", "foo", "bar", %({"name":$foo})]) do |output|
run_binary(args: ["-cn", "--arg", "foo", "bar", %({"name":$foo})]) do |output|
output.should eq %({"name":"bar"}\n)
end
end

it "multiple arg" do
run_binary(input: %("a: b"), args: ["-c", "-r", "--arg", "chart", "stolon", "--arg", "version", "1.5.10", "$version"]) do |output|
run_binary(args: ["-rcn", "-r", "--arg", "chart", "stolon", "--arg", "version", "1.5.10", "$version"]) do |output|
output.should eq %(1.5.10\n)
end
end

it "different option in between args" do
run_binary(input: %("a: b"), args: ["-c", "--arg", "chart", "stolon", "-r", "--arg", "version", "1.5.10", "$version"]) do |output|
run_binary(args: ["-rcn", "--arg", "chart", "stolon", "--arg", "version", "1.5.10", "$version"]) do |output|
output.should eq %(1.5.10\n)
end
end

it "when the arg name matches a directory name" do
run_binary(args: ["-rcn", "--arg", "spec", "dir", "$spec"]) do |output|
output.should eq %(dir\n)
end
end
end

describe "with the --argjson option" do
it "should be passed correctly" do
run_binary(input: %({}), args: ["-c", "--argjson", "foo", "123", %({"id":$foo})]) do |output|
run_binary(args: ["-rcn", "--argjson", "foo", "123", %({"id":$foo})]) do |output|
output.should eq %({"id":123}\n)
end
end

it "when the arg name matches a directory name" do
run_binary(args: ["-rcn", "--argjson", "spec", "123", "$spec"]) do |output|
output.should eq %(123\n)
end
end
end

describe "with the --slurpfile option" do
it "should be passed correctly" do
run_binary(input: %({}), args: ["-c", "--slurpfile", "ids", "spec/assets/raw.json", %({"ids":$ids})]) do |output|
run_binary(args: ["-rcn", "--slurpfile", "ids", "spec/assets/raw.json", %({"ids":$ids})]) do |output|
output.should eq %({"ids":[1,2,3]}\n)
end
end
end

describe "with the --rawfile option" do
it "should be passed correctly" do
run_binary(input: %({}), args: ["-c", "--rawfile", "ids", "spec/assets/raw.json", %({"ids":$ids})]) do |output|
run_binary(args: ["-rcn", "--rawfile", "ids", "spec/assets/raw.json", %({"ids":$ids})]) do |output|
output.should eq %({"ids":"1\\n2\\n3\\n"}\n)
end
end
end

describe "with the --args option" do
it "should be passed correctly" do
run_binary(input: %({}), args: ["-c", %({"ids":$ARGS.positional}), "--args", "1", "2", "3"]) do |output|
run_binary(args: ["-rcn", %({"ids":$ARGS.positional}), "--args", "1", "2", "3"]) do |output|
output.should eq %({"ids":["1","2","3"]}\n)
end
end
end

describe "with the --jsonargs option" do
it "should be passed correctly" do
run_binary(input: %({}), args: ["-c", %({"ids":$ARGS.positional}), "--jsonargs", "1", "2", "3"]) do |output|
run_binary(args: ["-rcn", %({"ids":$ARGS.positional}), "--jsonargs", "1", "2", "3"]) do |output|
output.should eq %({"ids":[1,2,3]}\n)
end
end
Expand Down
6 changes: 5 additions & 1 deletion src/oq.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "./converters/*"

# A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
module OQ
VERSION = "1.2.0"
VERSION = "1.2.1"

# The support formats that can be converted to/from.
enum Format
Expand Down Expand Up @@ -118,6 +118,10 @@ module OQ
self.consume_file_args input_args, "--rawfile", "--argfile", "--slurpfile"
self.consume_file_args input_args, "-f", "--from-file", count: 1

# Also parse out --arg, and --argjson as they may include identifiers that also exist as a directory/file
# which would result in incorrect arg extraction.
self.consume_file_args input_args, "--arg", "--argjson"

# Extract `jq` arguments from `ARGV`.
self.extract_args input_args, output

Expand Down

0 comments on commit 517d24e

Please sign in to comment.