diff options
| author | doufenghu <[email protected]> | 2024-01-31 18:00:36 +0800 |
|---|---|---|
| committer | doufenghu <[email protected]> | 2024-01-31 18:00:36 +0800 |
| commit | dae014360975d4716c6ac87754a798a801636d21 (patch) | |
| tree | 6dcbadb90dea957b2e92abc82cc5250c368e4ec0 /groot-bootstrap | |
| parent | c13784a15bbad455998a48d3ed5e617261212159 (diff) | |
[Improve][bootstrap] Add ConfigBuilder for better get config object of yaml.
Diffstat (limited to 'groot-bootstrap')
4 files changed, 57 insertions, 17 deletions
diff --git a/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfDecryptCommand.java b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfDecryptCommand.java index d923871..75f7819 100644 --- a/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfDecryptCommand.java +++ b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfDecryptCommand.java @@ -3,6 +3,8 @@ package com.geedgenetworks.bootstrap.command; import cn.hutool.setting.yaml.YamlUtil; import com.geedgenetworks.bootstrap.exception.CommandExecuteException; import com.geedgenetworks.bootstrap.exception.ConfigCheckException; +import com.geedgenetworks.bootstrap.utils.ConfigBuilder; +import com.geedgenetworks.bootstrap.utils.ConfigFileUtils; import com.geedgenetworks.bootstrap.utils.ConfigShadeUtils; import com.typesafe.config.*; import lombok.extern.slf4j.Slf4j; @@ -24,13 +26,10 @@ public class ConfDecryptCommand implements Command<ExecuteCommandArgs>{ @Override public void execute() throws CommandExecuteException, ConfigCheckException { - String decryptConfigFile = executeCommandArgs.getConfigFile(); - Path configPath = Paths.get(decryptConfigFile); + Path configPath = ConfigFileUtils.getConfigPath(executeCommandArgs); checkConfigExist(configPath); Map<String, Object> configMap = YamlUtil.loadByPath(configPath.toString()); - ConfigObject configObject = ConfigValueFactory.fromMap(configMap); - Config config = configObject.toConfig(); - Config decryptConfig = ConfigShadeUtils.decryptConfig(config); + Config decryptConfig = ConfigBuilder.of(configMap, true); System.out.println(String.format( "Decrypt config: %s", decryptConfig.root().render(ConfigRenderOptions.defaults().setOriginComments(false)))); log.info( diff --git a/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfEncryptCommand.java b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfEncryptCommand.java index 86a57ba..676cba5 100644 --- a/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfEncryptCommand.java +++ b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ConfEncryptCommand.java @@ -3,6 +3,8 @@ package com.geedgenetworks.bootstrap.command; import cn.hutool.setting.yaml.YamlUtil; import com.geedgenetworks.bootstrap.exception.CommandExecuteException; import com.geedgenetworks.bootstrap.exception.ConfigCheckException; +import com.geedgenetworks.bootstrap.utils.ConfigBuilder; +import com.geedgenetworks.bootstrap.utils.ConfigFileUtils; import com.geedgenetworks.bootstrap.utils.ConfigShadeUtils; import com.typesafe.config.*; import lombok.extern.slf4j.Slf4j; @@ -11,6 +13,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; +import static com.geedgenetworks.bootstrap.utils.ConfigFileUtils.checkConfigExist; + @Slf4j public class ConfEncryptCommand implements Command<ExecuteCommandArgs>{ private final ExecuteCommandArgs executeCommandArgs; @@ -24,11 +28,10 @@ public class ConfEncryptCommand implements Command<ExecuteCommandArgs>{ log.warn( "When both --decrypt and --encrypt are specified, only --encrypt will take effect"); } - String encryptConfigFile = executeCommandArgs.getConfigFile(); - Path configPath = Paths.get(encryptConfigFile); + Path configPath = ConfigFileUtils.getConfigPath(executeCommandArgs); + checkConfigExist(configPath); Map<String, Object> configMap = YamlUtil.loadByPath(configPath.toString()); - ConfigObject configObject = ConfigValueFactory.fromMap(configMap); - Config config = configObject.toConfig(); + Config config = ConfigBuilder.of(configMap, false); Config encryptConfig = ConfigShadeUtils.encryptConfig(config); System.out.println(String.format( "Encrypt config: %s", encryptConfig.root().render(ConfigRenderOptions.defaults().setOriginComments(false)))); diff --git a/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ExecuteCommand.java b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ExecuteCommand.java index 17bc278..79a27e1 100644 --- a/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ExecuteCommand.java +++ b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/command/ExecuteCommand.java @@ -1,23 +1,20 @@ package com.geedgenetworks.bootstrap.command; -import cn.hutool.setting.yaml.YamlUtil; import com.geedgenetworks.bootstrap.exception.CommandExecuteException; import com.geedgenetworks.bootstrap.exception.ConfigCheckException; import com.geedgenetworks.bootstrap.exception.JobExecuteException; import com.geedgenetworks.bootstrap.execution.ExecutionConfigKeyName; import com.geedgenetworks.bootstrap.execution.JobExecution; +import com.geedgenetworks.bootstrap.utils.ConfigBuilder; import com.geedgenetworks.bootstrap.utils.ConfigFileUtils; -import com.geedgenetworks.bootstrap.utils.ConfigShadeUtils; import com.geedgenetworks.common.Constants; import com.geedgenetworks.common.config.ConfigProvider; import com.geedgenetworks.common.config.GrootStreamConfig; import com.typesafe.config.Config; -import com.typesafe.config.ConfigObject; import com.typesafe.config.ConfigUtil; import com.typesafe.config.ConfigValueFactory; import lombok.extern.slf4j.Slf4j; import java.nio.file.Path; -import java.util.Map; import static com.geedgenetworks.bootstrap.utils.ConfigFileUtils.checkConfigExist; @Slf4j public class ExecuteCommand implements Command<ExecuteCommandArgs> { @@ -33,10 +30,7 @@ public class ExecuteCommand implements Command<ExecuteCommandArgs> { Path configFile = ConfigFileUtils.getConfigPath(executeCommandArgs); // check config file exist checkConfigExist(configFile); - Map<String, Object> configMap = YamlUtil.loadByPath(configFile.toString()); - ConfigObject configObject = ConfigValueFactory.fromMap(configMap); - Config config = configObject.toConfig(); - config = ConfigShadeUtils.decryptConfig(config); + Config config = ConfigBuilder.of(configFile); // if user specified job name using command line arguments, override config option if (!executeCommandArgs.getJobName().equals(Constants.DEFAULT_JOB_NAME)) { config = config.withValue( diff --git a/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/utils/ConfigBuilder.java b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/utils/ConfigBuilder.java new file mode 100644 index 0000000..954c058 --- /dev/null +++ b/groot-bootstrap/src/main/java/com/geedgenetworks/bootstrap/utils/ConfigBuilder.java @@ -0,0 +1,44 @@ +package com.geedgenetworks.bootstrap.utils; + +import cn.hutool.setting.yaml.YamlUtil; +import com.typesafe.config.*; +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +@Slf4j +public class ConfigBuilder { + private ConfigBuilder() { + throw new IllegalStateException("Utility class and cannot be instantiated"); + } + + public static Config of(@NonNull String filePath) { + Path path = Paths.get(filePath); + return of(path); + } + + public static Config of(@NonNull Path filePath) { + log.info("Loading config file from path: {}", filePath); + Map<String, Object> configMap = YamlUtil.loadByPath(filePath.toString()); + ConfigObject configObject = ConfigValueFactory.fromMap(configMap); + Config config = configObject.toConfig(); + return ConfigShadeUtils.decryptConfig(config); + } + + public static Config of(@NonNull Map<String, Object> objectMap) { + return of(objectMap, false); + } + + public static Config of(@NonNull Map<String, Object> objectMap, boolean needDecrypt) { + ConfigObject configObject = ConfigValueFactory.fromMap(objectMap); + Config config = configObject.toConfig(); + if (needDecrypt) { + return ConfigShadeUtils.decryptConfig(config); + } + return config; + } + +} |
