Coverage Summary for Class: Migrator (co.rsk.cli.config)

Class Method, % Line, %
Migrator 0% (0/3) 0% (0/21)
Migrator$MigratorFlags 0% (0/3) 0% (0/5)
Migrator$MigratorOptions 0% (0/4) 0% (0/9)
Total 0% (0/10) 0% (0/35)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2018 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18 package co.rsk.cli.config; 19  20 import co.rsk.cli.CliArg; 21 import co.rsk.cli.OptionalizableCliArg; 22 import com.typesafe.config.*; 23  24 import java.io.IOException; 25 import java.io.Reader; 26 import java.nio.charset.StandardCharsets; 27 import java.nio.file.Files; 28 import java.util.Enumeration; 29 import java.util.Properties; 30  31 public class Migrator { 32  33  private static final String NEW_CONFIG_PREFIX = "[new]"; 34  private static final String MINIMAL_CONFIG_FORMAT = "{%s = %s}"; 35  36  private final MigratorConfiguration configuration; 37  38  public Migrator(MigratorConfiguration configuration) { 39  this.configuration = configuration; 40  } 41  42  public String migrateConfiguration() throws IOException { 43  return migrateConfiguration( 44  Files.newBufferedReader(configuration.getSourceConfiguration(), StandardCharsets.UTF_8), 45  configuration.getMigrationConfiguration() 46  ); 47  } 48  49  public static String migrateConfiguration(Reader sourceReader, Properties migrationConfiguration) { 50  Config migratedConfig = ConfigFactory.parseReader(sourceReader); 51  Enumeration migrationPaths = migrationConfiguration.propertyNames(); 52  while (migrationPaths.hasMoreElements()) { 53  String originalPath = (String) migrationPaths.nextElement(); 54  if (originalPath.startsWith(NEW_CONFIG_PREFIX)) { 55  try { 56  String newConfigPath = originalPath.substring(NEW_CONFIG_PREFIX.length()).trim(); 57  Config newConfiguration = ConfigFactory.parseString(String.format(MINIMAL_CONFIG_FORMAT, newConfigPath, migrationConfiguration.getProperty(originalPath))); 58  migratedConfig = migratedConfig.withFallback(newConfiguration); 59  } catch (ConfigException e) { 60  throw new IllegalArgumentException(String.format("Unable to parse value for the %s property", originalPath), e); 61  } 62  } else if (migratedConfig.hasPath(originalPath)) { 63  ConfigValue configurationValueToMigrate = migratedConfig.getValue(originalPath); 64  migratedConfig = migratedConfig.withValue(migrationConfiguration.getProperty(originalPath), configurationValueToMigrate).withoutPath(originalPath); 65  } 66  } 67  68  return migratedConfig.root().render(ConfigRenderOptions.defaults().setOriginComments(false).setJson(false)); 69  } 70  71  public enum MigratorOptions implements OptionalizableCliArg { 72  INPUT_FILE("i", false), 73  OUTPUT_FILE("o", true), 74  MIGRATION_FILE("m", false), 75  ; 76  77  private final String optionName; 78  private final boolean optional; 79  80  MigratorOptions(String name, boolean optional) { 81  this.optionName = name; 82  this.optional = optional; 83  } 84  85  @Override 86  public boolean isOptional() { 87  return optional; 88  } 89  90  @Override 91  public String getName() { 92  return optionName; 93  } 94  } 95  96  public enum MigratorFlags implements CliArg { 97  REPLACE_IN_PLACE("replace"), 98  ; 99  100  private final String flagName; 101  102  MigratorFlags(String flagName) { 103  this.flagName = flagName; 104  } 105  106  @Override 107  public String getName() { 108  return flagName; 109  } 110  } 111 }