Skip to content

Commit

Permalink
must build default json config with default values (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
mageddo authored Aug 7, 2024
1 parent 2faab1f commit c464070
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import com.mageddo.dnsproxyserver.config.Config;
import com.mageddo.dnsproxyserver.config.dataprovider.mapper.ConfigJsonV2Mapper;
import com.mageddo.utils.Files;
import com.mageddo.utils.Runtime;
import com.mageddo.utils.Tests;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

@Slf4j
Expand All @@ -26,7 +23,7 @@ public class ConfigDAOJson implements ConfigDAO {
public Config find() {
final var workDir = this.configDAOEnv.findRaw().getCurrentPath();
final var relativeConfigFilePath = this.configDAOCmdArgs.findRaw().getConfigPath();
final var configFileAbsolutePath = buildConfigPath(workDir, relativeConfigFilePath);
final var configFileAbsolutePath = ConfigPathBuilder.build(workDir, relativeConfigFilePath);
return this.find(configFileAbsolutePath);
}

Expand All @@ -36,27 +33,6 @@ public Config find(Path configPath) {
return ConfigJsonV2Mapper.toConfig(jsonConfig, configPath);
}

public static Path buildConfigPath(Path workDir, String configPath) {
if (runningInTestsAndNoCustomConfigPath()) {
return Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json");
}
if (workDir != null) {
return workDir
.resolve(configPath)
.toAbsolutePath()
;
}
final var confRelativeToCurrDir = Paths
.get(configPath)
.toAbsolutePath();
if (Files.exists(confRelativeToCurrDir)) {
return confRelativeToCurrDir;
}
return Runtime.getRunningDir()
.resolve(configPath)
.toAbsolutePath();
}

static boolean runningInTestsAndNoCustomConfigPath() {
return !Arrays.toString(ConfigDAOCmdArgs.getArgs()).contains("--conf-path") && Tests.inTest();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mageddo.dnsproxyserver.config.dataprovider;

import com.mageddo.utils.Files;
import com.mageddo.utils.Runtime;
import lombok.extern.slf4j.Slf4j;

import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
public class ConfigPathBuilder {
private static Path build0(Path workDir, String configPath) {
if (ConfigDAOJson.runningInTestsAndNoCustomConfigPath()) {
final var file = Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json");
log.trace("status=runningInTests, usingEmptyFile={}", file);
return file;
}
if (workDir != null) {
return workDir
.resolve(configPath)
.toAbsolutePath()
;
}
final var confRelativeToCurrDir = Paths
.get(configPath)
.toAbsolutePath();
if (Files.exists(confRelativeToCurrDir)) {
return confRelativeToCurrDir;
}
return Runtime.getRunningDir()
.resolve(configPath)
.toAbsolutePath();
}

public static Path build(Path workDir, String relativeConfigFilePath) {
final var path = build0(workDir, relativeConfigFilePath);
log.debug("status=configPathBuilt, path={}", path);
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand All @@ -35,7 +34,7 @@ public class JsonConfigs {
public static ConfigJson loadConfig(Path configPath) {

if (!Files.exists(configPath)) {
createDefault(configPath);
createDefaultConfigOnPath(configPath);
}

return loadConfig(Files.readString(configPath));
Expand All @@ -48,7 +47,7 @@ public static ConfigJson loadConfig(String jsonContent) {
final var tree = objectMapper.readTree(jsonContent);
if (tree.isEmpty()) {
log.info("status=emptyConfigFile, action=usingDefault");
return new ConfigJsonV2();
return JsonConfigs.buildDefaultJsonConfig();
}
final var version = findVersion(tree);
return switch (version) {
Expand All @@ -61,23 +60,25 @@ public static ConfigJson loadConfig(String jsonContent) {
}

@SneakyThrows
static void createDefault(Path configPath) {
final var config = new ConfigJsonV2();

static void createDefaultConfigOnPath(Path configPath) {
Files.createDirectories(configPath.getParent()); // ensure directories are created
final var config = buildDefaultJsonConfig();
JsonUtils
.prettyInstance()
.writeValue(configPath.toFile(), config);
log.info("status=createdDefaultConfigFile, path={}", configPath);
}

public static ConfigJsonV2 buildDefaultJsonConfig() {
final var config = new ConfigJsonV2();
config
.get_envs()
.add(
new Env()
.setName(Config.Env.DEFAULT_ENV)
.setHostnames(Collections.singletonList(Entry.sample()))
.add(Entry.sample())
);

JsonUtils
.prettyInstance()
.writeValue(configPath.toFile(), config);
log.info("status=createdDefaultConfigFile, path={}", configPath);
return config;
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void mustBuildConfPathRelativeToWorkDir(@TempDir Path tmpDir){
ConfigDAOCmdArgs.setArgs(flags.getArgs());

// act
final var configPath = ConfigDAOJson.buildConfigPath(workDir, flags.getConfigPath());
final var configPath = ConfigPathBuilder.build(workDir, flags.getConfigPath());

// assert
assertEquals("config.json", configPath.getFileName().toString());
Expand Down

0 comments on commit c464070

Please sign in to comment.