@@ -92,6 +92,38 @@ def remove_service(service_name: str, conf_file_path: Optional[Path] = None) ->
9292 config .write (configfile , space_around_delimiters = False )
9393
9494
95+ def rename_service (old_name : str , new_name : str , conf_file_path : Optional [Path ] = None ) -> None :
96+ """Rename a service in the service file.
97+
98+ The service settings are preserved under the new name and the old
99+ section is removed.
100+
101+ Args:
102+ old_name: current service name
103+ new_name: desired service name
104+ conf_file_path: path to the pg_service.conf. If None the `conf_path()` is used,
105+ defaults to None
106+
107+ Raises:
108+ ServiceFileNotFound: when the service file is not found
109+ ServiceNotFound: when the old service is not found
110+ PermissionError: when the service file is read-only
111+ """
112+ config = full_config (conf_file_path )
113+ if old_name not in config :
114+ raise ServiceNotFound (
115+ service_name = old_name ,
116+ existing_service_names = service_names (),
117+ pg_service_filepath = conf_file_path or conf_path (),
118+ )
119+
120+ settings = dict (config [old_name ])
121+ config .remove_section (old_name )
122+ config [new_name ] = settings
123+ with open (conf_file_path or conf_path (), "w" ) as configfile :
124+ config .write (configfile , space_around_delimiters = False )
125+
126+
95127def service_config (service_name : str , conf_file_path : Optional [Path ] = None ) -> dict :
96128 """Returns the config from the given service name as a dict.
97129
0 commit comments