<commit_before>package org.rundeck.client.tool;

import com.lexicalscope.jewel.cli.CliFactory;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.rundeck.client.api.RundeckApi;
import org.rundeck.client.api.model.*;
import org.rundeck.client.tool.options.JobListOptions;
import org.rundeck.client.tool.options.JobLoadOptions;
import org.rundeck.client.tool.options.JobPurgeOptions;
import org.rundeck.client.util.Client;
import org.rundeck.client.util.Util;
import retrofit2.Call;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by greg on 3/28/16.
 */
public class Jobs {

    public static final String UUID_REMOVE = "remove";
    public static final String UUID_PRESERVE = "preserve";

    public static void main(String[] args) throws IOException {
        Client<RundeckApi> client = App.createClient();
        String[] actions = new String[]{"list", "load", "purge"};
        boolean success = true;
        if ("list".equals(args[0])) {
            list(App.tail(args), client);
        } else if ("load".equals(args[0])) {
            success = load(App.tail(args), client);
        } else if ("purge".equals(args[0])) {
            success = purge(App.tail(args), client);
        } else {

            throw new IllegalArgumentException(String.format("Unrecognized action: %s, expected one of %s", args[0],
                                                             Arrays.asList(actions)
            ));
        }
        if (!success) {
            System.exit(2);
        }
    }

    private static boolean purge(final String[] args, final Client<RundeckApi> client) throws IOException {
        JobPurgeOptions options = CliFactory.parseArguments(JobPurgeOptions.class, args);

        //if id,idlist specified, use directly
        //otherwise query for the list and assemble the ids

        //TODO: if file specified, write to file

        List<String> ids = new ArrayList<>();
        if (options.isIdlist()) {
            ids = Arrays.asList(options.getIdlist().split("\\s*,\\s*"));
        } else {
            Call<List<JobItem>> listCall;
            listCall = client.getService().listJobs(
                    options.getProject(),
                    options.getJob(),
                    options.getGroup()
            );
            List<JobItem> body = client.checkError(listCall);
            for (JobItem jobItem : body) {
                ids.add(jobItem.getId());
            }
        }

        DeleteJobsResult deletedJobs = client.checkError(client.getService().deleteJobs(ids));

        if (deletedJobs.isAllsuccessful()) {
            System.out.printf("%d Jobs were deleted", deletedJobs.getRequestCount());
            return true;
        }
        System.out.printf("Failed to delete %d Jobs%n", deletedJobs.getFailed().size());
        for (DeleteJob deleteJob : deletedJobs.getFailed()) {
            System.out.println("* " + deleteJob.toBasicString());
        }
        return false;
    }

    private static boolean load(final String[] args, final Client<RundeckApi> client) throws IOException {
        JobLoadOptions options = CliFactory.parseArguments(JobLoadOptions.class, args);
        if (!options.isFile()) {
            throw new IllegalArgumentException("-f is required");
        }
        File input = options.getFile();
        if (!input.canRead() || !input.isFile()) {
            throw new IllegalArgumentException(String.format("File is not readable or does not exist: %s", input));
        }

        RequestBody requestBody = RequestBody.create(
                "xml".equals(options.getFormat()) ? App.MEDIA_TYPE_XML : App.MEDIA_TYPE_YAML,
                input
        );

        Call<ImportResult> importResultCall = client.getService().loadJobs(
                options.getProject(),<commit_msg>add -f support for jobs purge<commit_after>package org.rundeck.client.tool;

import com.lexicalscope.jewel.cli.CliFactory;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.rundeck.client.api.RundeckApi;
import org.rundeck.client.api.model.*;
import org.rundeck.client.tool.options.JobListOptions;
import org.rundeck.client.tool.options.JobLoadOptions;
import org.rundeck.client.tool.options.JobPurgeOptions;
import org.rundeck.client.util.Client;
import org.rundeck.client.util.Util;
import retrofit2.Call;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by greg on 3/28/16.
 */
public class Jobs {

    public static final String UUID_REMOVE = "remove";
    public static final String UUID_PRESERVE = "preserve";

    public static void main(String[] args) throws IOException {
        Client<RundeckApi> client = App.createClient();
        String[] actions = new String[]{"list", "load", "purge"};
        boolean success = true;
        if ("list".equals(args[0])) {
            list(App.tail(args), client);
        } else if ("load".equals(args[0])) {
            success = load(App.tail(args), client);
        } else if ("purge".equals(args[0])) {
            success = purge(App.tail(args), client);
        } else {

            throw new IllegalArgumentException(String.format("Unrecognized action: %s, expected one of %s", args[0],
                                                             Arrays.asList(actions)
            ));
        }
        if (!success) {
            System.exit(2);
        }
    }

    private static boolean purge(final String[] args, final Client<RundeckApi> client) throws IOException {
        JobPurgeOptions options = CliFactory.parseArguments(JobPurgeOptions.class, args);

        //if id,idlist specified, use directly
        //otherwise query for the list and assemble the ids

        List<String> ids = new ArrayList<>();
        if (options.isIdlist()) {
            ids = Arrays.asList(options.getIdlist().split("\\s*,\\s*"));
        } else {
            if(!options.isJob() && !options.isGroup()){
                throw new IllegalArgumentException("must specify -i, or -j/-g to specify jobs to delete.");
            }
            Call<List<JobItem>> listCall;
            listCall = client.getService().listJobs(
                    options.getProject(),
                    options.getJob(),
                    options.getGroup()
            );
            List<JobItem> body = client.checkError(listCall);
            for (JobItem jobItem : body) {
                ids.add(jobItem.getId());
            }
        }

        if(options.isFile()){
            list(args, client);
        }

        DeleteJobsResult deletedJobs = client.checkError(client.getService().deleteJobs(ids));

        if (deletedJobs.isAllsuccessful()) {
            System.out.printf("%d Jobs were deleted%n", deletedJobs.getRequestCount());
            return true;
        }
        System.out.printf("Failed to delete %d Jobs%n", deletedJobs.getFailed().size());
        for (DeleteJob deleteJob : deletedJobs.getFailed()) {
            System.out.println("* " + deleteJob.toBasicString());
        }
        return false;
    }

    private static boolean load(final String[] args, final Client<RundeckApi> client) throws IOException {
        JobLoadOptions options = CliFactory.parseArguments(JobLoadOptions.class, args);
        if (!options.isFile()) {
            throw new IllegalArgumentException("-f is required");
        }
        File input = options.getFile();
        if (!input.canRead() || !input.isFile()) {
            throw new IllegalArgumentException(String.format("File is not readable or does not exist: %s", input));
        }

        RequestBody requestBody = RequestBody.create(
                "xml".equals(options.getFormat()) ? App.MEDIA_TYPE_XML : App.MEDIA_TYPE_YAML,
                input
        );

        Call<ImportResult> importResultCall = client.getService().loadJobs(
                options.getProject(),
{'fraction_non_alphanumeric': 0.12268080272624006, 'fraction_numerical': 0.0032815852581093022, 'mean_word_length': 2.3604749787955894, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6712339', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2137, 'n_words': 492}