From ee47b302eed8699f48ad7b647cbccea8d9a915c3 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Wed, 30 Aug 2023 14:24:18 +0100 Subject: [PATCH 1/6] feat: allow file flag on bref-local --- src/bref-local | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/bref-local b/src/bref-local index d370bc924..1f46907e4 100755 --- a/src/bref-local +++ b/src/bref-local @@ -19,6 +19,13 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) { $handler = $argv[1] ?? ''; $data = $argv[2] ?? null; +if ($data === '--file') { + if ($argv[3] === null) { + throw new Exception('A file path to a valid JSON file must be passed when invoking the `--file` flag.'); + } + $data = $argv[3]; +} + try { $handler = Bref::getContainer()->get($handler); } catch (NotFoundExceptionInterface $e) { From bc2f6753d8064d16227c180e17eaf9a681a679f4 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Wed, 30 Aug 2023 21:22:27 +0100 Subject: [PATCH 2/6] Uses getopt to parse args --- src/bref-local | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/bref-local b/src/bref-local index 1f46907e4..cf1a4d387 100755 --- a/src/bref-local +++ b/src/bref-local @@ -18,12 +18,10 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) { $handler = $argv[1] ?? ''; $data = $argv[2] ?? null; +$opts = getopt("f::"); -if ($data === '--file') { - if ($argv[3] === null) { - throw new Exception('A file path to a valid JSON file must be passed when invoking the `--file` flag.'); - } - $data = $argv[3]; +if (isset($opts['f']) && file_exists($opts['f'])) { + $data = file_get_contents($opts['f']); } try { From 53d87cee3bebf669d7824c681ceaee5b78d70480 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Wed, 30 Aug 2023 22:01:39 +0100 Subject: [PATCH 3/6] Long option path is convention on local SLS --- src/bref-local | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/bref-local b/src/bref-local index cf1a4d387..49971bd27 100755 --- a/src/bref-local +++ b/src/bref-local @@ -18,10 +18,14 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) { $handler = $argv[1] ?? ''; $data = $argv[2] ?? null; -$opts = getopt("f::"); +$opts = getopt('', ['path:']); -if (isset($opts['f']) && file_exists($opts['f'])) { - $data = file_get_contents($opts['f']); +if (isset($opts['path'])) { + if (! file_exists($opts['path'])) { + throw new Exception('The file ' . $opts['path'] . ' could not be found.'); + } + + $data = file_get_contents($opts['path']); } try { From 5bbfdd0fe7201144f82db73ec01598f4246e8f87 Mon Sep 17 00:00:00 2001 From: Alex Theobold Date: Thu, 31 Aug 2023 15:55:29 +0100 Subject: [PATCH 4/6] Resets the handler where path flag is set --- src/bref-local | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bref-local b/src/bref-local index 49971bd27..63e77d5ac 100755 --- a/src/bref-local +++ b/src/bref-local @@ -25,6 +25,7 @@ if (isset($opts['path'])) { throw new Exception('The file ' . $opts['path'] . ' could not be found.'); } + $handler = $argv[array_key_last($argv)]; $data = file_get_contents($opts['path']); } From f21c526503b9ce2a2a2a4fe8e9f5e789dae33f9e Mon Sep 17 00:00:00 2001 From: Alex Theobold Date: Thu, 31 Aug 2023 16:41:16 +0100 Subject: [PATCH 5/6] Adds docs to show path flag usage --- docs/function/local-development.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/function/local-development.md b/docs/function/local-development.md index 025046e0c..6fa407d94 100644 --- a/docs/function/local-development.md +++ b/docs/function/local-development.md @@ -80,6 +80,14 @@ Hello world # With JSON event data $ vendor/bin/bref-local my-function.php '{"name": "Jane"}' Hello Jane + +# With a path to a file containing a JSON event. +$cat event.json +{ + "name": "Alex" +} +$ vendor/bin/bref-local --path event.json my-function.php +Hello Alex ``` If you want to run your function in Docker: From 9b7211cb10dd3115326fc7fbc7872332de313177 Mon Sep 17 00:00:00 2001 From: Alex Theobold Date: Thu, 31 Aug 2023 16:42:47 +0100 Subject: [PATCH 6/6] Fix docs typo --- docs/function/local-development.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/function/local-development.md b/docs/function/local-development.md index 6fa407d94..6fe40633e 100644 --- a/docs/function/local-development.md +++ b/docs/function/local-development.md @@ -82,7 +82,7 @@ $ vendor/bin/bref-local my-function.php '{"name": "Jane"}' Hello Jane # With a path to a file containing a JSON event. -$cat event.json +$ cat event.json { "name": "Alex" }