import click

from dirt.libs import incident, log, path, complete

from dirt import hooks


@click.command("export", help="Export incident to .tar.gz archive")
@click.argument("incident_id", default="current", autocompletion=complete.get_completer(complete.incident_list))
@click.pass_context
@hooks.hookable
def shell_export_command(ctx, incident_id):
    current = incident.choose_incident(incident_id)
    if current is None:
        ctx.abort()
    log.shell("tar -zcvf {archive} -C {path} {identifier}",
              archive="dirt-{}.tar.gz".format(current.identifier),
              path=path.get_incidents_path(),
              identifier=current.identifier)


COMMAND = shell_export_command
import click

from dirt.libs import log, complete

from dirt import hooks


@click.command("tag", help="Tags for incident")
@click.argument("tag", required=True, autocompletion=complete.get_completer(complete.tags_list))
@click.pass_context
@hooks.hookable
def meta_add_tag_command(ctx, tag):
    current = ctx.obj["INCIDENT"]
    tag = current.add_tag(tag)
    if tag is None:
        log.error("Invalid tag name! Must start with letter and contain only letters, digits, underscores or dashes")
        ctx.abort()
    current.store()
    log.success("Tagged as {tag}", tag="#" + tag)


@click.command("tag", help="Tags for incident")
@click.argument("tag", required=True, autocompletion=complete.get_completer(complete.tags_list))
@click.pass_context
@hooks.hookable
def meta_remove_tag_command(ctx, tag):
    current = ctx.obj["INCIDENT"]
    current.remove_tag(tag)
    current.store()
    log.success("Untagged as {tag}", tag="#" + tag)
            log.success("Removed custom attribute {name}", name=name)
        else:
            log.success("Removed {value} from custom attribute {name}",
                        value=value,
                        name=name)

    return custom_attribute_handler


@click.group("del",
             help="Remove attribute (indicator) from incident",
             cls=extensions.extendable_group(custom_attribute))
@click.option("--incident_id",
              "-i",
              default="current",
              autocompletion=complete.get_completer(complete.incident_list))
@click.option("--force", "-f", is_flag=True, default=False)
@click.pass_context
@hooks.hookable
def del_command(ctx, incident_id, force):
    current = incident.choose_incident(incident_id)
    if current is None:
        log.error("No incident found.")
        ctx.abort()
    if current.closed and not force:
        log.error("Incident is closed - you must open it before adding items.")
        ctx.abort()
    ctx.obj["INCIDENT"] = current


extensions.register_subcommands(del_command, [attributes] +
            incidents = incident.find_incidents_by_tag(name)
            if not incidents:
                log.error("Incident or tag doesn't exist")
                ctx.abort()
            incidents = [incident.choose_incident(inc) for inc in incidents]
            log.echo("Incidents tagged as #{tag}", tag=name)
            log_incidents(incidents)
        elif len(current) == 1:
            log_incident(current[0])
        else:
            log_incidents(current)

    return show_object_handler


@click.group("show",
             help="Show information about incident(s)",
             cls=extensions.extendable_group(show_object_attribute,
                                             completer=complete.get_completer(
                                                 complete.incident_list,
                                                 complete.tags_list)))
@click.pass_context
@hooks.hookable
def show_command(ctx):
    pass


extensions.register_subcommands(show_command, [current_module], "SUBCOMMAND")

COMMAND = show_command