Skip to content

Instantly share code, notes, and snippets.

@yasuharu519
Last active September 30, 2018 17:25
Show Gist options
  • Save yasuharu519/7771a704edd53874cef215a8d947e46e to your computer and use it in GitHub Desktop.
Save yasuharu519/7771a704edd53874cef215a8d947e46e to your computer and use it in GitHub Desktop.
toggle krypton ssh config
#!/usr/bin/env python3
"""
.ssh/config の krypton の設定を toggle する
"""
from pathlib import Path
from logging import getLogger, StreamHandler, INFO
SSH_CONFIG = Path("~/.ssh/config").expanduser()
START_COMMENT = "# Added by Krypton start"
END_COMMENT = "# Added by Krypton end"
logger = getLogger(__name__)
handler = StreamHandler()
logger.setLevel(INFO)
logger.addHandler(handler)
logger.propagate = False
def toggledConfig(lines, start, end):
"""
start end の間のコメントアウトをトグル
"""
is_turnon = False
for i in range(start + 1, end):
current = lines[i]
new = ""
if current.startswith("# "):
new = current[2:]
is_turnon = True
else:
new = "# {}".format(current)
is_turnon = False
lines[i] = new
if is_turnon:
logger.info("Krypton mode on")
else:
logger.info("Krypton mode off")
return lines
def main():
# コメントの Index を取得
lines = []
with open(SSH_CONFIG) as f:
lines = f.readlines()
start = [i for i, x in enumerate(lines) if x.startswith(START_COMMENT)]
end = [i for i, x in enumerate(lines) if x.startswith(END_COMMENT)]
if len(start) > 0 and len(end) > 0:
newconf = toggledConfig(lines, start[0], end[0])
with open(SSH_CONFIG, "w") as f:
f.writelines(newconf)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment