mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 08:55:22 +01:00
Also rename yaml filename with rename command (#3447)
This commit is contained in:
parent
50a32b387e
commit
2e4645310b
1 changed files with 70 additions and 68 deletions
|
@ -496,14 +496,14 @@ def command_rename(args, config):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
|
# Load existing yaml file
|
||||||
with open(CORE.config_path, mode="r+", encoding="utf-8") as raw_file:
|
with open(CORE.config_path, mode="r+", encoding="utf-8") as raw_file:
|
||||||
raw_contents = raw_file.read()
|
raw_contents = raw_file.read()
|
||||||
|
|
||||||
yaml = yaml_util.load_yaml(CORE.config_path)
|
yaml = yaml_util.load_yaml(CORE.config_path)
|
||||||
if CONF_ESPHOME not in yaml or CONF_NAME not in yaml[CONF_ESPHOME]:
|
if CONF_ESPHOME not in yaml or CONF_NAME not in yaml[CONF_ESPHOME]:
|
||||||
print(
|
print(
|
||||||
color(
|
color(Fore.BOLD_RED, "Complex YAML files cannot be automatically renamed.")
|
||||||
Fore.BOLD_RED, "Complex YAML files cannot be automatically renamed."
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
old_name = yaml[CONF_ESPHOME][CONF_NAME]
|
old_name = yaml[CONF_ESPHOME][CONF_NAME]
|
||||||
|
@ -536,23 +536,24 @@ def command_rename(args, config):
|
||||||
flags=re.MULTILINE,
|
flags=re.MULTILINE,
|
||||||
)
|
)
|
||||||
|
|
||||||
raw_file.seek(0)
|
new_path = os.path.join(CORE.config_dir, args.name + ".yaml")
|
||||||
raw_file.write(new_raw)
|
print(
|
||||||
raw_file.flush()
|
f"Updating {color(Fore.CYAN, CORE.config_path)} to {color(Fore.CYAN, new_path)}"
|
||||||
|
)
|
||||||
print(f"Updating {color(Fore.CYAN, CORE.config_path)}")
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
rc = run_external_process("esphome", "config", CORE.config_path)
|
with open(new_path, mode="w", encoding="utf-8") as new_file:
|
||||||
|
new_file.write(new_raw)
|
||||||
|
|
||||||
|
rc = run_external_process("esphome", "config", new_path)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
raw_file.seek(0)
|
|
||||||
raw_file.write(raw_contents)
|
|
||||||
print(color(Fore.BOLD_RED, "Rename failed. Reverting changes."))
|
print(color(Fore.BOLD_RED, "Rename failed. Reverting changes."))
|
||||||
|
os.remove(new_path)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
cli_args = [
|
cli_args = [
|
||||||
"run",
|
"run",
|
||||||
CORE.config_path,
|
new_path,
|
||||||
"--no-logs",
|
"--no-logs",
|
||||||
"--device",
|
"--device",
|
||||||
CORE.address,
|
CORE.address,
|
||||||
|
@ -566,10 +567,11 @@ def command_rename(args, config):
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
rc = 1
|
rc = 1
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
raw_file.seek(0)
|
os.remove(new_path)
|
||||||
raw_file.write(raw_contents)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
os.remove(CORE.config_path)
|
||||||
|
|
||||||
print(color(Fore.BOLD_GREEN, "SUCCESS"))
|
print(color(Fore.BOLD_GREEN, "SUCCESS"))
|
||||||
print()
|
print()
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in a new issue