mirror of
https://github.com/google/nomulus
synced 2026-01-04 20:24:22 +00:00
Here's an alternate approach that I think simplifies the existing code quite a bit. Now instead of doing: RegistryToolEnvironment.loadFromArgs(args); RegistryToolEnvironment.get().setup(); You just do one chained call: RegistryToolEnvironment.parseFromArgs(args).setup(); or call setup() on a known environment constant: RegistryToolEnvironment.ALPHA.setup(); This avoids having loadFromArgs() implicitly set the active env (but *not* do setup) and then having setup() *not* set the active env, both of which were confusing. Now parseFromArgs() is only responsible for parsing from args, and setup() both sets the env as the active one and does the environment variable setup (which also ensures that the RegistryToolEnvironment.instance field doesn't get out of sync with the RegistryEnvironment value). In addition, this CL adds a runCommandInEnvironment() method to CommandTestCase and ensures that the UNITTEST environment is always set before constructing the default command instance. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=117492978
117 lines
4.6 KiB
Java
117 lines
4.6 KiB
Java
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.domain.registry.tools;
|
|
|
|
import static com.google.common.base.Preconditions.checkArgument;
|
|
import static com.google.common.base.Preconditions.checkState;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.domain.registry.config.RegistryEnvironment;
|
|
|
|
/** Enum of production environments, used for the {@code --environment} flag. */
|
|
enum RegistryToolEnvironment {
|
|
PRODUCTION(RegistryEnvironment.PRODUCTION),
|
|
ALPHA(RegistryEnvironment.ALPHA),
|
|
CRASH(RegistryEnvironment.CRASH),
|
|
QA(RegistryEnvironment.QA),
|
|
SANDBOX(RegistryEnvironment.SANDBOX),
|
|
LOCALHOST(RegistryEnvironment.LOCAL),
|
|
UNITTEST(RegistryEnvironment.UNITTEST),
|
|
PDT(RegistryEnvironment.PRODUCTION, ImmutableMap.of(
|
|
"com.google.domain.registry.rde.key.receiver",
|
|
"pdt-escrow-test@icann.org"));
|
|
|
|
private static final ImmutableList<String> FLAGS = ImmutableList.of("-e", "--environment");
|
|
private static RegistryToolEnvironment instance;
|
|
private final RegistryEnvironment actualEnvironment;
|
|
private final ImmutableMap<String, String> extraProperties;
|
|
|
|
private RegistryToolEnvironment(
|
|
RegistryEnvironment actualEnvironment,
|
|
ImmutableMap<String, String> extraProperties) {
|
|
this.actualEnvironment = actualEnvironment;
|
|
this.extraProperties = extraProperties;
|
|
}
|
|
|
|
private RegistryToolEnvironment(RegistryEnvironment actualEnvironment) {
|
|
this(actualEnvironment, ImmutableMap.<String, String>of());
|
|
}
|
|
|
|
/**
|
|
* Extracts environment from command-line arguments.
|
|
*
|
|
* <p>This operation can't be performed by JCommander because it needs to happen before any
|
|
* command classes get loaded. This is because this routine will set system properties that are
|
|
* referenced by static initializers for many classes. This approach also allows us to change the
|
|
* default values of JCommander parameters, based on the selected environment.
|
|
*
|
|
* @see #get()
|
|
*/
|
|
static RegistryToolEnvironment parseFromArgs(String[] args) {
|
|
return valueOf(getFlagValue(args, FLAGS).toUpperCase());
|
|
}
|
|
|
|
/**
|
|
* Returns the current environment.
|
|
*
|
|
* <p>This should be called after {@link #parseFromArgs(String[])}.
|
|
*/
|
|
static RegistryToolEnvironment get() {
|
|
checkState(instance != null, "No RegistryToolEnvironment has been set up");
|
|
return instance;
|
|
}
|
|
|
|
/** Setup execution environment. Call this method before any classes are loaded. */
|
|
void setup() {
|
|
instance = this;
|
|
System.setProperty(RegistryEnvironment.PROPERTY, actualEnvironment.name());
|
|
for (ImmutableMap.Entry<String, String> entry : extraProperties.entrySet()) {
|
|
System.setProperty(entry.getKey(), entry.getValue());
|
|
}
|
|
}
|
|
|
|
/** Extracts value from command-line arguments associated with any {@code flags}. */
|
|
private static String getFlagValue(String[] args, Iterable<String> flags) {
|
|
for (String flag : flags) {
|
|
boolean expecting = false;
|
|
for (int i = 0; i < args.length; i++) {
|
|
// XXX: Kludge to stop processing once we reach what is *probably* the command name.
|
|
// This is necessary in order to allow commands to accept arguments named '-e'.
|
|
// This code should probably be updated, should any zero arity flags be added to
|
|
// RegistryCli, LoggingParameters, or AppEngineConnection.
|
|
if (args[i].startsWith("-")) {
|
|
expecting = !args[i].contains("=");
|
|
} else {
|
|
if (expecting) {
|
|
expecting = false;
|
|
} else {
|
|
break; // This is the command name, unless zero arity flags were added.
|
|
}
|
|
}
|
|
if (args[i].equals(flag)) {
|
|
checkArgument(i + 1 < args.length, "%s flag missing value.", flag);
|
|
return args[i + 1];
|
|
}
|
|
if (args[i].startsWith(flag + "=")
|
|
|| args[i].startsWith(flag + " ")) {
|
|
return args[i].substring(flag.length() + 1);
|
|
}
|
|
}
|
|
}
|
|
throw new IllegalArgumentException("Please specify the environment flag, e.g. -e production.");
|
|
}
|
|
}
|