Skip to content

Commit

Permalink
Add config files feature.
Browse files Browse the repository at this point in the history
The allows to have a local config to customize defaults for CL options.

Closes #22.

stack-info: PR: #32, branch: ZolotukhinM/stack/2
  • Loading branch information
ZolotukhinM committed Oct 6, 2024
1 parent f6aa5ae commit 74708e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,25 @@ stack-pr view -B HEAD~5 -H HEAD~2
# Land first three PRs from the stack
stack-pr land -B HEAD~5 -H HEAD~2
```
## Command Line Options Reference
The section is not added yet, contributions are welcome!
### Config files
Default values for command line options can be specified via a config file.
Path to the config file can be specified via `STACKPR_CONFIG` envvar, and by
default it's assumed to be `.stack-pr.cfg` in the current folder.
An example of a config file:
```cfg
[common]
verbose=True
hyperlinks=True
draft=False
keep_body=False
[repo]
remote=origin
target=main
reviewer=GithubHandle1,GithubHandle2
```
40 changes: 31 additions & 9 deletions src/stack_pr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
# ===----------------------------------------------------------------------=== #

import argparse
import configparser
import json
import os
import re
Expand Down Expand Up @@ -1230,33 +1231,41 @@ def command_view(args: CommonArgs):
# ===----------------------------------------------------------------------=== #


def create_argparser() -> argparse.ArgumentParser:
def create_argparser(
config: configparser.ConfigParser,
) -> argparse.ArgumentParser:
"""Helper for CL option definition and parsing logic."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help="sub-command help", dest="command")

common_parser = argparse.ArgumentParser(add_help=False)
common_parser.add_argument(
"-R", "--remote", default="origin", help="Remote name"
"-R",
"--remote",
default=config.get("repo", "remote", fallback="origin"),
help="Remote name",
)
common_parser.add_argument("-B", "--base", help="Local base branch")
common_parser.add_argument(
"-H", "--head", default="HEAD", help="Local head branch"
)
common_parser.add_argument(
"-T", "--target", default="main", help="Remote target branch"
"-T",
"--target",
default=config.get("repo", "target", fallback="main"),
help="Remote target branch",
)
common_parser.add_argument(
"--hyperlinks",
action=argparse.BooleanOptionalAction,
default=True,
default=config.getboolean("common", "hyperlinks", fallback=True),
help="Enable or disable hyperlink support.",
)
common_parser.add_argument(
"-V",
"--verbose",
action="store_true",
default=False,
default=config.getboolean("common", "verbose", fallback=False),
help="Enable verbose output from Git subcommands.",
)

Expand All @@ -1269,14 +1278,14 @@ def create_argparser() -> argparse.ArgumentParser:
parser_submit.add_argument(
"--keep-body",
action="store_true",
default=False,
default=config.getboolean("common", "keep_body", fallback=False),
help="Keep current PR body and only add/update cross links",
)
parser_submit.add_argument(
"-d",
"--draft",
action="store_true",
default=False,
default=config.getboolean("common", "draft", fallback=False),
help="Submit PRs in draft mode",
)
parser_submit.add_argument(
Expand All @@ -1287,7 +1296,10 @@ def create_argparser() -> argparse.ArgumentParser:
)
parser_submit.add_argument(
"--reviewer",
default=os.getenv("STACK_PR_DEFAULT_REVIEWER", default=""),
default=os.getenv(
"STACK_PR_DEFAULT_REVIEWER",
default=config.get("repo", "reviewer", fallback=""),
),
help="List of reviewers for the PR",
)

Expand All @@ -1310,8 +1322,18 @@ def create_argparser() -> argparse.ArgumentParser:
return parser


def load_config(config_file):
config = configparser.ConfigParser()
if os.path.isfile(config_file):
config.read(config_file)
return config


def main():
parser = create_argparser()
config_file = os.getenv("STACKPR_CONFIG", ".stack-pr.cfg")
config = load_config(config_file)

parser = create_argparser(config)
args = parser.parse_args()

if not args.command:
Expand Down

0 comments on commit 74708e0

Please sign in to comment.