99from click import Command , Option
1010from yaspin import yaspin # type: ignore
1111
12- from ..core .manage_json import read_json , write_json # type: ignore
12+ from ..core .copy_files import *
13+ from ..core .manage_json import read_json , write_json
1314from ..core .manage_templates import template_to_file
1415from ..utils .cli import clear , confirm
1516from .custom_group import CustomGroup
@@ -22,16 +23,18 @@ def __init__(self) -> None:
2223 super ().__init__ ()
2324
2425 def create (self ) -> Command :
25- help = ""
26+ help = (
27+ "Create all files for the containerization of the server/network."
28+ )
2629 options = [Option (["--network" ], is_flag = True , default = False )]
2730
2831 def callback (network : bool = False ) -> None :
32+ clear (0 )
33+
2934 services : set [dict [str , Any ]] = set ([])
3035 networks : set [str ] = set ([])
3136 envs : set [dict [str , Any ]] = set ([])
3237
33- clear (0 )
34-
3538 if not network :
3639 menu = Menus ()
3740 service , env = self .__get_data (menu )
@@ -53,13 +56,14 @@ def callback(network: bool = False) -> None:
5356 services .add (service )
5457 envs .add (env )
5558
56- clear (1 )
59+ clear (0.5 )
5760
5861 if not confirm (
5962 msg = f"Want to continue adding services? (Count: { len (services )} )" ,
6063 ):
6164 break
6265
66+ clear (0 )
6367 self .__save_files (
6468 data = {
6569 "compose" : {
@@ -69,6 +73,8 @@ def callback(network: bool = False) -> None:
6973 "envs" : envs ,
7074 }
7175 )
76+ clear (0 )
77+ print ("Files saved!" )
7278
7379 return Command (
7480 name = inspect .currentframe ().f_code .co_name , # type: ignore
@@ -78,11 +84,71 @@ def callback(network: bool = False) -> None:
7884 )
7985
8086 def update (self ) -> Command :
81- help = ""
82- options = [Option ()]
87+ help = "Update the contents of the containers."
88+ options = [
89+ Option (["--service" ], default = None ),
90+ Option (["--add" ], is_flag = True , default = False ),
91+ Option (["--remove" ], is_flag = True , default = False ),
92+ ]
93+
94+ def callback (
95+ service : str | None = None , add : bool = False , remove : bool = False
96+ ) -> None :
97+ clear (0 )
98+
99+ path : Path = Path ()
100+
101+ if not path .exists ():
102+ print ("Missing JSON file for services. Use 'create' first." )
103+ return
104+
105+ data : dict [str , Any ] = read_json (Path ()) or {}
106+ compose : dict [str , Any ] = data .get ("compose" , {}) or {}
107+
108+ services : set [dict [str , Any ]] = set (compose .get ("services" , []))
109+ networks : set [str ] = set (compose .get ("networks" , []))
110+ envs : set [dict [str , Any ]] = set (data .get ("envs" , []))
111+
112+ if networks :
113+ pass
114+ if envs :
115+ pass
116+
117+ if not services :
118+ print ("No services found. Use 'create' first." )
119+ return
120+
121+ return Command (
122+ name = inspect .currentframe ().f_code .co_name , # type: ignore
123+ help = help ,
124+ callback = callback ,
125+ params = options , # type: ignore
126+ )
83127
84- def callback () -> None :
85- pass
128+ def build (self ) -> Command :
129+ help = "Build the files for the containerization in case create failed."
130+ options : list [Option ] = []
131+
132+ def callback (
133+ service : str | None = None , add : bool = False , remove : bool = False
134+ ) -> None :
135+ clear (0 )
136+
137+ path : Path = Path ()
138+
139+ if not path .exists ():
140+ print ("Missing JSON file for services. Use 'create' first." )
141+ return
142+
143+ data : dict [str , Any ] = read_json (Path ()) or {}
144+
145+ if not data :
146+ print ("JSON file is empty. Use 'create' first." )
147+
148+ clear (0 )
149+ self .__save_files (data , build = True )
150+ clear (0 )
151+ print ("Files saved!" )
86152
87153 return Command (
88154 name = inspect .currentframe ().f_code .co_name , # type: ignore
@@ -94,7 +160,7 @@ def callback() -> None:
94160 def __get_data (
95161 self , menu : Menus , get_service : bool = True , get_env : bool = True
96162 ) -> tuple [dict [str , Any ], dict [str , Any ]]:
97- clear (1 )
163+ clear (0.5 )
98164
99165 name = self .__get_name (message = "Enter the name of the service: " )
100166
@@ -110,6 +176,7 @@ def __get_data(
110176
111177 def __get_name (self , message : str ) -> str :
112178 while True :
179+ clear (0.5 )
113180 name : str = inquirer .text ( # type: ignore
114181 message = message , validate = EmptyInputValidator ()
115182 ).execute ()
@@ -119,8 +186,10 @@ def __get_name(self, message: str) -> str:
119186
120187 return name
121188
122- def __save_files (self , data : dict [str , Any ]) -> None :
123- write_json (Path (), data )
189+ @yaspin (text = "Creating files..." , color = "cyan" )
190+ def __save_files (self , data : dict [str , Any ], build : bool = False ) -> None :
191+ if not build :
192+ write_json (Path (), data )
124193
125194 composer : dict [str , Any ] = data .get ("composer" ) or {}
126195 template_to_file (Path (), composer , Path ())
0 commit comments