• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

localstack / localstack / 74eac421-9806-47ba-8f8c-92653ce53828

15 Apr 2025 06:51PM UTC coverage: 86.472% (+0.07%) from 86.407%
74eac421-9806-47ba-8f8c-92653ce53828

push

circleci

web-flow
CloudFormation Engine v2: Base Mappings and Conditions tests for Update Graph and PreProc (#12527)

2 of 2 new or added lines in 1 file covered. (100.0%)

69 existing lines in 6 files now uncovered.

63665 of 73625 relevant lines covered (86.47%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/localstack-core/localstack/cli/profiles.py
1
import argparse
1✔
2
import os
1✔
3
import sys
1✔
4
from typing import Optional
1✔
5

6
# important: this needs to be free of localstack imports
7

8

9
def set_and_remove_profile_from_sys_argv():
1✔
10
    """
11
    Performs the following steps:
12

13
    1. Use argparse to parse the command line arguments for the --profile flag.
14
       All occurrences are removed from the sys.argv list, and the value from
15
       the last occurrence is used.  This allows the user to specify a profile
16
       at any point on the command line.
17

18
    2. If a --profile flag is not found, check for the -p flag.  The first
19
       occurrence of the -p flag is used and it is not removed from sys.argv.
20
       The reasoning for this is that at least one of the CLI subcommands has
21
       a -p flag, and we want to keep it in sys.argv for that command to
22
       pick up.  An existing bug means that if a -p flag is used with a
23
       subcommand, it could erroneously be used as the profile value as well.
24
       This behaviour is undesired, but we must maintain back-compatibility of
25
       allowing the profile to be specified using -p.
26

27
    3. If a profile is found, the 'CONFIG_PROFILE' os variable is set
28
       accordingly. This is later picked up by ``localstack.config``.
29

30
    WARNING:  Any --profile options are REMOVED from sys.argv, so that they are
31
              not passed to the localstack CLI. This allows the profile option
32
              to be set at any point on the command line.
33
    """
34
    parser = argparse.ArgumentParser()
1✔
35
    parser.add_argument("--profile")
1✔
36
    namespace, sys.argv = parser.parse_known_args(sys.argv)
1✔
37
    profile = namespace.profile
1✔
38

39
    if not profile:
1✔
40
        # if no profile is given, check for the -p argument
41
        profile = parse_p_argument(sys.argv)
1✔
42

43
    if profile:
1✔
44
        os.environ["CONFIG_PROFILE"] = profile.strip()
1✔
45

46

47
def parse_p_argument(args) -> Optional[str]:
1✔
48
    """
49
    Lightweight arg parsing to find the first occurrence of ``-p <config>``, or ``-p=<config>`` and return the value of
50
    ``<config>`` from the given arguments.
51

52
    :param args: list of CLI arguments
53
    :returns: the value of ``-p``.
54
    """
55
    for i, current_arg in enumerate(args):
1✔
56
        if current_arg.startswith("-p="):
1✔
57
            # if using the "<arg>=<value>" notation, we remove the "-p=" prefix to get the value
58
            return current_arg[3:]
1✔
59
        if current_arg == "-p":
1✔
60
            # otherwise use the next arg in the args list as value
61
            try:
1✔
62
                return args[i + 1]
1✔
63
            except IndexError:
1✔
64
                return None
1✔
65

UNCOV
66
    return None
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc