Improve RegistryToolEnvironment setup behavior

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
This commit is contained in:
nickfelt 2016-03-17 15:37:58 -07:00 committed by Justine Tunney
parent 14c794aa45
commit 5174c1c63f
7 changed files with 60 additions and 36 deletions

View file

@ -72,8 +72,7 @@ public final class GtechTool {
.build();
public static void main(String[] args) throws Exception {
RegistryToolEnvironment.loadFromArgs(args);
RegistryToolEnvironment.get().setup();
RegistryToolEnvironment.parseFromArgs(args).setup();
new RegistryCli().run("gtech_tool", args, COMMAND_MAP);
}
}

View file

@ -76,8 +76,7 @@ public final class RegistryTool {
.build();
public static void main(String[] args) throws Exception {
RegistryToolEnvironment.loadFromArgs(args);
RegistryToolEnvironment.get().setup();
RegistryToolEnvironment.parseFromArgs(args).setup();
new RegistryCli().run("registry_tool", args, COMMAND_MAP);
}
}

View file

@ -15,7 +15,7 @@
package com.google.domain.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@ -60,21 +60,23 @@ enum RegistryToolEnvironment {
*
* @see #get()
*/
static void loadFromArgs(String[] args) {
instance = valueOf(getFlagValue(args, FLAGS).toUpperCase());
static RegistryToolEnvironment parseFromArgs(String[] args) {
return valueOf(getFlagValue(args, FLAGS).toUpperCase());
}
/**
* Returns the current environment.
*
* <p>This should be called after {@link #loadFromArgs(String[])}.
* <p>This should be called after {@link #parseFromArgs(String[])}.
*/
static RegistryToolEnvironment get() {
return checkNotNull(instance);
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());