Skip to content

Commit

Permalink
feat: new --edit option to edit config file in editor (#2028)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Jun 16, 2023
1 parent cd4c06d commit f726959
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
40 changes: 39 additions & 1 deletion src/pdm/cli/commands/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import argparse
import os
from pathlib import Path
from typing import Any, Mapping

from pdm import termui
from pdm._types import RepositoryConfig
from pdm.cli.commands.base import BaseCommand
from pdm.exceptions import PdmUsageError
from pdm.project import Project
from pdm.project.config import DEFAULT_REPOSITORIES, REPOSITORY, SOURCE, Config

Expand All @@ -21,10 +24,46 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
help="Set config in the project's local configuration file",
)
parser.add_argument("-d", "--delete", action="store_true", help="Unset a configuration key")
parser.add_argument(
"-e",
"--edit",
action="store_true",
help="Edit the configuration file in the default editor(defined by EDITOR env var)",
)
parser.add_argument("key", help="Config key", nargs="?")
parser.add_argument("value", help="Config value", nargs="?")

@staticmethod
def get_editor() -> str:
for key in "VISUAL", "EDITOR":
rv = os.getenv(key)
if rv:
return rv
if os.name == "nt":
return "notepad"
for editor in "sensible-editor", "vim", "nano":
if os.system(f"which {editor} >/dev/null 2>&1") == 0:
return editor
return "vi"

def edit_file(self, path: Path) -> None:
import subprocess

editor = self.get_editor()
proc = subprocess.Popen(f'{editor} "{path}"', shell=True)

if proc.wait() != 0:
raise PdmUsageError(f"Editor {editor} exited abnormally")

def handle(self, project: Project, options: argparse.Namespace) -> None:
self.ui = project.core.ui
if options.edit:
if options.key:
raise PdmUsageError("Cannot specify an argument when `--edit` is given")
if options.delete:
raise PdmUsageError("`--delete` doesn't work when `--edit` is given")
config = project.project_config if options.local else project.global_config
return self.edit_file(config.config_file)
if options.delete:
self._delete_config(project, options)
elif options.value:
Expand Down Expand Up @@ -101,7 +140,6 @@ def _show_config(self, config: Mapping[str, Any], supersedes: Mapping[str, Any])
)

def _list_config(self, project: Project, options: argparse.Namespace) -> None:
self.ui = project.core.ui
assert Config.site is not None
self.ui.echo(
f"Site/default configuration ([success]{Config.site.config_file}[/]):",
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/completions/pdm.bash
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ _pdm_a919b69078acdf0a_complete()
;;

(config)
opts="--delete --global --help --local --project --verbose"
opts="--delete --edit --global --help --local --project --verbose"
;;

(export)
Expand Down
3 changes: 2 additions & 1 deletion src/pdm/cli/completions/pdm.fish
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from completion' -l help -d 'Show

# config
complete -c pdm -A -n '__fish_seen_subcommand_from config' -l delete -d 'Unset a configuration key'
complete -c pdm -A -n '__fish_seen_subcommand_from config' -l edit -d 'Edit the configuration file in the default editor(defined by EDITOR env var)'
complete -c pdm -A -n '__fish_seen_subcommand_from config' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from config' -l help -d 'Show this help message and exit.'
complete -c pdm -A -n '__fish_seen_subcommand_from config' -l local -d 'Set config in the project\'s local configuration file'
Expand All @@ -102,7 +103,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from config' -l verbose -d 'Use `-

# export
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l dev -d 'Select dev dependencies'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l expandvars -d 'Expand environment variables in pyproject.toml'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l expandvars -d 'Expand environment variables in requirements'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l format -d 'Specify the export file format'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l group -d 'Select group of optional-dependencies separated by comma or dev-dependencies (with `-d`). Can be supplied multiple times, use ":all" to include all groups under the same species.'
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/completions/pdm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function TabExpansion($line, $lastWord) {
}
"completion" { $completer.AddParams(@("powershell", "bash", "zsh", "fish")); break }
"config" {
$completer.AddOpts(@([Option]::new(@("--delete", "--global", "--local", "-d", "-l", "-g")), $projectOption))
$completer.AddOpts(@([Option]::new(@("--delete", "--global", "--local", "--edit", "-e", "-d", "-l", "-g")), $projectOption))
$completer.AddParams(@(getConfigKeys), $false)
break
}
Expand Down
1 change: 1 addition & 0 deletions src/pdm/cli/completions/pdm.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ _pdm() {
{-g,--global}'[Use the global project, supply the project root with `-p` option]' \
{-l,--local}"[Set config in the project's local configuration file]" \
{-d,--delete}'[Unset a configuration key]' \
{-e,--edit}'[Edit the configuration file in the default editor(defined by EDITOR env var)]' \
'1:key:->keys' \
'2:value:_files' && return 0
if [[ $state == keys ]]; then
Expand Down

0 comments on commit f726959

Please sign in to comment.