11from abc import ABC , abstractmethod
22from config_manager import ConfigManager
3+ import shlex
34
45
56class PlatformCommandBuilder (ABC ):
@@ -11,6 +12,38 @@ def build_env_command(self, env_dict):
1112 def build_service_start_script (self , service_name , start_command , binary_path ):
1213 pass
1314
15+ @abstractmethod
16+ def build_chdir_command (self , chdir ):
17+ pass
18+
19+ @abstractmethod
20+ def build_rm_command (self , file_path , recursive = False ):
21+ pass
22+
23+ @abstractmethod
24+ def build_mkdir_command (self , path ):
25+ pass
26+
27+ @abstractmethod
28+ def build_exists_command (self , path ):
29+ pass
30+
31+ @abstractmethod
32+ def get_shell_command (self ):
33+ pass
34+
35+ @abstractmethod
36+ def build_entrypoint_script (self , env_cmd , chdir_cmd , cmd , verbose ):
37+ pass
38+
39+ @abstractmethod
40+ def check_exists_result (self , ret ):
41+ pass
42+
43+
44+ def escape_powershell_single_quote (value ):
45+ return str (value ).replace ("'" , "''" )
46+
1447
1548class WindowsCommandBuilder (PlatformCommandBuilder ):
1649 def build_env_command (self , env_dict ):
@@ -21,8 +54,7 @@ def build_env_command(self, env_dict):
2154 for k , v in env_dict .items ():
2255 if v is None or v == "" :
2356 continue
24- env_commands .append (f"$env:{ k } ='{ v } '" )
25-
57+ env_commands .append (f"$env:{ escape_powershell_single_quote (k )} ='{ escape_powershell_single_quote (v )} '" )
2658 return "; " .join (env_commands ) + ";"
2759
2860 def build_service_start_script (self , service_name , start_command , binary_path ):
@@ -66,18 +98,92 @@ def build_service_start_script(self, service_name, start_command, binary_path):
6698Remove-Item "$env:USERPROFILE\\ start_{ service_name } .ps1" -Force -ErrorAction SilentlyContinue
6799"""
68100
101+ def build_chdir_command (self , chdir ):
102+ if chdir is None :
103+ return "Set-Location $env:USERPROFILE"
104+ return f"Set-Location '{ escape_powershell_single_quote (chdir )} '"
105+
106+ def build_rm_command (self , file_path , recursive = False ):
107+ flags = "-Force -ErrorAction SilentlyContinue"
108+ if recursive :
109+ flags += " -Recurse"
110+ return f"Remove-Item '{ escape_powershell_single_quote (str (file_path ))} ' { flags } "
111+
112+ def build_mkdir_command (self , path ):
113+ return f"New-Item -ItemType Directory -Path '{ escape_powershell_single_quote (str (path ))} ' -Force"
114+
115+ def build_exists_command (self , path ):
116+ return f"Test-Path '{ escape_powershell_single_quote (str (path ))} '"
117+
118+ def get_shell_command (self ):
119+ return "powershell.exe -Command -"
120+
121+ def build_entrypoint_script (self , env_cmd , chdir_cmd , cmd , verbose ):
122+ env_section = f"{ env_cmd } \n " if env_cmd else ""
123+ script = f"""
124+ $ErrorActionPreference = "Stop"
125+
126+ { env_section } { chdir_cmd }
127+
128+ { cmd }
129+ """
130+ if verbose :
131+ script = f"$VerbosePreference = 'Continue'\n { script } "
132+ return script
133+
134+ def check_exists_result (self , ret ):
135+ return ret .stdout and ret .stdout .strip ().lower () == "true"
136+
69137
70138class UnixCommandBuilder (PlatformCommandBuilder ):
71139 def build_env_command (self , env_dict ):
72140 if not env_dict :
73141 return ""
74142
75- env_values = " " .join (f"' { k } ={ v } ' " for k , v in env_dict .items () if v is not None and v != "" )
76- return f"env { env_values } "
143+ env_values = " " .join (f"{ k } ={ shlex . quote ( str ( v )) } " for k , v in env_dict .items () if v is not None and v != "" )
144+ return f"export { env_values } \n "
77145
78146 def build_service_start_script (self , service_name , start_command , binary_path ) -> str :
79147 return start_command
80148
149+ def build_chdir_command (self , chdir ):
150+ if chdir is None :
151+ return "cd $HOME"
152+ return f"cd '{ shlex .quote (str (chdir ))} '"
153+
154+ def build_rm_command (self , file_path , recursive = False ):
155+ flag = "-rf" if recursive else "-f"
156+ return f"rm { flag } { shlex .quote (str (file_path ))} "
157+
158+ def build_mkdir_command (self , path ):
159+ return f"mkdir -p { shlex .quote (str (path ))} "
160+
161+ def build_exists_command (self , path ):
162+ return f"test -e { shlex .quote (str (path ))} "
163+
164+ def get_shell_command (self ):
165+ return "bash"
166+
167+ def build_entrypoint_script (self , env_cmd , chdir_cmd , cmd , verbose ):
168+ script = f"""
169+ set -o pipefail
170+ set -o errexit
171+ set -o nounset
172+ set -o errtrace
173+
174+ { env_cmd }
175+
176+ { chdir_cmd }
177+
178+ { cmd }
179+ """
180+ if verbose :
181+ script = f"set -x\n { script } "
182+ return script
183+
184+ def check_exists_result (self , ret ):
185+ return ret .returncode == 0
186+
81187
82188class PlatformFactory :
83189 @staticmethod
0 commit comments