Skip to content

Commit

Permalink
Implement continue command
Browse files Browse the repository at this point in the history
  • Loading branch information
jonx8 committed Aug 21, 2023
1 parent 455a4ee commit 5802b87
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 78 deletions.
9 changes: 7 additions & 2 deletions src/main/java/ru/etu/petci/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.etu.petci;

import ru.etu.petci.handlers.CommandHandler;
import ru.etu.petci.handlers.ContinueCommandHandler;
import ru.etu.petci.handlers.InitCommandHandler;

import java.io.IOException;
Expand All @@ -10,7 +11,7 @@

public class Main {
public static final String SETTINGS_FILE_NAME = "settings.properties";
public static String JOBS_DIR_NAME = "";
public static final String JOBS_DIR_NAME = "";
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());

static {
Expand Down Expand Up @@ -38,7 +39,11 @@ public static void main(String[] args) {
handler = new InitCommandHandler();
exitStatus = handler.handle(args.length > 1 ? args[1] : "");
}
case "continue" -> LOGGER.info("User entered \"continue\" command.");
case "continue" -> {
LOGGER.info("User entered \"continue\" command.");
handler = new ContinueCommandHandler();
exitStatus = handler.handle("");
}
case "add" -> LOGGER.info("User entered \"add\" command.");
default -> {
System.out.printf("petCI: \"%s\" is not a command. Use \"help\" command%n", args[0]);
Expand Down
66 changes: 28 additions & 38 deletions src/main/java/ru/etu/petci/configuration/Configurator.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,57 @@
package ru.etu.petci.configuration;


import ru.etu.petci.jobs.Job;
import ru.etu.petci.observers.RepositoryObserver;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.io.*;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Properties;

import static ru.etu.petci.Main.JOBS_DIR_NAME;
import static ru.etu.petci.Main.SETTINGS_FILE_NAME;

public class Configurator {


public RepositoryObserver readRepositoryConfig() {
public RepositoryObserver readRepositoryConfig() throws IOException {
RepositoryObserver observer = new RepositoryObserver();
try {
Properties repoConfig = new Properties();
repoConfig.load(new FileReader("repository.properties"));
if (!observer.setRepositoryPath(repoConfig.getProperty("repository_path"))) {
System.exit(1);
}
Properties repoConfig = new Properties();
try (var propertyReader = new FileReader("repository.properties")) {
repoConfig.load(propertyReader);
observer.setRepositoryPath(repoConfig.getProperty("repository_path"));
observer.setBranchName(repoConfig.getProperty("branch_name", "master"));
observer.setLastHash(repoConfig.getProperty("last_hash", null));
} catch (IOException e) {
System.out.println("Error while reading repository config");
System.exit(1);
observer.setLastHash(repoConfig.getProperty("last_hash"));
}
return observer;
}


public void saveRepositoryConfig(String repoPath, String branchName, String lastHash) {
try {
var properties = new Properties();
properties.setProperty("repository_path", Path.of(repoPath).toAbsolutePath().normalize().toString());
properties.setProperty("branch_name", branchName);
properties.setProperty("last_hash", lastHash.length() == 40 ? lastHash : "null");
properties.store(new FileWriter("repository.properties"), "Repository configuration");
} catch (IOException e) {
throw new RuntimeException(e);
public void saveRepositoryConfig(String repoPath, String branchName, String lastHash) throws IOException {
var properties = new Properties();
properties.setProperty("repository_path", Path.of(repoPath).toAbsolutePath().normalize().toString());
properties.setProperty("branch_name", branchName);
properties.setProperty("last_hash", lastHash.length() == 40 ? lastHash : "null");
try (var propertyWriter = new FileWriter("repository.properties")) {
properties.store(propertyWriter, "Repository configuration");
}
}

public void saveRepositoryConfig(String repoPath, String branchName) {
public void saveRepositoryConfig(String repoPath, String branchName) throws IOException {
saveRepositoryConfig(repoPath, branchName, "");
}


/**
* <p>This method performs the check whether the current folder
* is an application folder. An application folder must consist
* of a configuration file with name {@link}
* and a directory with name {@link}.</p>
* <p>If there is no application directory, then the method returned false,
* user should use "init" command.</p>
*/
public boolean checkApplicationDir() {
return Files.isDirectory(Path.of(JOBS_DIR_NAME)) && Files.isRegularFile(Path.of(SETTINGS_FILE_NAME));
public void saveJobConfig(Job job) {
Objects.requireNonNull(job);
try (var out = new BufferedWriter(new FileWriter(JOBS_DIR_NAME + File.separator + job.getName()))) {
out.append(job.getName());
out.newLine();
out.append(job.getScriptFile().toString());
out.newLine();
out.append(String.valueOf(job.isActive()));
} catch (IOException e) {
System.out.println("Error with writing jobFile");
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.etu.petci.exceptions;

public class RepositoryNotFoundException extends Exception {
public RepositoryNotFoundException(String message) {
super(message);
}
}
2 changes: 1 addition & 1 deletion src/main/java/ru/etu/petci/handlers/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface CommandHandler {
/**
* This method should handle a certain command which is entered by user.
*
* @param arg argument after name of the command. You should check if arg is null.
* @param arg argument after name of the command. Arg is NotNull.
* @return exit code for the program
*/
int handle(String arg);
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.etu.petci.handlers;


import ru.etu.petci.configuration.Configurator;
import ru.etu.petci.exceptions.RepositoryNotFoundException;
import ru.etu.petci.jobs.Job;
import ru.etu.petci.jobs.JobsExecutor;
import ru.etu.petci.observers.RepositoryObserver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ContinueCommandHandler implements CommandHandler {

private static final Logger LOGGER;

static {
LOGGER = Logger.getLogger(ContinueCommandHandler.class.getName());
LOGGER.setLevel(Level.WARNING);
}

@Override
public int handle(String arg) {
Objects.requireNonNull(arg);
var configurator = new Configurator();
try {
RepositoryObserver repositoryObserver = configurator.readRepositoryConfig();
List<Job> jobs = new ArrayList<>();
// TODO Reading jobs from properties
repositoryObserver.setExecutor(new JobsExecutor(jobs));
repositoryObserver.start();
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, "The program was interrupted");
Thread.currentThread().interrupt();
} catch (IOException | RepositoryNotFoundException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
return 1;
}
return 1;
}
}
14 changes: 10 additions & 4 deletions src/main/java/ru/etu/petci/handlers/InitCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ru.etu.petci.configuration.Configurator;

import java.io.IOException;
import java.util.Objects;
import java.util.Scanner;
import java.util.logging.Level;
Expand Down Expand Up @@ -37,10 +38,15 @@ public int handle(String arg) {
// If branchName is empty, using master-branch
System.out.print("Name of observing branch (default - master): ");
var branchName = scanner.nextLine().strip();
if (branchName.isEmpty()) {
configurator.saveRepositoryConfig(repositoryPath, "master");
} else {
configurator.saveRepositoryConfig(repositoryPath, branchName);
try {
if (branchName.isEmpty()) {
configurator.saveRepositoryConfig(repositoryPath, "master");
} else {
configurator.saveRepositoryConfig(repositoryPath, branchName);
}
} catch (IOException e) {
LOGGER.severe(e.getMessage());
return 1;
}
scanner.close();
System.out.println("Successful initialization!");
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/ru/etu/petci/jobs/Job.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.etu.petci.jobs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Objects;

public class Job {
private boolean isActive = true;
private String name;
private final Path scriptFile;

public Job(Path scriptFile, String name) {
this.scriptFile = scriptFile.toAbsolutePath().normalize();
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Path getScriptFile() {
return scriptFile;
}


public void setActive(boolean active) {
isActive = active;
}

public boolean isActive() {
return isActive;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Job job)) return false;
return Objects.equals(name, job.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}


public int execute() throws IOException {
Process scriptProcess = Runtime.getRuntime().exec(scriptFile.toString());
try {
var input = new BufferedReader(new InputStreamReader(scriptProcess.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
scriptProcess.waitFor();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return scriptProcess.exitValue();
}
}
39 changes: 39 additions & 0 deletions src/main/java/ru/etu/petci/jobs/JobsExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.etu.petci.jobs;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JobsExecutor {

private final List<Job> jobs;
private static final Logger LOGGER = Logger.getLogger(JobsExecutor.class.getName());

static {
LOGGER.setLevel(Level.WARNING);
}

public JobsExecutor(List<Job> jobs) {
this.jobs = jobs;
}

public void runJobs() throws IOException {
Objects.requireNonNull(jobs);
for (Job job : jobs) {
System.out.println(job.getScriptFile().toAbsolutePath());
System.out.printf("Run job \"%s\"...%n%n", job.getName());
if (!job.isActive()) {
System.out.println(": Deactivated");
continue;
}
if (job.execute() == 0) {
System.out.printf("%n--Succeed--%n");
} else {
System.out.printf("%n--Failed--%n");
}
}
}

}
Loading

0 comments on commit 5802b87

Please sign in to comment.