# Buggy code
def launchctl(sub_cmd, *args, **kwargs):
    """
    Run a launchctl command and raise an error if it fails

    Args: additional args are passed to launchctl
        sub_cmd (str): Sub command supplied to launchctl

    Kwargs: passed to ``cmd.run_all``
        return_stdout (bool): A keyword argument. If true return the stdout of
            the launchctl command

    Returns:
        bool: ``True`` if successful
        str: The stdout of the launchctl command if requested

    Raises:
        CommandExecutionError: If command fails

    CLI Example:

    .. code-block:: bash

        import salt.utils.mac_service
        salt.utils.mac_service.launchctl('debug', 'org.cups.cupsd')
    """
    # Get return type
    return_stdout = kwargs.pop("return_stdout", False)

    # Construct command
    cmd = ["launchctl", sub_cmd]
    cmd.extend(args)

    # Run command
    kwargs["python_shell"] = False
    ret = salt.modules.cmdmod.run_all(cmd, **kwargs)

    # Raise an error or return successful result
    if ret["retcode"]:
        out = "Failed to {0} service:\n".format(sub_cmd)
        out += "stdout: {0}\n".format(ret["stdout"])
        out += "stderr: {0}\n".format(ret["stderr"])
        out += "retcode: {0}".format(ret["retcode"])
        raise CommandExecutionError(out)
    else:
        return ret["stdout"] if return_stdout else True


# Traceback (NameError)
Traceback (most recent call last):
File "/testing/scripts/salt-call", line 11, in <module>
salt_call()
File "/testing/salt/scripts.py", line 400, in salt_call
client.run()
File "/testing/salt/cli/call.py", line 57, in run
caller.run()
File "/testing/salt/cli/caller.py", line 134, in run
ret = self.call()
File "/testing/salt/cli/caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "/testing/salt/modules/mac_service.py", line 130, in launchctl
return salt.utils.mac_utils.launchctl(sub_cmd, *args, **kwargs)
File "/testing/salt/utils/mac_utils.py", line 270, in launchctl
ret = salt.modules.cmdmod.run_all(cmd, **kwargs)
File "/testing/salt/modules/cmdmod.py", line 1834, in run_all
**kwargs)
File "/testing/salt/modules/cmdmod.py", line 357, in _run
if not _check_avail(cmd):
File "/testing/salt/modules/cmdmod.py", line 230, in _check_avail
if __salt__['config.get']('cmd_blacklist_glob'):
NameError: global name '__salt__' is not defined

# Fixed code
def launchctl(sub_cmd, *args, **kwargs):
    """
    Run a launchctl command and raise an error if it fails

    Args: additional args are passed to launchctl
        sub_cmd (str): Sub command supplied to launchctl

    Kwargs: passed to ``cmd.run_all``
        return_stdout (bool): A keyword argument. If true return the stdout of
            the launchctl command

    Returns:
        bool: ``True`` if successful
        str: The stdout of the launchctl command if requested

    Raises:
        CommandExecutionError: If command fails

    CLI Example:

    .. code-block:: bash

        import salt.utils.mac_service
        salt.utils.mac_service.launchctl('debug', 'org.cups.cupsd')
    """
    # Get return type
    return_stdout = kwargs.pop("return_stdout", False)

    # Construct command
    cmd = ["launchctl", sub_cmd]
    cmd.extend(args)

    # Run command
    kwargs["python_shell"] = False
    kwargs = salt.utils.args.clean_kwargs(**kwargs)
    ret = __salt__["cmd.run_all"](cmd, **kwargs)

    # Raise an error or return successful result
    if ret["retcode"]:
        out = "Failed to {0} service:\n".format(sub_cmd)
        out += "stdout: {0}\n".format(ret["stdout"])
        out += "stderr: {0}\n".format(ret["stderr"])
        out += "retcode: {0}".format(ret["retcode"])
        raise CommandExecutionError(out)
    else:
        return ret["stdout"] if return_stdout else True