Skip to content

Commit

Permalink
Merge pull request #1121 from polyseam/configurator-updates
Browse files Browse the repository at this point in the history
Configurator updates
  • Loading branch information
johnstonmatt authored Feb 8, 2025
2 parents d1b3a4f + dd84340 commit 35193b9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 79 deletions.
4 changes: 4 additions & 0 deletions blocks/common/cluster/core-prompts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@
message: "Please enter your Git Repo URL:"
type: Input
default: ""
validators:
- url

- name: cert_manager_email
message: >-
What email address should be used for Let's Encrypt certificate
registration?
type: Input
default: [email protected]
validators:
- email

- name: enable_external_dns
message: >-
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.27.3",
"version": "2.27.4",
"deno_version": "2.1.7",
"tasks": {
"tar-win-amd64": "tar -czvf dist/release-archives/cndi-win-amd64.tar.gz -C dist/win-amd64/in .",
Expand Down
183 changes: 106 additions & 77 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ type EchoCreateOptions = {
skipPush?: boolean;
};

const DEFAULT_RESPONSES_FILE_PATH = path.join(
Deno.cwd(),
"cndi_responses.yaml",
);

const echoCreate = (options: EchoCreateOptions, slug?: string) => {
const cndiCreate = "cndi create";
const cndiCreateSlug = slug ? ` ${slug}` : "";
Expand All @@ -84,8 +89,12 @@ const echoCreate = (options: EchoCreateOptions, slug?: string) => {
? ` --deployment-target-label ${options.deploymentTargetLabel}`
: "";
const cndiCreateSkipPush = options.skipPush ? " --skip-push" : "";
const cndiCreateResponsesFile =
options.responsesFile === DEFAULT_RESPONSES_FILE_PATH
? ""
: ` --responses-file ${options.responsesFile}`;
console.log(
`${cndiCreate}${cndiCreateSlug}${cndiCreateInteractive}${cndiCreateTemplate}${cndiCreateOutput}${cndiCreateDeploymentTargetLabel}${cndiCreateSet}${cndiCreateSkipPush}\n`,
`${cndiCreate}${cndiCreateSlug}${cndiCreateInteractive}${cndiCreateTemplate}${cndiCreateDeploymentTargetLabel}${cndiCreateResponsesFile}${cndiCreateOutput}${cndiCreateSet}${cndiCreateSkipPush}\n`,
);
};

Expand All @@ -108,7 +117,7 @@ const createCommand = new Command()
"-r, --responses-file <responses_file:string>",
"Path to YAML 'responses file' to supply to Template prompts.",
{
default: path.join(Deno.cwd(), "cndi_responses.yaml"),
default: DEFAULT_RESPONSES_FILE_PATH,
},
)
.option("--skip-push", "Skip pushing to remote repository")
Expand Down Expand Up @@ -145,6 +154,101 @@ const createCommand = new Command()
let template: string | undefined = options?.template;
let overrides: Record<string, CNDITemplatePromptResponsePrimitive> = {};

// load responses from file
let responsesFileText = "";

try {
console.log("options.responsesFile", options.responsesFile);
responsesFileText = await Deno.readTextFile(options.responsesFile);
} catch (errLoadingResponsesFile) {
if (errLoadingResponsesFile instanceof Deno.errors.NotFound) {
// no responses file found, continue with defaults
} else {
console.error(
label,
ccolors.error("Error loading"),
ccolors.key_name(options.responsesFile),
ccolors.error("as responses file"),
);
ccolors.caught(errLoadingResponsesFile as Error, 1502);
console.log(
ccolors.warn(
"⚠️ continuing with defaults in spite of unexpected error",
),
);
}
}

const responses: Record<string, CNDITemplatePromptResponsePrimitive> = {};

if (responsesFileText) {
try {
const parsed = YAML.parse(responsesFileText) as Record<
string,
CNDITemplatePromptResponsePrimitive
>;
for (const [key, value] of Object.entries(parsed)) {
if (typeof value === "string") {
responses[key] = value;
}
}
} catch (errorParsingResponses) {
const err = new ErrOut(
[
ccolors.error("Error parsing"),
ccolors.key_name(options.responsesFile),
ccolors.error("as responses file"),
],
{
label,
code: 1503,
id: "create/!isValidYAML(responsesFile)",
cause: errorParsingResponses as Error,
},
);
await err.out();
return;
}

const responseCount = Object.keys(responses).length;

if (responseCount) {
console.log();
console.log(
ccolors.key_name("cndi"),
"is pulling",
ccolors.success(responseCount.toString()),
"responses from",
ccolors.success(options.responsesFile) +
"!",
);
console.log();
overrides = responses as Record<
string,
CNDITemplatePromptResponsePrimitive
>;
}
}

if (options.set) {
for (const set of options.set) {
const [key, value] = set.split("=");
overrides[key] = value;
}
}

if (typeof overrides?.git_repo === "string" && !slug) {
try {
slug = new URL(overrides.git_repo as string).pathname.slice(1);
} catch (_error) {
console.log(
ccolors.warn("Failed to parse"),
ccolors.key_name("git_repo"),
ccolors.warn("response as URL!"),
);
}
}

if (!slug) {
if (interactive) {
slug = await PromptTypes.Input.prompt({
Expand Down Expand Up @@ -222,81 +326,6 @@ const createCommand = new Command()
return;
}

// load responses from file
let responsesFileText = "";

try {
responsesFileText = await Deno.readTextFile(options.responsesFile);
} catch (errLoadingResponsesFile) {
if (errLoadingResponsesFile instanceof Deno.errors.NotFound) {
// no responses file found, continue with defaults
} else {
console.error(
label,
ccolors.error("Error loading"),
ccolors.key_name(options.responsesFile),
ccolors.error("as responses file"),
);
ccolors.caught(errLoadingResponsesFile as Error, 1502);
console.log(
ccolors.warn(
"⚠️ continuing with defaults in spite of unexpected error",
),
);
}
}

let responses: Record<string, CNDITemplatePromptResponsePrimitive> = {};

try {
responses = YAML.parse(responsesFileText) as Record<
string,
CNDITemplatePromptResponsePrimitive
>;
} catch (errorParsingResponses) {
const err = new ErrOut(
[
ccolors.error("Error parsing"),
ccolors.key_name(options.responsesFile),
ccolors.error("as responses file"),
],
{
label,
code: 1503,
id: "create/!isValidYAML(responsesFile)",
cause: errorParsingResponses as Error,
},
);
await err.out();
return;
}

const responseCount = Object.keys(responses).length;

if (responseCount) {
console.log();
console.log(
ccolors.key_name("cndi"),
"is pulling",
ccolors.success(responseCount.toString()),
"responses from",
ccolors.success(options.responsesFile) +
"!",
);
console.log();
overrides = responses as Record<
string,
CNDITemplatePromptResponsePrimitive
>;
}

if (options.set) {
for (const set of options.set) {
const [key, value] = set.split("=");
overrides[key] = value;
}
}

if (options.deploymentTargetLabel) {
const slug = options.deploymentTargetLabel.split("/");

Expand Down
2 changes: 1 addition & 1 deletion templates/redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ prompts:

- name: default_redis_password
message: "Please enter Redis default user password"
default: password
default: "{{ $cndi.get_random_string(32) }}"
type: Secret

- name: cluster_size
Expand Down

0 comments on commit 35193b9

Please sign in to comment.