Skip to content

Commit

Permalink
Use system config directory for Quark's config file
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaSajt committed Sep 14, 2023
1 parent 5c9dda3 commit 39a1c75
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
27 changes: 15 additions & 12 deletions Quark/src/main/java/xortroll/goldleaf/quark/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,34 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class Config {
public static String ConfigPathName = "quark-config.cfg";
public static String ConfigPath = ConfigPathName;
public static Path ConfigPath;
private Properties inner_cfg;
private File cfg_file;

static {
try {
ConfigPath = Paths.get(new File(Config.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile().getPath(), ConfigPathName).toString();
}
catch(Exception e) {
ConfigPath = ConfigPathName;
}
}

public Config() throws Exception {
if(ConfigPath == null) {
String configHome = System.getenv("XDG_CONFIG_HOME");
if (configHome == null || configHome.trim().length() == 0) {
configHome = System.getProperty("user.home") + File.separator + ".config";
}
Path quarkConfigDirPath = Paths.get(configHome, "quark");
if (Files.notExists(quarkConfigDirPath)) {
Files.createDirectories(quarkConfigDirPath);
}
ConfigPath = quarkConfigDirPath.resolve("quark-config.cfg");
}
this.inner_cfg = new Properties();
reloadConfigFile();
}

public void reloadConfigFile() throws Exception {
this.cfg_file = Paths.get(ConfigPath).toFile();
this.cfg_file = ConfigPath.toFile();
if(this.cfg_file.isFile()) {
this.inner_cfg.load(new FileInputStream(this.cfg_file));
}
Expand Down
3 changes: 2 additions & 1 deletion Quark/src/main/java/xortroll/goldleaf/quark/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package xortroll.goldleaf.quark;

import java.nio.file.Paths;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand All @@ -38,7 +39,7 @@ public static void main(String[] args) {
try {
CommandLine cmd = parser.parse(options, args);
if(cmd.hasOption(ConfigFileOption)) {
Config.ConfigPath = cmd.getOptionValue(ConfigFileOption);
Config.ConfigPath = Paths.get(cmd.getOptionValue(ConfigFileOption));
}
}
catch(Exception e) {
Expand Down

0 comments on commit 39a1c75

Please sign in to comment.