async function askMissingData()

in util/config/configurator/configurator.ts [149:271]


async function askMissingData(missingData: Prompts, override: boolean = false) {
  if (Object.keys(missingData).length === 0) {
    outro("Configuration set from ops");
    process.exit(0);
  }
  console.log();

  if (!override) {
    console.log("Configuration partially set from ops. Need a few more:");
  }

  for (const key in missingData) {
    let inputFromPrompt: string;
    const prompt: PromptData = missingData[key];
    // let askedForPassword = false;

    if (Array.isArray(prompt.type)) {
      const defaultValueOK = prompt.default && prompt.type.includes(prompt.default);
      if (prompt.default && !defaultValueOK) {
        console.log();
        console.warn(`The default value ${prompt.default} is not in the enum values.`);
      }

      const selected = await select({
        message: prompt.label || `Pick a value for '${key}'`,
        options: prompt.type.map((v) => ({ label: v, value: v })),
        initialValue: defaultValueOK ? prompt.default : undefined,
      });

      if (!selected || isCancel(selected)) {
        cancel("Operation cancelled");
        process.exit(0);
      }

      inputFromPrompt = selected.toString();

      // inputConfigs[key] = { ...prompt, value: selected.toString() };
    } else if (prompt.type === "bool") {
      const selected = await select({
        initialValue: prompt.default === "true" || prompt.default as unknown as boolean === true ? "true" : "false",
        message: prompt.label || `Pick a true/false for '${key}'`,
        options: [
          { label: "true", value: "true" },
          { label: "false", value: "false" },
        ],
      });

      if (!selected || isCancel(selected)) {
        cancel("Operation cancelled");
        process.exit(0);
      }

      inputFromPrompt = selected.toString();
      // inputConfigs[key] = { ...prompt, value: selected.toString() };
    } else if (prompt.type === "password") {
      if (prompt.default) {
        console.log();
        console.warn("Default password value is not supported. Please enter the password manually.");
      }
      const input = await password({
        message: prompt.label || `Enter password value for ${key}`,
        validate: (value) => {
          if (!value) {
            return "Password cannot be empty";
          }
        }
      });

      if (isCancel(input)) {
        cancel("Operation cancelled");
        process.exit(0);
      }
      inputFromPrompt = input;
      // askedForPassword = true;
    } else {
      const defaultMsgFragment = prompt.default ? `(default: ${prompt.default})` : "";
      const message = `Enter value for ${key} ${defaultMsgFragment} (${prompt.type})`;

      let input = await text({
        message: prompt.label || message,
        // defaultValue: prompt.default?.toString(),
        initialValue: prompt.default?.toString(),
        validate: (value) => {
          switch (prompt.type) {
            case "int":
              if (!Number.isInteger(Number(value))) {
                return `Value for ${key} must be an integer number`;
              }
              break;
            case "float":
              if (!Number(value)) {
                return `Value for ${key} must be a number`;
              }
          }
          return;
        }
      }) as string;

      if (isCancel(input)) {
        cancel("Operation cancelled");
        process.exit(0);
      }


      inputFromPrompt = input;
      // inputConfigs[key] = { ...prompt, value: input };
    }

    // if (!askedForPassword) {
    //   console.log("Setting", key, "to", inputFromPrompt);
    // } else {
    //   console.log("Setting", key, "to", "*".repeat(inputFromPrompt.length));
    // }

    console.log(`Setting ${key} to ${inputFromPrompt}`);
    const { exitCode, stderr } =
      await $`${OPS} -config ${key}=${inputFromPrompt}`.nothrow();
    if (exitCode !== 0) {
      cancel(stderr.toString());
      return process.exit(1);
    }
  }
}