Skip to content

Commit

Permalink
Refactoring for better file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Feb 5, 2024
1 parent 6a96789 commit 185ed14
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
2 changes: 0 additions & 2 deletions convex-cli/src/main/java/convex/cli/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
Expand Down
11 changes: 6 additions & 5 deletions convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,10 @@ public char[] getKeyPassword() {
* Gets the keystore file name currently used for the CLI
* @return File name, or null if not specified
*/
public String getKeyStoreFilename() {
public File getKeyStoreFile() {
if ( keyStoreFilename != null) {
return Helpers.expandTilde(keyStoreFilename).strip();
File f= Utils.getPath(keyStoreFilename);
return f;
}
return null;
}
Expand Down Expand Up @@ -318,7 +319,7 @@ public KeyStore loadKeyStore() {
* @return KeyStore instance, or null if does not exist
*/
public KeyStore loadKeyStore(boolean isCreate, char[] password) {
File keyFile = new File(getKeyStoreFilename());
File keyFile = getKeyStoreFile();
try {
if (keyFile.exists()) {
keyStore = PFXTools.loadStore(keyFile, password);
Expand Down Expand Up @@ -358,7 +359,7 @@ public AKeyPair loadKeyFromStore(String publicKey) {

char[] storePassword=getStorePassword();

File keyFile = new File(getKeyStoreFilename());
File keyFile = getKeyStoreFile();
try {
if (!keyFile.exists()) {
throw new CLIError("Cannot find keystore file "+keyFile.getCanonicalPath());
Expand Down Expand Up @@ -431,7 +432,7 @@ public void saveKeyStore(char[] storePassword) {
// save the keystore file
if (keyStore==null) throw new CLIError("Trying to save a keystore that has not been loaded!");
try {
PFXTools.saveStore(keyStore, new File(getKeyStoreFilename()), storePassword);
PFXTools.saveStore(keyStore, getKeyStoreFile(), storePassword);
} catch (Throwable t) {
throw Utils.sneakyThrow(t);
}
Expand Down
8 changes: 0 additions & 8 deletions convex-cli/src/main/java/convex/cli/peer/APeerCommand.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package convex.cli.peer;

import convex.cli.Helpers;
import convex.cli.Main;
import picocli.CommandLine.ParentCommand;

public abstract class APeerCommand implements Runnable {

@ParentCommand
private Peer peerParent;

public String getEtchStoreFilename() {
if ( peerParent.etchStoreFilename != null) {
return Helpers.expandTilde(peerParent.etchStoreFilename).strip();
}
return null;
}

protected Main cli() {
return peerParent.cli();
Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/peer/PeerCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void run() {
// save the new keypair in the keystore
PFXTools.setKeyPair(keyStore, keyPair, mainParent.getKeyPassword());

File keyFile = new File(mainParent.getKeyStoreFilename());
File keyFile = mainParent.getKeyStoreFile();

// save the store to a file
PFXTools.saveStore(keyStore, keyFile, mainParent.getStorePassword());
Expand Down
26 changes: 17 additions & 9 deletions convex-core/src/main/java/convex/core/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,34 @@ public static BigInteger toSignedBigInteger(byte[] data) {
}

/**
* Create a path if necessary to a File object. Interprets leading "~" as user home directory
* Create a path if necessary to a File object. Interprets leading "~" as user home directory.
*
* @param file File object to see if the path part of the filename exists, if not then create it.
* @return target File, as an absolute path, with parent directories created recursively if needed
* @throws IOException
*/
public static File ensurePath(File file) throws IOException {
String path=file.getPath();
if (path!=null && path.startsWith("~")) {
path=System.getProperty("user.home")+path.substring(1);
} else {
path=file.getAbsolutePath();
}

// Get path of parent directory, using absolute path (may be current working directory user.dir)
File target=new File(path);
File target=getPath(file.getPath());
String dirPath=target.getParent();
Files.createDirectories(Path.of(dirPath));
return target;
}

/**
* Gets the absolute path File for a given file name. Interprets leading "~" as user home directory.
* @param filename
* @return
*/
public static File getPath(String path) {
if (path!=null && path.startsWith("~")) {
path=System.getProperty("user.home")+path.substring(1);
return new File(path);
} else {
path=new File(path).getAbsolutePath();
return new File(path);
}
}

/**
* Converts an int to a hex string e.g. "80cafe80"
Expand Down

0 comments on commit 185ed14

Please sign in to comment.