Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --json-file/-j option to read from file w/ json contents #311

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0fbb22d
wip: simple json handling - needs big clean up
jacobmealey Apr 26, 2024
9e2f4a4
clean up source code
jacobmealey May 7, 2024
0bd91b6
added more tests, need to add special handling for queries
jacobmealey May 7, 2024
0e22840
finalized testing and found a bug where querys all stack together
jacobmealey May 10, 2024
e01fd50
query strings generating. need to fix for iteration
jacobmealey May 11, 2024
3d55d30
cleaning up for pr
jacobmealey May 14, 2024
dcea792
test w/ actions
jacobmealey May 14, 2024
41bb29e
Added sudo back
jacobmealey May 14, 2024
18ecad7
put json objects
jacobmealey May 15, 2024
29de2be
remove putting whole object?
jacobmealey May 15, 2024
a1d0ed8
removed whole line
jacobmealey May 15, 2024
ba76262
fixed memory issues
jacobmealey May 15, 2024
5618105
remove sanitizer
jacobmealey May 15, 2024
1d9d70d
spelling
jacobmealey May 15, 2024
1685194
attempting to make everything play nice
jacobmealey May 15, 2024
0785c94
fixed typo
jacobmealey May 15, 2024
5264619
different packages and paths
jacobmealey May 15, 2024
20c3b8c
cleaned up tests
jacobmealey May 15, 2024
73b272a
removed mac os specifics from makefile, clean style
jacobmealey May 15, 2024
95fa596
remove params putting
jacobmealey May 15, 2024
26e4859
not leaking
jacobmealey May 15, 2024
bb1a0e3
free this_query
jacobmealey May 15, 2024
373bec0
clean up for pr
jacobmealey May 16, 2024
1851fee
cleanup file handling
jacobmealey May 16, 2024
cc74cdf
get other actions working -- need to get curl for win working
jacobmealey May 16, 2024
b44e296
undo file cleanup... doesn't work on windows
jacobmealey May 16, 2024
542fe6a
put json in behind a flag
jacobmealey May 17, 2024
6ceea4a
fix whitespace in tests.json
jacobmealey May 17, 2024
12e6725
white space Update .github/workflows/makefile.yml
jacobmealey May 17, 2024
6eb908e
Update homebrew includes .github/workflows/makefile.yml
jacobmealey May 17, 2024
292fd77
Update .github/workflows/makefile.yml
jacobmealey May 17, 2024
a8dad0a
make --json-file act like other optional builds
jacobmealey May 17, 2024
ad93491
Merge branch 'trurl-json-in' of github.com:jacobmealey/urler into tru…
jacobmealey May 17, 2024
aa39f61
Make it act as a filter
jacobmealey May 24, 2024
e5e2429
added error check for parts key
jacobmealey May 24, 2024
ede3153
style
jacobmealey May 24, 2024
aa7d58b
update oom messaging
jacobmealey May 24, 2024
d2b3fca
cleanup makefile
jacobmealey May 24, 2024
2f81538
Update Makefile
jacobmealey May 24, 2024
3975591
flip around the lines
jacobmealey May 24, 2024
1c22c47
makefile JSON_C_PREFIX, update readme
jacobmealey Jun 11, 2024
e6f135e
cleanup readme
jacobmealey Jun 11, 2024
a947832
trying setting json_c_path in GITHUB_ENV
jacobmealey Jun 11, 2024
7502449
prefix not path
jacobmealey Jun 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ jobs:
sudo apt-get update
sudo apt-get install libcurl4-openssl-dev

- name: install libjson-c
run: |
sudo apt-get update
sudo apt-get install libjson-c-dev

- name: build
run: make

Expand Down
22 changes: 20 additions & 2 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
jobs:
ubuntu:
runs-on: ubuntu-latest

env:
TRURL_JSON_IN: true

strategy:
matrix:
Expand All @@ -30,7 +33,7 @@ jobs:
- name: install libcurl
run: |
sudo apt-get update
sudo apt-get install libcurl4-openssl-dev ${{ matrix.build.install_packages }}
sudo apt-get install libcurl4-openssl-dev libjson-c-dev ${{ matrix.build.install_packages }}

- name: code style check
run: make checksrc
Expand All @@ -47,13 +50,16 @@ jobs:
cygwin:
runs-on: windows-latest

env:
TRURL_JSON_IN: true

steps:
- uses: actions/checkout@v4

- name: install cygwin
uses: cygwin/cygwin-install-action@master
with:
packages: curl, libcurl-devel, libcurl4, make, gcc-core, python39
packages: curl, libcurl-devel, libcurl4, make, gcc-core, python39, libjson-c-devel

- name: make
run: make
Expand All @@ -67,9 +73,21 @@ jobs:
macos:
runs-on: macos-latest

env:
TRURL_JSON_IN: true

steps:
- uses: actions/checkout@v4

- name: Install Homebrew
id: install-homebrew
uses: Homebrew/actions/setup-homebrew@master

- name: Install json-c
run: |
brew install json-c
echo "JSON_C_PREFIX=$(brew --prefix)" >> "$GITHUB_ENV"

- name: make
run: make

Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ CFLAGS += -Wconversion -Wmissing-prototypes -Wwrite-strings -Wsign-compare -Wno-
ifndef NDEBUG
CFLAGS += -g
endif
ifdef TRURL_JSON_IN
CFLAGS += -DTRURL_JSON_IN -Wno-gnu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any info on -Wno-gnu is it necessary? because it breaks the build on older GCCs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm thats not good. It was required by json-c , I think some of there macros are what required this. I'm wondering if we should jump out of json-c, and use one that is purely c89 (and small enough we can just include the source in the repo or something like @vszakats has been saying)

I think a lot of the scaffolding can stay in place for parsing single objects / streaming stuff. it would really just be the guts of from_json() that would need to be swapped out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What OS / compiler have you been using?

LDLIBS += -ljson-c
ifneq ($(strip $(JSON_C_PREFIX)),)
CFLAGS += -I$(JSON_C_PREFIX)/include
LDLIBS += -L$(JSON_C_PREFIX)/lib
endif
endif
MANUAL = trurl.1

PREFIX ?= /usr/local
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ trurl also uses `CURLUPART_ZONEID` added in libcurl 7.81.0 and
It would certainly be possible to make trurl work with older libcurl versions
if someone wanted to.

## Building with JSON input
To allow trurl to accept JSON input you must install libjson-c. Json input is disabled by default, so it
must be enabled at compile time.
1. Install json-c
- On Ubuntu and Debian linux it is called `libjson-c-dev`.
- On Cygwin it is called `libjson-c-devel`.
- On Homebrew it is called `json-c`.
2. Enable JSON input at compile time
- Ensure `TRURL_JSON_IN` is in you environment
- In your environment set `JSON_C_PREFIX` to the install path of json-c to include and link json-c from unexpected locations. This step is only required if json-c is installed somewhere gcc can't find.


### Older libcurls

trurl builds with libcurl older than 7.81.0 but will then not work as
Expand Down
66 changes: 66 additions & 0 deletions testfiles/test0003.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}},
{
"parts": {
"host": "example.com"
}}
]
31 changes: 31 additions & 0 deletions testfiles/test0004.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"parts": {
"scheme": "ftp",
"user": "scream",
"host": "url.com",
"path": "/",
"query": "ignoredquery",
"fragment": "a-fragment"
},
"params": [
{
"key": "query",
"value": "pair"
},
{
"key": "singlequery",
}
]
},
{
"parts": {
"scheme": "http",
"host": "example.org",
"path": "/"
}
},
{
"url": "example.com"
}
]
58 changes: 58 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,20 @@
"returncode": 0
}
},
{
"required": ["json-input"],
"input": {
"arguments": [
"--json-file",
"testfiles/test0003.txt"
]
},
"expected": {
"returncode": 0,
"stderr": "",
"stdout": "http://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\nhttp://example.com/\n"
}
},
{
"input": {
"arguments": [
Expand Down Expand Up @@ -2544,5 +2558,49 @@
"stderr": "",
"returncode": 0
}
},
{
"required": ["json-input"],
"input": {
"arguments": [
"-j",
"testfiles/test0001.txt"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testfiles/test0001.txt is not in this PR

]
},
"expected": {
"returncode": 13 ,
"stderr": "trurl error: Cannot parse JSON, expected an array of objects.\ntrurl error: Try trurl -h for help\n",
"stdout": ""
}
},
{
"required": ["json-input"],
"input": {
"arguments": [
"-j",
"testfiles/test0004.txt"
]
},
"expected": {
"returncode": 0,
"stderr": "trurl note: ignoring 'query', provide a separate 'params' array.\ntrurl note: Required key \"parts\" not found in json object.\n",
"stdout": "ftp://[email protected]/?query=pair&singlequery#a-fragment\nhttp://example.org/\n"
}
},
{
"required": ["json-input"],
"input": {
"arguments": [
"-j",
"testfiles/test0004.txt",
"--set",
"user=person"
]
},
"expected": {
"returncode": 0,
"stderr": "trurl note: ignoring 'query', provide a separate 'params' array.\ntrurl note: Required key \"parts\" not found in json object.\n",
"stdout": "ftp://[email protected]/?query=pair&singlequery#a-fragment\nhttp://[email protected]/\n"
}
}
]
37 changes: 37 additions & 0 deletions trurl.1
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ Set the component to multiple values and output the result once for each
iteration. Several combined iterations are allowed to generate combinations,
but only one \fI--iterate\fP option per component. The listed items to iterate
over should be separated by single spaces.
.IP "-j, --json-file [file]"
Creates urls based on a file which holds a JSON array. The --json-file option expects
an array of objects, each object containing a "parts" object and optionally a "params"
array. The keys of the "parts" object are equivalent to the options for --get or --set.
The only required key in the parts object is "host". the get/set option "query" is not
a valid key for the parts object. --json-file requires a separate "params" array where
each element of the array specifies a "key" and a "value".

Like --url-file, [file] may either be a path to a file, or if the file name is '-' then
--json-file will read from standard in.
.IP "--json"
Outputs all set components of the URLs as JSON objects. All components of the
URL that have data get populated in the parts object using their component
Expand Down Expand Up @@ -457,6 +467,33 @@ A problem with --get
A problem with --iterate
.IP 12
A problem with --replace or --force-replace
.f1
.IP "use JSON input for trurl"
.nf
$ echo '
[
{
"parts": {
"host": "example.com",
"path": "/a/path/to/index"
},
"params": [
{
"key": "foo",
"value": "bar"
}
]
},
{
"parts": {
"host": "curl.se",
"scheme": "ftp"
}
}
]' | trurl -j -
http://example.com/a/path/to/index?foo=bar
ftp://curl.se/
.fi
.SH WWW
https://curl.se/trurl
.SH "SEE ALSO"
Expand Down
Loading