private void ChangeEnvironment() { // If invoked with an argument, it is the name or index of an environment. Attempt to set that // environment and exit var target = _args.Shift(); if (target.Exists()) { var envName = target.AsString(); if (envName == _environments.CurrentName) { return; } if (!TrySetEnvironment(envName)) { throw new ExecutionException($"Could not set environment {envName}"); } OnEnvironmentChanged(); return; } // If we only have a single environment, switch directly to it with no input from the user var environments = _environments.GetNames(); if (environments.Count == 1) { _environments.SetCurrent(0); OnEnvironmentChanged(); return; } PromptUserForEnvironment(); }
/// <summary> /// Sets the current environment by position /// </summary> /// <param name="environments"></param> /// <param name="index"></param> public static void SetCurrent(this IEnvironmentCollection environments, int index) { Assert.ArgumentNotNull(environments, nameof(environments)); var allEnvs = environments.GetNames(); if (index < 0 || index >= allEnvs.Count) { return; } environments.SetCurrent(allEnvs[index]); }