Skip to content

Commit

Permalink
Update (#5)
Browse files Browse the repository at this point in the history
* Adding Custom Output Format

* Updated 📖

* Build

* Updated main function to catch errors
  • Loading branch information
smashedr authored Jul 18, 2023
1 parent 8aed921 commit d8c37c5
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 16 deletions.
74 changes: 74 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .zipline.example
Original file line number Diff line number Diff line change
@@ -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
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions get_test_token.py
Original file line number Diff line number Diff line change
@@ -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]}')
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
build
flake8
psycopg2
python-decouple
python-dotenv
requests
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions zipline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

0 comments on commit d8c37c5

Please sign in to comment.