diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..6ad110b --- /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 setup.py install + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline zipline.py + + - name: test-3.10 + image: python:3.10 + depends_on: + - zipline + commands: + - python3 -m pip install -r requirements.txt + - flake8 zipline.py + - python3 setup.py install + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline zipline.py + + - name: test-3.9 + image: python:3.9 + depends_on: + - zipline + commands: + - python3 -m pip install -r requirements.txt + - flake8 zipline.py + - python3 setup.py install + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline zipline.py + + - name: test-3.8 + image: python:3.8 + depends_on: + - zipline + commands: + - python3 -m pip install -r requirements.txt + - flake8 zipline.py + - python3 setup.py install + - export ZIPLINE_TOKEN=$(python3 get_test_token.py) + - zipline zipline.py 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 6d04311..55f9223 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,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_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/get_test_token.py b/get_test_token.py new file mode 100644 index 0000000..19dfe4f --- /dev/null +++ b/get_test_token.py @@ -0,0 +1,39 @@ +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 +r = requests.post(zip_url, json=zip_data) +r.raise_for_status() + +# get token from postgres +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) + +# print environment variable for export +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 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..8855f51 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: @@ -143,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) @@ -212,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()