From 04f4a8e9339102aa4aca7d71404db108d68dbee9 Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Mon, 17 Jul 2023 13:59:04 -0700 Subject: [PATCH 1/7] Adding Custom Output Format --- setup.py | 2 +- zipline.py | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 992aa1f..0468425 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ long_description = f.read() setup( - version='0.1.4', + version='0.1.5', name='zipline-cli', description='Python 3 CLI for Zipline', long_description=long_description, diff --git a/zipline.py b/zipline.py index c2c5db4..d151798 100644 --- a/zipline.py +++ b/zipline.py @@ -17,7 +17,6 @@ class ZipURL(object): Zipline URL Object :param file_url: str: Zipline File Display URL """ - __slots__ = ['url', 'raw'] def __init__(self, file_url: str): @@ -76,7 +75,6 @@ def send_file(self, file_name: str, file_object: TextIO, r = requests.post(url, headers=headers, files=files) r.raise_for_status() return ZipURL(r.json()['files'][0]) - # return f'https://example.com/dummy/{file_name}' def format_output(filename: str, url: ZipURL) -> str: @@ -86,9 +84,8 @@ def format_output(filename: str, url: ZipURL) -> str: :param url: ZipURL: ZipURL to Format :return: str: Formatted Output """ - if url.raw: - return f'{filename}\n{url}\n{url.raw}' - return f'{filename}\n{url}' + zipline_format = config('ZIPLINE_FORMAT', '{filename}\n{url}\n{raw_url}') + return zipline_format.format(filename=filename, url=url, raw_url=url.raw) def gen_rand(length: Optional[int] = 4) -> str: From 42bddc459e2b67d3cb28ce59255c30575bc4db33 Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Mon, 17 Jul 2023 18:54:13 -0700 Subject: [PATCH 2/7] Added ZIPLINE_FORMAT and Drone --- .drone.yml | 74 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 14 +++++---- get_test_token.py | 41 ++++++++++++++++++++++++++ requirements.txt | 2 ++ 4 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 .drone.yml create mode 100644 get_test_token.py diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..81d76a5 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,74 @@ +--- +load: "" +kind: pipeline +name: test-amd64 + +platform: + arch: amd64 + +environment: + ZIPLINE_URL: http://zipline:3000/ + +services: + - name: postgres + image: postgres:15 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DATABASE: postgres + + - name: zipline + image: ghcr.io/diced/zipline:latest + environment: + DEBUG: true + CORE_PORT: 3000 + CORE_RETURN_HTTPS: false + CORE_SECRET: H8Y9lSs48w3HwOgFfpaF0G + CORE_DATABASE_URL: postgres://postgres:postgres@postgres/postgres + depends_on: + - postgres + +steps: + - name: test-3.11 + image: python:3.11 + depends_on: + - zipline + commands: + - python3 -m pip install -r requirements.txt + - flake8 zipline.py + - python3 -m pip install -e . + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline test/test.txt + + - name: test-3.10 + image: python:3.10 + depends_on: + - zipline + commands: + - python3 -m pip install -r requirements.txt + - flake8 zipline.py + - python3 -m pip install -e . + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline test/test.txt + + - name: test-3.9 + image: python:3.9 + depends_on: + - zipline + commands: + - python3 -m pip install -r requirements.txt + - flake8 zipline.py + - python3 -m pip install -e . + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline test/test.txt + + - name: test-3.8 + image: python:3.8 + depends_on: + - zipline + commands: + - python3 -m pip install -r requirements.txt + - flake8 zipline.py + - python3 -m pip install -e . + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline test/test.txt diff --git a/README.md b/README.md index 6d04311..cbc5411 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![PyPI](https://img.shields.io/github/issues-raw/cssnr/zipline-cli?logo=github&logoColor=white&label=Issues)](https://github.com/cssnr/zipline-cli/issues) [![Codacy Badge](https://img.shields.io/codacy/grade/1eee626c47fa4e6fb8b1ed3efdd3e518?logo=codacy&logoColor=white&label=Codacy)](https://app.codacy.com/gh/cssnr/zipline-cli/dashboard) [![PyPI](https://img.shields.io/pypi/v/zipline-cli?logo=python&logoColor=white&label=PyPi)](https://pypi.org/project/zipline-cli/) +[![Build Status](https://drone.hosted-domains.com/api/badges/cssnr/zipline-cli/status.svg)](https://drone.hosted-domains.com/cssnr/zipline-cli) [![](https://repository-images.githubusercontent.com/661201286/8dfadbc8-94c0-4eaa-88bd-7ee351859510)](https://github.com/cssnr/zipline-cli) # Zipline CLI @@ -95,12 +96,13 @@ Environment Variables are stored in the `.zipline` file in your home directory. * Location: `~/.zipline` or `$HOME/.zipline` -| Variable | Description | -|----------------|-----------------------------------------------------------------------------| -| ZIPLINE_URL | URL to your Zipline Instance | -| ZIPLINE_TOKEN | Authorization Token from Zipline | -| ZIPLINE_EMBED | Set this enable Embed on your uploads | -| ZIPLINE_EXPIRE | See: https://zipline.diced.tech/docs/guides/upload-options#image-expiration | +| Variable | Description | +|----------------|------------------------------------------------------------------------------| +| ZIPLINE_URL | URL to your Zipline Instance | +| ZIPLINE_TOKEN | Authorization Token from Zipline | +| ZIPLINE_EMBED | Set this enable Embed on your uploads | +| ZIPLINE_FORMAT | Output Format after upload. Available variables: `{url.url}` and `{url.raw}` | +| ZIPLINE_EXPIRE | See: https://zipline.diced.tech/docs/guides/upload-options#image-expiration | You may override them by exporting the variables in your current environment or using the corresponding command line arguments. See `-h` for more info. diff --git a/get_test_token.py b/get_test_token.py new file mode 100644 index 0000000..39c614a --- /dev/null +++ b/get_test_token.py @@ -0,0 +1,41 @@ +import os +import sys +import psycopg2 +import requests + +query = 'SELECT "token" FROM public."User" WHERE id=1 ORDER BY id ASC;' + +pg_data = { + 'host': 'postgres', + 'database': 'postgres', + 'user': 'postgres', + 'password': 'postgres', +} + +zip_data = { + 'username': 'administrator', + 'password': 'password', + 'code': None, +} + +# set zipline url +zip_uri = os.environ.get('ZIPLINE_URL', 'http://zipline:3000').rstrip('/') +zip_url = f'{zip_uri}/api/auth/login' + +# log in to create account (and get cookie) +r = requests.post(zip_url, json=zip_data) +r.raise_for_status() + +# get token (or could use cookie, not tested) +with psycopg2.connect(**pg_data) as conn: + with conn.cursor() as cur: + cur.execute(query) + result = cur.fetchone() + +# exit with error if no results +if not result: + print(f'No result from postgres query: {query}') + sys.exit(1) + +# export environment variable +print(f'{result[0]}') diff --git a/requirements.txt b/requirements.txt index e53e361..313aa59 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ build +flake8 +psycopg2 python-decouple python-dotenv requests From e366b902e935e386b729c62a130545b8defc670c Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Mon, 17 Jul 2023 18:56:04 -0700 Subject: [PATCH 3/7] Test file fix --- .drone.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index 81d76a5..8b9338b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -38,7 +38,7 @@ steps: - flake8 zipline.py - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - - zipline test/test.txt + - zipline zipline.py - name: test-3.10 image: python:3.10 @@ -49,7 +49,7 @@ steps: - flake8 zipline.py - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - - zipline test/test.txt + - zipline zipline.py - name: test-3.9 image: python:3.9 @@ -60,7 +60,7 @@ steps: - flake8 zipline.py - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - - zipline test/test.txt + - zipline zipline.py - name: test-3.8 image: python:3.8 @@ -71,4 +71,4 @@ steps: - flake8 zipline.py - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - - zipline test/test.txt + - zipline zipline.py From b7467ec704a3a8a5609a0e496eb7700b96721b72 Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Mon, 17 Jul 2023 19:21:11 -0700 Subject: [PATCH 4/7] Updated :book: --- .drone.yml | 4 ++++ README.md | 14 +++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.drone.yml b/.drone.yml index 8b9338b..cfa0255 100644 --- a/.drone.yml +++ b/.drone.yml @@ -36,6 +36,7 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py + - python3 -m build - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py @@ -47,6 +48,7 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py + - python3 -m build - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py @@ -58,6 +60,7 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py + - python3 -m build - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py @@ -69,6 +72,7 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py + - python3 -m build - python3 -m pip install -e . - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py diff --git a/README.md b/README.md index cbc5411..efb7a4d 100644 --- a/README.md +++ b/README.md @@ -96,13 +96,13 @@ Environment Variables are stored in the `.zipline` file in your home directory. * Location: `~/.zipline` or `$HOME/.zipline` -| Variable | Description | -|----------------|------------------------------------------------------------------------------| -| ZIPLINE_URL | URL to your Zipline Instance | -| ZIPLINE_TOKEN | Authorization Token from Zipline | -| ZIPLINE_EMBED | Set this enable Embed on your uploads | -| ZIPLINE_FORMAT | Output Format after upload. Available variables: `{url.url}` and `{url.raw}` | -| ZIPLINE_EXPIRE | See: https://zipline.diced.tech/docs/guides/upload-options#image-expiration | +| Variable | Description | +|----------------|-----------------------------------------------------------------------------| +| ZIPLINE_URL | URL to your Zipline Instance | +| ZIPLINE_TOKEN | Authorization Token from Zipline | +| ZIPLINE_EMBED | Set this enable Embed on your uploads | +| ZIPLINE_FORMAT | Output Format after upload. Available variables: `{url}` and `{raw_url}` | +| ZIPLINE_EXPIRE | See: https://zipline.diced.tech/docs/guides/upload-options#image-expiration | You may override them by exporting the variables in your current environment or using the corresponding command line arguments. See `-h` for more info. From 81defebbba8fd0120a8448cee23ab47eb57eb627 Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Mon, 17 Jul 2023 19:37:40 -0700 Subject: [PATCH 5/7] Build --- .drone.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.drone.yml b/.drone.yml index cfa0255..6ad110b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -36,8 +36,7 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py - - python3 -m build - - python3 -m pip install -e . + - python3 setup.py install - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py @@ -48,8 +47,7 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py - - python3 -m build - - python3 -m pip install -e . + - python3 setup.py install - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py @@ -60,8 +58,7 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py - - python3 -m build - - python3 -m pip install -e . + - python3 setup.py install - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py @@ -72,7 +69,6 @@ steps: commands: - python3 -m pip install -r requirements.txt - flake8 zipline.py - - python3 -m build - - python3 -m pip install -e . + - python3 setup.py install - export ZIPLINE_TOKEN=$(python3 get_test_token.py) - zipline zipline.py From af0f717441880eb98508efdf06ccb5b307647b23 Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:04:52 -0700 Subject: [PATCH 6/7] Updated main function to catch errors --- .zipline.example | 3 ++- README.md | 16 +++++++++------- zipline.py | 10 +++++++--- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.zipline.example b/.zipline.example index fcbd4ce..b3c5fc8 100644 --- a/.zipline.example +++ b/.zipline.example @@ -1,4 +1,5 @@ ZIPLINE_URL=https://i.cssnr.com/ ZIPLINE_TOKEN=LlMPn29pIhfFHiCeNFJPtr8h.7gdjTxk5bpnSdMZNSF ZIPLINE_EMBED=True -ZIPLINE_EXPIRE= +ZIPLINE_FORMAT="{filename} -> {url}\n{raw_url}" +ZIPLINE_EXPIRE=1y diff --git a/README.md b/README.md index efb7a4d..55f9223 100644 --- a/README.md +++ b/README.md @@ -96,13 +96,15 @@ Environment Variables are stored in the `.zipline` file in your home directory. * Location: `~/.zipline` or `$HOME/.zipline` -| Variable | Description | -|----------------|-----------------------------------------------------------------------------| -| ZIPLINE_URL | URL to your Zipline Instance | -| ZIPLINE_TOKEN | Authorization Token from Zipline | -| ZIPLINE_EMBED | Set this enable Embed on your uploads | -| ZIPLINE_FORMAT | Output Format after upload. Available variables: `{url}` and `{raw_url}` | -| ZIPLINE_EXPIRE | See: https://zipline.diced.tech/docs/guides/upload-options#image-expiration | +| Variable | Description | +|----------------|------------------------------------------------------------------------------| +| ZIPLINE_URL | URL to your Zipline Instance | +| ZIPLINE_TOKEN | Authorization Token from Zipline | +| ZIPLINE_EMBED | Set this enable Embed on your uploads | +| ZIPLINE_FORMAT | Output Format after upload. Variables: `{filename}`, `{url}` and `{raw_url}` | +| ZIPLINE_EXPIRE | See: https://zipline.diced.tech/docs/guides/upload-options#image-expiration | + +See [.zipline.example](.zipline.example) for an example `.zipline` file. You may override them by exporting the variables in your current environment or using the corresponding command line arguments. See `-h` for more info. diff --git a/zipline.py b/zipline.py index d151798..8855f51 100644 --- a/zipline.py +++ b/zipline.py @@ -140,7 +140,7 @@ def setup(env_file: Path) -> None: sys.exit(0) -def main() -> None: +def run() -> None: zipline_file = '.zipline' env_file = Path(os.path.expanduser('~')) / zipline_file dotenv_path = env_file if os.path.isfile(env_file) else find_dotenv(filename=zipline_file) @@ -209,11 +209,15 @@ def main() -> None: sys.exit(exit_code) -if __name__ == '__main__': +def main() -> None: try: - main() + run() except KeyboardInterrupt: sys.exit(1) except Exception as error: print('\nError: {}'.format(str(error))) sys.exit(1) + + +if __name__ == '__main__': + main() From f8b70b8a72976b5be32bec6d7950f6fcde1c4575 Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:25:15 -0700 Subject: [PATCH 7/7] Update --- get_test_token.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/get_test_token.py b/get_test_token.py index 39c614a..19dfe4f 100644 --- a/get_test_token.py +++ b/get_test_token.py @@ -4,14 +4,12 @@ import requests query = 'SELECT "token" FROM public."User" WHERE id=1 ORDER BY id ASC;' - pg_data = { 'host': 'postgres', 'database': 'postgres', 'user': 'postgres', 'password': 'postgres', } - zip_data = { 'username': 'administrator', 'password': 'password', @@ -22,11 +20,11 @@ zip_uri = os.environ.get('ZIPLINE_URL', 'http://zipline:3000').rstrip('/') zip_url = f'{zip_uri}/api/auth/login' -# log in to create account (and get cookie) +# log in to create account r = requests.post(zip_url, json=zip_data) r.raise_for_status() -# get token (or could use cookie, not tested) +# get token from postgres with psycopg2.connect(**pg_data) as conn: with conn.cursor() as cur: cur.execute(query) @@ -37,5 +35,5 @@ print(f'No result from postgres query: {query}') sys.exit(1) -# export environment variable +# print environment variable for export print(f'{result[0]}')