.. versionadded:: 2018.3.0

    Change the attributes of files. This function accepts one or more files and
    the following options:

    operator
        Can be wither ``add`` or ``remove``. Determines whether attributes
        should be added or removed from files

    attributes
        One or more of the following characters: ``aAcCdDeijPsStTu``,
        representing attributes to add to/remove from files

    version
        a version number to assign to the file(s)

    flags
        One or more of the following characters: ``RVf``, representing
        flags to assign to chattr (recurse, verbose, suppress most errors)

    CLI Example:

    .. code-block:: bash

        salt '*' file.chattr foo1.txt foo2.txt operator=add attributes=ai
        salt '*' file.chattr foo3.txt operator=remove attributes=i version=2

def chattr(*files, **kwargs):
    '''
    .. versionadded:: 2018.3.0

    Change the attributes of files. This function accepts one or more files and
    the following options:

    operator
        Can be wither ``add`` or ``remove``. Determines whether attributes
        should be added or removed from files

    attributes
        One or more of the following characters: ``aAcCdDeijPsStTu``,
        representing attributes to add to/remove from files

    version
        a version number to assign to the file(s)

    flags
        One or more of the following characters: ``RVf``, representing
        flags to assign to chattr (recurse, verbose, suppress most errors)

    CLI Example:

    .. code-block:: bash

        salt '*' file.chattr foo1.txt foo2.txt operator=add attributes=ai
        salt '*' file.chattr foo3.txt operator=remove attributes=i version=2
    '''
    operator = kwargs.pop('operator', None)
    attributes = kwargs.pop('attributes', None)
    flags = kwargs.pop('flags', None)
    version = kwargs.pop('version', None)

    if (operator is None) or (operator not in ('add', 'remove')):
        raise SaltInvocationError(
            "Need an operator: 'add' or 'remove' to modify attributes.")
    if attributes is None:
        raise SaltInvocationError("Need attributes: [aAcCdDeijPsStTu]")

    cmd = ['chattr']

    if operator == "add":
        attrs = '+{0}'.format(attributes)
    elif operator == "remove":
        attrs = '-{0}'.format(attributes)

    cmd.append(attrs)

    if flags is not None:
        cmd.append('-{0}'.format(flags))

    if version is not None:
        cmd.extend(['-v', version])

    cmd.extend(files)

    result = __salt__['cmd.run'](cmd, python_shell=False)

    if bool(result):
        raise CommandExecutionError(
            "chattr failed to run, possibly due to bad parameters.")

    return True