import logging
import os

from inputs.constants import LOGSTASH_CONFIG_DIR
from inputs.models import LogInput

_log = logging.getLogger(__name__)


def remove_all_log_inputs() -> None:
    """Remove all log input and config files"""
    _log.info('Remove all log inputs')
    all_log_inputs = LogInput.objects.all()
    config_file_names = set(log_input.config_file_name for log_input in all_log_inputs if log_input.config_file_name)
    all_log_inputs.delete()
    for file_name in config_file_names:
        try:
            os.remove(os.path.join(LOGSTASH_CONFIG_DIR, file_name))
        except IOError:
            continue
