Skip to content

Commit

Permalink
Load config files from proper dir. Fixes mitmproxy#6014
Browse files Browse the repository at this point in the history
  • Loading branch information
wesolowski-gh committed Apr 2, 2023
1 parent 0475430 commit e82b04b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
23 changes: 16 additions & 7 deletions mitmproxy/optmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,7 @@ def set(self, *specs: str, defer: bool = False) -> None:
May raise an `OptionsError` if a value is malformed or an option is unknown and defer is False.
"""
# First, group specs by option name.
unprocessed: dict[str, list[str]] = {}
for spec in specs:
if "=" in spec:
name, value = spec.split("=", maxsplit=1)
unprocessed.setdefault(name, []).append(value)
else:
unprocessed.setdefault(spec, [])
unprocessed: dict[str, list[str]] = specs_to_dict(specs)

# Second, convert values to the correct type.
processed: dict[str, Any] = {}
Expand Down Expand Up @@ -519,6 +513,21 @@ def dump_dicts(opts, keys: Iterable[str] | None = None) -> dict:
return options_dict


def specs_to_dict(specs: Sequence[str]) -> dict[str, list[str]]:
"""
Takes a sequence of set specification in standard form (option=value).
Returns a dict of option names to a list of string values.
"""
options = {}
for spec in specs:
if "=" in spec:
name, value = spec.split("=", maxsplit=1)
options.setdefault(name, []).append(value)
else:
options.setdefault(spec, [])
return options


def parse(text):
if not text:
return {}
Expand Down
20 changes: 15 additions & 5 deletions mitmproxy/tools/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
from mitmproxy.utils import debug


def load_options(opts, args):
args_options = optmanager.specs_to_dict(args.setoptions)
# if confdir option is set in args, config files should be loaded from there
if "confdir" in args_options:
confdir = args_options["confdir"][-1]
else:
confdir = opts.confdir
optmanager.load_paths(
opts,
os.path.join(confdir, "config.yaml"),
os.path.join(confdir, "config.yml"),
)


def process_options(parser, opts, args):
if args.version:
print(debug.dump_system_info())
Expand Down Expand Up @@ -81,12 +95,8 @@ async def main() -> T:
sys.exit(1)

try:
load_options(opts, args)
opts.set(*args.setoptions, defer=True)
optmanager.load_paths(
opts,
os.path.join(opts.confdir, "config.yaml"),
os.path.join(opts.confdir, "config.yml"),
)
process_options(parser, opts, args)

if args.options:
Expand Down
15 changes: 15 additions & 0 deletions test/mitmproxy/test_optmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,18 @@ def test_set():
opts.process_deferred()
assert "deferredsequenceoption" not in opts.deferred
assert opts.deferredsequenceoption == ["a", "b"]


def test_spec_to_dict():
assert(optmanager.specs_to_dict([])
== {})
assert(optmanager.specs_to_dict(["option"])
== {"option": []})
assert(optmanager.specs_to_dict(["option=value"])
== {"option": ["value"]})
assert(optmanager.specs_to_dict(["option=value1", "option=value2"])
== {"option": ["value1", "value2"]})
assert(optmanager.specs_to_dict(["option=value1", "option"])
== {"option": ["value1"]})
assert(optmanager.specs_to_dict(["option1=value1", "option2=value2"])
== {"option1": ["value1"], "option2": ["value2"]})

0 comments on commit e82b04b

Please sign in to comment.