Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rename profile option #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions sshoot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def action_get_command(self, manager: Manager, args: Namespace):
)
self.print(" ".join(cmdline))

def action_rename(self, manager: Manager, args: Namespace):
"""Rename a sshoot profile."""
manager.rename_profile(args.name, args.new_name)
self.print(_(f"Profile renamed to '{args.new_name}'."))

def get_parser(self) -> ArgumentParser:
"""Return a configured argparse.ArgumentParse instance."""
parser = ArgumentParser(
Expand Down Expand Up @@ -288,6 +293,20 @@ def get_parser(self) -> ArgumentParser:
profile_completer,
)

# Rename a profile
rename_parser = subparsers.add_parser(
"rename", help=_("rename a profile")
)
complete_argument(
rename_parser.add_argument(
"name", help=_("name of the profile to rename"),
),
profile_completer
)
rename_parser.add_argument(
"new_name", help=_("profile new name")
)

# Get profile command
get_command_parser = subparsers.add_parser(
"get-command", help=_("return the sshuttle command for a profile")
Expand Down
6 changes: 6 additions & 0 deletions sshoot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ def is_running(self, name: str) -> bool:
return False
return True

def rename_profile(self, old_name: str, new_name: str):
"""Rename a profile."""
profile = self.get_profile(old_name)
self._config.add_profile(new_name, profile)
self.remove_profile(old_name)

def get_cmdline(
self,
name: str,
Expand Down
12 changes: 12 additions & 0 deletions tests/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,18 @@ def test_is_running_pidfile_no_process(self, profile_manager, pid_file):
# The stale pidfile is deleted.
assert not pid_file.exists()

def test_rename_profile(self, profile_manager: Manager, profile):
"""If the profile exists, rename it."""
profile_manager.rename_profile("profile", "new_profile")
new_profile = profile_manager.get_profile("new_profile")

assert new_profile is not None

def test_rename_profile_unknown(self, profile_manager: Manager):
"""Manager.rename_profile raises an error if name is unknown."""
with pytest.raises(ManagerProfileError):
profile_manager.rename_profile("unknown", "new_profile")

def test_get_cmdline(self, profile_manager, pid_file):
"""Manager.get_cmdline returns the command line for the profile."""
assert profile_manager.get_cmdline("profile") == [
Expand Down
Loading