Skip to content

Commit

Permalink
Split property label to name and version
Browse files Browse the repository at this point in the history
  • Loading branch information
blavka committed Jul 1, 2022
1 parent 28f6521 commit 50939b2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
39 changes: 26 additions & 13 deletions src/hardwario/chester/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import sys
import string
import re
from datetime import datetime
from loguru import logger
from ..pib import PIB, PIBException
Expand Down Expand Up @@ -216,25 +217,33 @@ def cli_fw(ctx, url, token):
ctx.obj['fwapi'] = FirmwareApi(url=url, token=token)


def validate_version(ctx, param, value):
if re.match(r'^v\d{1,3}\.\d{1,3}\.\d{1,3}(-.*?)?$', value):
return value
raise click.BadParameter('Bad version format expect is example: v1.0.0-alpha .')


@cli_fw.command('upload')
@click.option('--label', type=str, help='Firmware label (max 100 characters).', prompt=True, required=True)
@click.option('--name', type=str, help='Firmware name (max 100 characters).', prompt=True, required=True)
@click.option('--version', type=str, help='Firmware version (max 50 characters).', callback=validate_version, prompt=True, required=True)
@click.pass_context
def command_fw_upload(ctx, label):
def command_fw_upload(ctx, name, version):
'''Upload application firmware.'''
fw = ctx.obj['fwapi'].upload(label, '.')
fw = ctx.obj['fwapi'].upload(name, version, '.')
url = ctx.obj['fwapi'].url
click.echo(f'Unique identifier: {fw["id"]}')
click.echo(f'Sharable link : https://firmware.hardwario.com/chester/{fw["id"]}')
click.echo(f'Sharable link : {url[:-4]}/{fw["id"]}')


@cli_fw.command('list')
@click.option('--limit', type=click.IntRange(0, 100, clamp=True))
@click.pass_context
def command_fw_upload(ctx, limit):
'''List application firmwares.'''
click.echo(f'{"UUID":32} {"Time":19} Label')
click.echo(f'{"UUID":32} {"Upload UTC date/time":20} Label')
for fw in ctx.obj['fwapi'].list(limit=limit):
dt = datetime.fromtimestamp(fw['timestamp'])
click.echo(f'{fw["id"]} {dt} {fw["label"]}')
dt = fw['created_at'][:10] + ' ' + fw['created_at'][11:-5]
click.echo(f'{fw["id"]} {dt} {fw["name"]}:{fw["version"]}')


@cli_fw.command('delete')
Expand All @@ -249,19 +258,23 @@ def command_fw_delete(ctx, id):

@cli_fw.command('info')
@click.option('--id', metavar="ID", show_default=True, required=True)
@click.option('--show-all', is_flag=True, help='Show all properties.')
@click.pass_context
def command_fw_info(ctx, id):
def command_fw_info(ctx, id, show_all):
'''Info firmware detail.'''
fw = ctx.obj['fwapi'].detail(id)
url = ctx.obj['fwapi'].url
click.echo(f'Unique identifier: {fw["id"]}')
click.echo(f'Label: {fw["label"]}')
click.echo(f'Sharable link: https://firmware.hardwario.com/chester/{fw["id"]}')
click.echo(f'Upload date/time: {datetime.fromtimestamp(fw["timestamp"])}')
click.echo(f'Commit revision: {fw["revision"]}')
click.echo(f'Name: {fw["name"]}')
click.echo(f'Version: {fw["version"]}')
click.echo(f'Sharable link: {url}/{fw["id"]}')
click.echo(f'Upload date/time: {fw["created_at"]}')
click.echo(f'Commit revision: {fw["git_revision"]}')
click.echo(f'SHA256 firmware: {fw["firmware_sha256"]}')
click.echo(f'SHA256 app_update: {fw["app_update_sha256"]}')
click.echo(f'SHA256 zephyr_elf: {fw["zephyr_elf_sha256"]}')
click.echo(f'Build Manifest: {json.dumps(fw["manifest"])}')
if show_all:
click.echo(f'Build Manifest: {json.dumps(fw["manifest"])}')


def main():
Expand Down
17 changes: 10 additions & 7 deletions src/hardwario/chester/firmwareapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def __init__(self, url=DEFAULT_API_URL, token=None):
if token:
self.set_token(token)

@property
def url(self):
return self._url

def set_token(self, token):
self._headers['Authorization'] = 'Bearer ' + token

Expand All @@ -38,19 +42,18 @@ def request(self, method, url, **kwargs):

return self._response.json()

def upload(self, label, app_path='.'):
logger.debug(f'label={label}')

revision = None
def upload(self, name, version, app_path='.'):
git_revision = None
try:
revision = subprocess.check_output(
git_revision = subprocess.check_output(
['git', '-C', app_path, 'rev-parse', 'HEAD']).decode('ascii').strip()
except Exception:
pass

data = {
'label': label,
'revision': revision
'name': name,
'version': version,
'git_revision': git_revision
}

files = {}
Expand Down

0 comments on commit 50939b2

Please sign in to comment.