.. versionadded:: 2019.2.0

    Apply a patch to the configuration source, and load the result into the
    running config of the device.

    patchfile
        A patch file to apply to the configuration source.

    options
        Options to pass to patch.

    source_hash
        If the patch file (specified via the ``patchfile`` argument)  is an
        HTTP(S) or FTP URL and the file exists in the minion's file cache, this
        option can be passed to keep the minion from re-downloading the file if
        the cached copy matches the specified hash.

    show_changes: ``True``
        Controls how changes are presented. If ``True``, this function will
        return the of the changes made.
        If ``False``, then it will return a boolean (``True`` if any changes
        were made, otherwise False).

    source: ``running``
        The configuration source. Choose from: ``running``, ``candidate``, or
        ``startup``. Default: ``running``.

    path: ``None``
        Save the temporary configuration to a specific path, then read from
        there. This argument is optional, can the user prefers a particular
        location of the temporary file.

    test: ``False``
        Dry run? If set as ``True``, will apply the config, discard and return
        the changes. Default: ``False`` and will commit the changes on the
        device.

    commit: ``True``
        Commit the configuration changes? Default: ``True``.

    debug: ``False``
        Debug mode. Will insert a new key in the output dictionary, as
        ``loaded_config`` containing the raw configuration loaded on the device.

    replace: ``True``
        Load and replace the configuration. Default: ``True``.

    CLI Example:

    .. code-block:: bash

        salt '*' net.patch https://example.com/running_config.patch

def patch(patchfile,
          options='',
          saltenv='base',
          source_hash=None,
          show_changes=True,
          source='running',
          path=None,
          test=False,
          commit=True,
          debug=False,
          replace=True):
    '''
    .. versionadded:: 2019.2.0

    Apply a patch to the configuration source, and load the result into the
    running config of the device.

    patchfile
        A patch file to apply to the configuration source.

    options
        Options to pass to patch.

    source_hash
        If the patch file (specified via the ``patchfile`` argument)  is an
        HTTP(S) or FTP URL and the file exists in the minion's file cache, this
        option can be passed to keep the minion from re-downloading the file if
        the cached copy matches the specified hash.

    show_changes: ``True``
        Controls how changes are presented. If ``True``, this function will
        return the of the changes made.
        If ``False``, then it will return a boolean (``True`` if any changes
        were made, otherwise False).

    source: ``running``
        The configuration source. Choose from: ``running``, ``candidate``, or
        ``startup``. Default: ``running``.

    path: ``None``
        Save the temporary configuration to a specific path, then read from
        there. This argument is optional, can the user prefers a particular
        location of the temporary file.

    test: ``False``
        Dry run? If set as ``True``, will apply the config, discard and return
        the changes. Default: ``False`` and will commit the changes on the
        device.

    commit: ``True``
        Commit the configuration changes? Default: ``True``.

    debug: ``False``
        Debug mode. Will insert a new key in the output dictionary, as
        ``loaded_config`` containing the raw configuration loaded on the device.

    replace: ``True``
        Load and replace the configuration. Default: ``True``.

    CLI Example:

    .. code-block:: bash

        salt '*' net.patch https://example.com/running_config.patch
    '''
    config_saved = save_config(source=source, path=path)
    if not config_saved or not config_saved['result']:
        return config_saved
    path = config_saved['out']
    patchfile_cache = __salt__['cp.cache_file'](patchfile)
    if patchfile_cache is False:
        return {
            'out': None,
            'result': False,
            'comment': 'The file "{}" does not exist.'.format(patchfile)
        }
    replace_pattern = __salt__['file.patch'](path,
                                             patchfile_cache,
                                             options=options)
    with salt.utils.files.fopen(path, 'r') as fh_:
        updated_config = fh_.read()
    return __salt__['net.load_config'](text=updated_config,
                                       test=test,
                                       debug=debug,
                                       replace=replace,
                                       commit=commit)