private static CLIExitCode ProcessPasswordOption()

in Configurator/Core/CLI/CommandLine.cs [716:795]


    private static CLIExitCode ProcessPasswordOption(string passwordOptionName, string passwordFileOptionName, string environmentVariable, ServerInstallation serverInstallation)
    {
      var passwordOption = CommandLineParser.GetMatchingProvidedOption(passwordOptionName);
      var passwordFileOption = CommandLineParser.GetMatchingProvidedOption(passwordFileOptionName);
      if (passwordOption != null)
      {
        if (!TryToSetValue(serverInstallation.Controller, passwordOption.Name, passwordOption.Value))
        {
          return new CLIExitCode(ExitCode.InvalidOptionValue, passwordOption.Value, passwordOption.Name);
        }

        if (serverInstallation.Controller.ConfigurationType == ConfigurationType.Reconfigure)
        {
          serverInstallation.Controller.Settings.ExistingRootPassword = passwordOption.Value;
        }

        CommandLineParser.ProvidedOptions.Remove(passwordOption);
      }
      else if (passwordFileOption != null)
      {
        // Read password from file.
        if (string.IsNullOrEmpty(passwordFileOption.Value))
        {
          return new CLIExitCode(ExitCode.OptionValueNotFound, passwordFileOption.Name);
        }

        // Validate the file exists.
        if (!File.Exists(passwordFileOption.Value))
        {
          return new CLIExitCode(ExitCode.PasswordFileDoesNotExist, passwordFileOption.Value);
        }

        // Read password from file.
        var passwordValuePair = Utilities.ReadTextFile(passwordFileOption.Value, out string errorMessage);
        if (!string.IsNullOrEmpty(errorMessage))
        {
          return new CLIExitCode(ExitCode.ErrorReadingPasswordFile, passwordFileOption.Value, errorMessage);
        }

        var items = passwordValuePair.Split('=');
        string password = null;
        if (items.Length != 2
            || !items[0].Equals("password", StringComparison.CurrentCultureIgnoreCase)
            || string.IsNullOrEmpty(items[1]))
        {
          return new CLIExitCode(ExitCode.PasswordFileInvalidContents, passwordFileOption.Value);
        }
        else
        {
          password = items[1];
        }

        // Set password.
        if (!TryToSetValue(serverInstallation.Controller, passwordOptionName, password))
        {
          return new CLIExitCode(ExitCode.InvalidOptionValue, passwordOptionName, password);
        }

        CommandLineParser.ProvidedOptions.Remove(passwordFileOption);
      }
      else
      {
        // Read from environment variable.
        string password = Environment.GetEnvironmentVariable(environmentVariable);

        if (!string.IsNullOrEmpty(password)
            && !TryToSetValue(serverInstallation.Controller, passwordOptionName, password))
        {
          return new CLIExitCode(ExitCode.InvalidOptionValue, passwordOptionName, password);
        }

        // If password could not be obtained raise error.
        if (string.IsNullOrEmpty(password))
        {
          return new CLIExitCode(ExitCode.OptionValueNotFound, passwordOptionName);
        }
      }

      return new CLIExitCode(ExitCode.Success);
    }