11import asyncio
22import os
3+ import zipfile
34from pathlib import Path
45
56import click
89from metablock import Metablock
910
1011METABLOCK_SPACE = os .environ .get ("METABLOCK_SPACE" , "" )
12+ METABLOCK_ENV = os .environ .get ("METABLOCK_ENV" , "prod" )
13+ METABLOCK_BLOCK_ID = os .environ .get ("METABLOCK_BLOCK_ID" , "" )
1114METABLOCK_API_TOKEN = os .environ .get ("METABLOCK_API_TOKEN" , "" )
1215
1316
@@ -22,15 +25,50 @@ def main() -> None:
2225
2326@main .command ()
2427@click .argument ("path" , type = click .Path (exists = True ))
25- @click .option ("--space" , "space_name" , help = "Space name" , default = METABLOCK_SPACE )
28+ @click .option (
29+ "--space" ,
30+ "space_name" ,
31+ help = "Space name" ,
32+ default = METABLOCK_SPACE ,
33+ show_default = True ,
34+ )
2635@click .option ("--token" , help = "metablock API token" , default = METABLOCK_API_TOKEN )
2736def apply (path : str , space_name : str , token : str ) -> None :
28- """Apply metablock manifest"""
37+ """Apply metablock manifest to a metablock space """
2938 asyncio .get_event_loop ().run_until_complete (
3039 _apply (path , space_name or METABLOCK_SPACE , token or METABLOCK_API_TOKEN )
3140 )
3241
3342
43+ @main .command ()
44+ @click .argument ("path" , type = click .Path (exists = True ))
45+ @click .option (
46+ "--env" ,
47+ help = "Environment to ship to" ,
48+ type = click .Choice (["prod" , "stage" ]),
49+ default = METABLOCK_ENV ,
50+ show_default = True ,
51+ )
52+ @click .option (
53+ "--block-id" ,
54+ help = "Block ID" ,
55+ default = METABLOCK_BLOCK_ID ,
56+ show_default = True ,
57+ )
58+ @click .option (
59+ "--name" ,
60+ help = "Optional deployment name" ,
61+ default = "shipped from metablock-py" ,
62+ show_default = True ,
63+ )
64+ @click .option ("--token" , help = "metablock API token" , default = METABLOCK_API_TOKEN )
65+ def ship (path : str , env : str , block_id : str , name : str , token : str ) -> None :
66+ """Deploy a new version of html block"""
67+ asyncio .get_event_loop ().run_until_complete (
68+ _ship (path , env , block_id , name , token or METABLOCK_API_TOKEN )
69+ )
70+
71+
3472async def _apply (path : str , space_name : str , token : str ) -> None :
3573 if not token :
3674 click .echo ("metablock API token is required" , err = True )
@@ -52,9 +90,41 @@ async def _apply(path: str, space_name: str, token: str) -> None:
5290 block = by_name .get (name )
5391 if block :
5492 # update
55- await mb .services .update (block .id , ** config )
93+ await mb .blocks .update (block .id , ** config )
5694 click .echo (f"updated block { name } " )
5795 else :
5896 # create
59- await space .services .create (name = name , ** config )
97+ await space .blocks .create (name = name , ** config )
6098 click .echo (f"created new block { name } " )
99+
100+
101+ async def _ship (path : str , env : str , block_id : str , name : str , token : str ) -> None :
102+ if not token :
103+ click .echo ("metablock API token is required" , err = True )
104+ raise click .Abort ()
105+ if not block_id :
106+ click .echo ("metablock block-id is required" , err = True )
107+ raise click .Abort ()
108+ p = Path (path )
109+ if not p .is_dir ():
110+ click .echo (f"path { p } does not exist" , err = True )
111+ raise click .Abort ()
112+
113+ # Create a zip file from the directory
114+ zip_path = p .with_suffix (".zip" )
115+ try :
116+ with zipfile .ZipFile (zip_path , "w" , zipfile .ZIP_DEFLATED ) as zipf :
117+ for file in p .rglob ("*" ): # Recursively add all files in the directory
118+ arcname = file .relative_to (p ) # Preserve relative paths in the archive
119+ zipf .write (file , arcname )
120+ click .echo (f"Created zip file: { zip_path } " )
121+
122+ async with Metablock (auth_key = token ) as mb :
123+ block = await mb .blocks .get (block_id )
124+ await block .ship (zip_path , name = name , env = env )
125+ click .echo (f"shipped { zip_path } to { block .name } { env } " )
126+ finally :
127+ # Clean up the zip file after shipping
128+ if zip_path .exists ():
129+ zip_path .unlink ()
130+ click .echo (f"Removed temporary zip file: { zip_path } " )
0 commit comments