package io.qameta.allure.command;

import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option;
import io.qameta.allure.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.nio.file.Path;
import java.nio.file.Paths;

import static io.qameta.allure.utils.CommandUtils.copyDirectory;
import static io.qameta.allure.utils.CommandUtils.createMain;

/**
 * @author Artem Eroshenko <eroshenkoam@qameta.io>
 */
@Command(name = "generate", description = "Generate report")
public class ReportGenerate implements AllureCommand {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReportGenerate.class);

    @Inject
    private ResultsOptions resultsOptions = new ResultsOptions();

    @Option(
            title = "Report directory",
            name = {"-o", "--report-dir", "--output"},
            description = "The directory to generate Allure report into.")
    protected String reportDirectory = "allure-report";

    @Inject
    private VerboseOptions verboseOptions = new VerboseOptions();

    @Override
    public void run(Context context) throws Exception {
        verboseOptions.configureLogLevel();
        Path output = Paths.get(reportDirectory);
        Main main = createMain(context);
        main.generate(output, resultsOptions.getResultsDirectories());

        copyDirectory(context.getWebDirectory(), output);
        LOGGER.info("Report successfully generated to the directory <{}>. " +
                "Use `allure open` command to show the report.", reportDirectory);
    }
}

@@ -165,16 +165,61 @@
 e.Main;%0A
+import io.qameta.allure.utils.DeleteVisitor;%0A
 import o
@@ -235,16 +235,16 @@
 Logger;%0A
-
 import o
@@ -296,16 +296,44 @@
 Inject;%0A
+import java.nio.file.Files;%0A
 import j
@@ -848,24 +848,253 @@
 Options();%0A%0A
+    @Option(%0A            title = %22Clean report directory%22,%0A            name = %7B%22-c%22, %22--clean%22%7D,%0A            description = %22Clean Allure report directory before generate new one.%22%0A    )%0A    private boolean cleanReportDirectory;%0A%0A
     @Option(
@@ -1514,16 +1514,16 @@
 evel();%0A
-
         
@@ -1560,24 +1560,157 @@
 Directory);%0A
+%0A        if (cleanReportDirectory && Files.exists(output)) %7B%0A            Files.walkFileTree(output, new DeleteVisitor());%0A        %7D%0A%0A
         Main

add an ability to delete report directory before generation (fixes #133, via #173)