Skip to content
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
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
## 7.0.0b14 (unreleased)


- Add PowerShell support to the `completion` command: auto-detect PowerShell on Windows,
add correct profile path, create directory if missing, and fix success message to use
`. $PROFILE` instead of `source`.
[Anu13lol]

- Save `~/.plonecli/config.toml` with a real TOML writer, so quotes, backslashes
and newlines in any value round-trip instead of producing a file plonecli can
no longer read. An unreadable config now fails with a message naming the path.
Expand Down Expand Up @@ -512,3 +517,4 @@ New features:

- initital version with list templates support and bobtemplates.plone integration
[MrTango, tmassman, Gomez]

30 changes: 20 additions & 10 deletions plonecli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ def skill(context, action, scope, copy_only, force):
@click.argument(
"shell",
required=False,
type=click.Choice(["bash", "zsh", "fish"]),
type=click.Choice(["bash", "zsh", "fish", "powershell"]),
)
@click.option(
"--install", is_flag=True, help="Install completion into your shell config"
Expand All @@ -755,14 +755,18 @@ def completion(shell, install):
import os

if shell is None:
login_shell = os.path.basename(os.environ.get("SHELL", ""))
if login_shell in ("bash", "zsh", "fish"):
shell = login_shell

if os.environ.get("PSModulePath"):
shell = "powershell"
else:
raise click.UsageError(
f"Could not detect shell (SHELL={os.environ.get('SHELL', '')!r}).\n"
"Please specify one: plonecli completion bash|zsh|fish"
)
login_shell = os.path.basename(os.environ.get("SHELL", ""))
if login_shell in ("bash", "zsh", "fish"):
shell = login_shell
else:
raise click.UsageError(
f"Could not detect shell (SHELL={os.environ.get('SHELL', '')!r}).\n"
"Please specify one: plonecli completion bash|zsh|fish"
)

env_var = "_PLONECLI_COMPLETE"
source_cmd = f"{env_var}={shell}_source plonecli"
Expand All @@ -785,13 +789,17 @@ def completion(shell, install):
"bash": os.path.expanduser("~/.bashrc"),
"zsh": os.path.expanduser("~/.zshrc"),
"fish": os.path.expanduser("~/.config/fish/completions/plonecli.fish"),
"powershell": str(Path.home() / "Documents" / "PowerShell" / "Microsoft.PowerShell_profile.ps1"),
}
rc_file = rc_files[shell]

if shell == "fish":
# Fish uses a completions directory with the script itself
os.makedirs(os.path.dirname(rc_file), exist_ok=True)
eval_line = f"env {source_cmd} | source"
elif shell == "powershell":
os.makedirs(os.path.dirname(rc_file), exist_ok=True)
eval_line = f'$env:_PLONECLI_COMPLETE = "powershell_source"\nplonecli | Out-String | Invoke-Expression'
else:
eval_line = f'eval "$({source_cmd})"'

Expand All @@ -807,8 +815,10 @@ def completion(shell, install):
f.write(f"\n# plonecli shell completion\n{eval_line}\n")

echo(f"Shell completion installed in {rc_file}", fg="green")
echo(f"Restart your shell or run: source {rc_file}", fg="green")

if shell == "powershell":
echo("Restart your shell or run: . $PROFILE", fg="green")
else:
echo(f"Restart your shell or run: source {rc_file}", fg="green")

if __name__ == "__main__":
cli()
Loading