Skip to content

Commit

Permalink
Corrected issue 48
Browse files Browse the repository at this point in the history
  • Loading branch information
adoble committed Jan 11, 2024
1 parent 8344fc9 commit 042f89d
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 162 deletions.
31 changes: 15 additions & 16 deletions src/main/java/org/doble/adr/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,32 @@ public class Environment {

/** The file system that is being used */
public final FileSystem fileSystem;
/** The output stream used for this program. */

/** The output stream used for this program. */
public final PrintStream out;
/** The error stream used for this program. */

/** The error stream used for this program. */
public final PrintStream err;
/** The input stream used for this program. */

/** The input stream used for this program. */
public final InputStream in;

/** The directory where the tool is running */
public final Path dir;
public final Path dir;

/** The command line to run the editor used for editing the ADRs */
public final String editorCommand;
public final String editorCommand;

/** The runner used for firing up the editor */
public final EditorRunner editorRunner;

/** The author used for the ADRs */
public final String author;

/**
* Private Constructor so that only the builder can be used to
* Private Constructor so that only the builder can be used to
* construct the class.
*
* @param builder The builder used
*/
private Environment(Builder builder) {
Expand All @@ -68,8 +69,6 @@ private Environment(Builder builder) {
this.author = builder.author;
}



public static class Builder {
private FileSystem fileSystem;
private PrintStream outStream;
Expand Down Expand Up @@ -108,12 +107,12 @@ public Builder userDir(String currentDirectory) {
this.currentDirectory = fileSystem.getPath(currentDirectory);
return this;
}

public Builder editorCommand(String editorCommand) {
this.editorCommand = editorCommand;
return this;
}

public Builder editorRunner(EditorRunner editorRunner) {
this.editorRunner = editorRunner;
return this;
Expand Down
91 changes: 48 additions & 43 deletions src/main/java/org/doble/commands/CommandInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,68 +24,75 @@
* @author adoble
*
*/
@Command(name = "init",
description = "Initialise the directory of architecture decision records:\n" +
" * creates a subdirectory of the current working directory\n" +
" * creates the first ADR in that subdirectory, recording the decision to" +
" record architectural decisions with ADRs."
)
@Command(name = "init", description = "Initialise the directory of architecture decision records:\n" +
" * creates a subdirectory of the current working directory\n" +
" * creates the first ADR in that subdirectory, recording the decision to" +
" record architectural decisions with ADRs.")
public class CommandInit implements Callable<Integer> {

@Option(names = { "-t", "-template" }, paramLabel = "TEMPLATE", description = "Template file used for ADRs.")
private String template;
@Option(names = { "-i", "-initial" }, paramLabel = "INITIALTEMPLATE",
description = "A template for the initial ADR created during intialization")
private String initialTemplate;

@Parameters(arity = "0..1", paramLabel = "DOCDIR", description = "The directory to store the ADRs relative to "
+ " the current directory."
+ " Default is ${DEFAULT-VALUE}.")
private String docPath = ADRProperties.defaultDocPath; //TODO change this to type path
@ParentCommand
private CommandADR commandADR;
private String template;

@Option(names = { "-i",
"-initial" }, paramLabel = "INITIALTEMPLATE", description = "A template for the initial ADR created during intialization")
private String initialTemplate;

@Parameters(arity = "0..1", paramLabel = "DOCDIR", description = "The directory to store the ADRs relative to "
+ " the current directory."
+ " Default is ${DEFAULT-VALUE}.")
private String docPath = ADRProperties.defaultDocPath; // TODO change this to type path

@ParentCommand
private CommandADR commandADR;

private Environment env;
private ADRProperties properties;


/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see commands.Command#command(java.lang.String[])
*/
@Override
public Integer call() throws Exception {
int exitCode = 0;
int exitCode = 0;

this.env = commandADR.getEnvironment();
properties = new ADRProperties(env);

if (env.editorCommand == null) {
String msg = "WARNING: Editor for the ADR has not been found in the environment variables.\n"
+ "Have you set the environment variable EDITOR or VISUAL with the editor program you want to use?\n";
env.err.println(msg);
exitCode = ADR.ERRORENVIRONMENT;
}
} else if (env.editorCommand.contains("\"") || env.editorCommand.contains("\'")) {
String msg = "ERROR: Editor for the ADR has been set in the environment variable,\n"
+ "but contains invalid characters such as \" or \'.\n";
env.err.println(msg);
return ADR.ERRORENVIRONMENT;

}

properties.setProperty("docPath", docPath);

DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
properties.setProperty("dateFormat", ADRProperties.defaultDateFormat);

if (template != null) properties.setProperty("templateFile", template.toString());
if (initialTemplate != null) properties.setProperty("initialTemplateFile", initialTemplate.toString());
if (template != null)
properties.setProperty("templateFile", template.toString());
if (initialTemplate != null)
properties.setProperty("initialTemplateFile", initialTemplate.toString());

Path adrPath = env.dir.resolve(".adr");

// Check if the directory has already been initialized
// Check if the directory has already been initialized
if (Files.notExists(adrPath)) {
Files.createDirectories(adrPath);
} else {
env.err.println("Directory is already initialised for ADR.");
return ADR.ERRORGENERAL;
}

// Check that any template file specified really exists
if (template != null) {
Path templatePath = env.fileSystem.getPath(template);
Expand All @@ -96,8 +103,8 @@ public Integer call() throws Exception {
return CommandLine.ExitCode.USAGE;
}
}
// Check that any initial template file specifed really exists

// Check that any initial template file specifed really exists
if (initialTemplate != null) {
Path initialTemplatePath = env.fileSystem.getPath(initialTemplate);
if (!Files.exists(initialTemplatePath)) {
Expand All @@ -108,7 +115,6 @@ public Integer call() throws Exception {
}
}


// Create a properties file
/*
* Path propPath = adrPath.resolve("adr.properties");
Expand All @@ -117,28 +123,27 @@ public Integer call() throws Exception {
* BufferedWriter writer = Files.newBufferedWriter(propPath);
* properties.store(writer, null); writer.close();
*/

properties.store();

// Now create the docs directory which contains the adr directory
Path docsPath = env.dir.resolve(properties.getProperty("docPath"));
env.out.println("Creating ADR directory at " + docsPath);
Files.createDirectories(docsPath);


// If no template is specified and no initial template is specified create
// an initial ADR using the default (Nygard) form
// an initial ADR using the default (Nygard) form
if (template == null && initialTemplate == null) {
Record record = new Record.Builder(docsPath, dateFormatter)
.template("rsrc:" + ADRProperties.defaultInitialTemplateName)
.id(1)
.name("Record architecture decisions")
.author(env.author)
.date(LocalDate.now())
.build();
record.createPeristentRepresentation();
.build();
record.createPeristentRepresentation();
}

// If a template is specified and an initial template is specified create an
// initial ADR using the specified initial template
if (template != null && initialTemplate != null) {
Expand All @@ -152,15 +157,15 @@ public Integer call() throws Exception {

record.createPeristentRepresentation();
}

// If an initial template is specified, but no template give error message
if (template == null && initialTemplate != null) {
env.err.println("ERROR: Initial template [INITIALTEMPLATE] spceified, but no template [TEMPLATE]specified. "
+ "No initial ADR created!");
env.err.println(
"ERROR: Initial template [INITIALTEMPLATE] spceified, but no template [TEMPLATE]specified. "
+ "No initial ADR created!");
env.err.println();
exitCode = CommandLine.ExitCode.USAGE;
}


return exitCode;
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/doble/commands/CommandNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ public Integer call() throws Exception {
// set up the record object
Path docsPath = rootPath.resolve(properties.getProperty("docPath"));

// Check to see if the editor command has been set.
// Check to see if the editor command has been set and is correct.
// Need to recheck here as the the user may have changed the environement
// variable since running an initialisation.
if (env.editorCommand == null) {
String msg = "ERROR: Editor for the ADR has not been found in the environment variables.\n"
+ "Have you set the environment variable EDITOR or VISUAL with the editor program you want to use?\n";
env.err.println(msg);
exitCode = ADR.ERRORENVIRONMENT;
} else if (env.editorCommand.contains("\"") || env.editorCommand.contains("\'")) {
String msg = "ERROR: Editor for the ADR has been set in the environment variable,\n"
+ "but contains invalid characters such as \" or \'.\n";
env.err.println(msg);
return ADR.ERRORENVIRONMENT;

}

// Set up the template file
Expand Down
Loading

0 comments on commit 042f89d

Please sign in to comment.