find_tag_and_insert_text

Operation to find a tag and insert text below that tag.

This is a simple combination of the find_tagged_line and insert_text operations.

Example

The following example inserts text to a file named settings.py by first searching for a string matching the tag "# qwikstart: middleware":

examples/operations/find_tag_and_insert_text.yml
steps:
    "Add middleware to settings":
        name: find_tag_and_insert_text
        file_path: "examples/data/settings.py"
        tag: "# qwikstart: middleware"
        text: '"django.contrib.auth.middleware.AuthenticationMiddleware",'

Before this operation is run, settings.py would have the following code:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    # qwikstart: middleware
]

After running the operation, the text is inserted below the tag:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    # qwikstart: middleware
    "django.contrib.auth.middleware.AuthenticationMiddleware",
]

Required context

file_path

Path to file relative to the current working directory.

tag

Text used as a placeholder for detecting where to insert text. For example:

# qwikstart: inject-line-below
text

Text that will be inserted.

Optional context

line_ending

default: '\n'

Text appended to the end of inserted text.

match_indent

default: True

See also