Skip to content

Commit

Permalink
fix: exit code not passed to sys.exit (#209)
Browse files Browse the repository at this point in the history
* fix: exit code not passed to sys.exit

* feat: added fail-with-body option
  • Loading branch information
jackie-linz authored Aug 26, 2024
1 parent b5405e3 commit ff4a661
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ docker pull ghcr.io/okigan/awscurl
## Options

```sh
usage: __main__.py [-h] [-v] [-i] [-X REQUEST] [-d DATA] [-H HEADER] [-k] [--data-binary] [--region REGION] [--profile PROFILE] [--service SERVICE]
usage: __main__.py [-h] [-v] [-i] [-X REQUEST] [-d DATA] [-H HEADER] [-k] [--fail-with-body] [--data-binary] [--region REGION] [--profile PROFILE] [--service SERVICE]
[--access_key ACCESS_KEY] [--secret_key SECRET_KEY] [--security_token SECURITY_TOKEN] [--session_token SESSION_TOKEN] [-L] [-o <file>]
uri

Expand All @@ -145,6 +145,7 @@ options:
-H HEADER, --header HEADER
HTTP header (default: None)
-k, --insecure Allow insecure server connections when using SSL (default: False)
--fail-with-body Fail on HTTP errors but save the body (default: False)
--data-binary Process HTTP POST data exactly as specified with no extra processing whatsoever. (default: False)
--region REGION AWS region [env var: AWS_DEFAULT_REGION] (default: us-east-1)
--profile PROFILE AWS profile [env var: AWS_PROFILE] (default: default)
Expand Down
5 changes: 3 additions & 2 deletions awscurl/awscurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ def inner_main(argv):
parser.add_argument('-H', '--header', help='HTTP header', action='append')
parser.add_argument('-k', '--insecure', action='store_true', default=False,
help='Allow insecure server connections when using SSL')
parser.add_argument('--fail-with-body', action='store_true', help='Fail on HTTP errors but save the body', default=False)

parser.add_argument('--data-binary', action='store_true',
help='Process HTTP POST data exactly as specified with '
Expand Down Expand Up @@ -567,13 +568,13 @@ def inner_main(argv):
with open(filename, "w") as f:
f.write(response.text)

exit_code = 0 if response.ok else 1
exit_code = 0 if response.ok or not args.fail_with_body else 22

return exit_code


def main():
inner_main(sys.argv[1:])
return inner_main(sys.argv[1:])


if __name__ == '__main__':
Expand Down
21 changes: 17 additions & 4 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,31 @@ def test_make_request(self, *args, **kvargs):
class TestInnerMainMethod(TestCase):
maxDiff = None

def test_exit_code(self, *args, **kwargs):
def test_exit_code_without_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--service', 's3', 'https://awscurl-sample-bucket.s3.amazonaws.com']),
1
0
)

def test_exit_code_with_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--fail-with-body', '--service', 's3', 'https://awscurl-sample-bucket.s3.amazonaws.com']),
22
)

class TestInnerMainMethodEmptyCredentials(TestCase):
maxDiff = None

def test_exit_code(self, *args, **kwargs):
def test_exit_code_without_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--access_key', '', '--secret_key', '', '--session_token', '', '--service', 's3',
'https://awscurl-sample-bucket.s3.amazonaws.com']),
1
0
)

def test_exit_code_with_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--fail-with-body', '--access_key', '', '--secret_key', '', '--session_token', '', '--service', 's3',
'https://awscurl-sample-bucket.s3.amazonaws.com']),
22
)

0 comments on commit ff4a661

Please sign in to comment.