11import asyncio
22import os
3- import zipfile
43from pathlib import Path
54
65import click
76import yaml
87
9- from metablock import Metablock
8+ from metablock import Metablock , __version__
9+ from metablock .utils import temp_zipfile
1010
1111METABLOCK_SPACE = os .environ .get ("METABLOCK_SPACE" , "" )
1212METABLOCK_ENV = os .environ .get ("METABLOCK_ENV" , "prod" )
@@ -20,6 +20,7 @@ def manifest(file_path: Path) -> dict:
2020
2121
2222@click .group ()
23+ @click .version_option (version = __version__ )
2324def main () -> None :
2425 pass
2526
@@ -131,25 +132,13 @@ async def _ship(path: str, env: str, block_id: str, name: str, token: str) -> No
131132 click .echo ("metablock block-id is required" , err = True )
132133 raise click .Abort ()
133134 p = Path (path )
134- if not p .is_dir ():
135- click .echo (f"path { p } does not exist" , err = True )
136- raise click .Abort ()
137-
138- # Create a zip file from the directory
139- zip_path = p .with_suffix (".zip" )
140135 try :
141- with zipfile .ZipFile (zip_path , "w" , zipfile .ZIP_DEFLATED ) as zipf :
142- for file in p .rglob ("*" ): # Recursively add all files in the directory
143- arcname = file .relative_to (p ) # Preserve relative paths in the archive
144- zipf .write (file , arcname )
145- click .echo (f"Created zip file: { zip_path } " )
146-
147- async with Metablock (auth_key = token ) as mb :
148- block = await mb .blocks .get (block_id )
149- await block .ship (zip_path , name = name , env = env )
150- click .echo (f"shipped { zip_path } to { block .name } { env } " )
151- finally :
152- # Clean up the zip file after shipping
153- if zip_path .exists ():
154- zip_path .unlink ()
155- click .echo (f"Removed temporary zip file: { zip_path } " )
136+ with temp_zipfile (p ) as zip_path :
137+ click .echo (f"Created zip file: { zip_path } " )
138+ async with Metablock (auth_key = token ) as mb :
139+ block = await mb .blocks .get (block_id )
140+ await block .ship (zip_path , name = name , env = env , timeout = 10 )
141+ click .echo (f"shipped { zip_path } to { block .name } { env } " )
142+ except ValueError as e :
143+ click .echo (str (e ), err = True )
144+ raise click .Abort () from None
0 commit comments