@catladder/cli 3.1.1 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -53,7 +53,7 @@
53
53
  }
54
54
  ],
55
55
  "license": "MIT",
56
- "version": "3.1.1",
56
+ "version": "3.2.1",
57
57
  "scripts": {
58
58
  "lint": "eslint \"src/**/*.ts\"",
59
59
  "lint:fix": "eslint \"src/**/*.ts\" --fix",
@@ -1,17 +1,23 @@
1
1
  import type { ComponentContext } from "@catladder/pipeline";
2
2
  import {
3
3
  createKubernetesCloudsqlBaseValues,
4
+ getDeployConfigType,
4
5
  isOfDeployType,
5
6
  } from "@catladder/pipeline";
6
7
  import type Vorpal from "vorpal";
7
8
  import type { CommandInstance } from "vorpal";
9
+ import clipboard from "clipboardy";
8
10
  import {
9
11
  getEnvVarsResolved,
10
12
  getPipelineContextByChoice,
11
13
  parseChoice,
12
14
  } from "../../../../config/getProjectConfig";
13
15
  import { envAndComponents } from "./utils/autocompletions";
14
- import { startCloudSqlProxyInCurrentShell } from "../../../../gcloud/cloudSql/startProxy";
16
+ import {
17
+ ERROR_NOT_INSTALLED,
18
+ startCloudSqlProxyInCurrentShell,
19
+ } from "../../../../gcloud/cloudSql/startProxy";
20
+ import { logError, logLines, logWarning } from "../../../../utils/log";
15
21
 
16
22
  type ProxyInfo = {
17
23
  instanceName: string;
@@ -19,61 +25,15 @@ type ProxyInfo = {
19
25
  DB_PASSWORD: string;
20
26
  DB_USER: string;
21
27
  };
22
- export default async (vorpal: Vorpal) =>
23
- vorpal
24
- .command("project-cloud-sql-proxy <envComponent>", "proxy to cloud sql db")
25
- .autocomplete(await envAndComponents())
26
- .action(async function ({ envComponent }) {
27
- const { env, componentName } = parseChoice(envComponent);
28
- if (!componentName) {
29
- this.log("need componentName");
30
- return;
31
- }
32
-
33
- const context = await getPipelineContextByChoice(env, componentName);
34
- let proxyInfo: ProxyInfo;
35
-
36
- if (env === "review") {
37
- vorpal.log(
38
- "⚠️ connection string does not include mr information on review environments",
39
- );
40
- }
41
- if (isOfDeployType(context.deploy?.config, "kubernetes")) {
42
- proxyInfo = await getProxyInfoForKubernetes(this, context);
43
- } else if (isOfDeployType(context.deploy?.config, "google-cloudrun")) {
44
- proxyInfo = await getProxyInfoForCloudRun(this, context);
45
- } else {
46
- throw new Error("unsupported environment");
47
- }
48
-
49
- // skynet-164509:europe-west6:pvl-cyclomania-review=tcp:5432
50
- const { DB_PASSWORD, DB_NAME, DB_USER, instanceName } = proxyInfo;
51
- const { localPort } = await this.prompt({
52
- type: "number",
53
- name: "localPort",
54
- default: "54320",
55
- message: "Local port: ",
56
- });
57
-
58
- this.log("");
59
- this.log(`postgres-PW: ${DB_PASSWORD}`);
60
- this.log("");
61
- this.log("connection strings:");
62
- this.log("");
63
- this.log(
64
- `DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@localhost:${localPort}/${DB_NAME}?schema=public`,
65
- );
66
- this.log("");
67
- this.log(
68
- `DATABASE_JDBC_URL=jdbc:postgresql://localhost:${localPort}/${DB_NAME}?schema=public&user=${DB_USER}&password=${DB_PASSWORD}`,
69
- );
70
- this.log("");
71
28
 
72
- await startCloudSqlProxyInCurrentShell({
73
- instanceName,
74
- localPort,
75
- });
76
- });
29
+ const getProxyInfo = (context: ComponentContext) => {
30
+ if (isOfDeployType(context.deploy?.config, "kubernetes")) {
31
+ return getProxyInfoForKubernetes(this, context);
32
+ } else if (isOfDeployType(context.deploy?.config, "google-cloudrun")) {
33
+ return getProxyInfoForCloudRun(this, context);
34
+ }
35
+ throw new Error(`unsupported environment: ${context.deploy?.config?.type}`);
36
+ };
77
37
 
78
38
  const getProxyInfoForKubernetes = async (
79
39
  vorpal: CommandInstance,
@@ -111,20 +71,124 @@ const getProxyInfoForCloudRun = async (
111
71
  !isOfDeployType(context.deploy?.config, "google-cloudrun") ||
112
72
  !context.deploy?.config.cloudSql
113
73
  ) {
114
- throw new Error("unsupported");
74
+ const deployType = getDeployConfigType(context.deploy?.config);
75
+ const errorMessage =
76
+ deployType === "google-cloudrun"
77
+ ? `DeployConfig is missing the cloudSql property`
78
+ : `Unsupported DeployConfig type: ${deployType}`;
79
+ logError(vorpal, errorMessage);
80
+ throw new Error(errorMessage);
115
81
  }
116
-
117
82
  const envVars = await getEnvVarsResolved(vorpal, context.env, context.name);
118
-
119
- const DB_PASSWORD = envVars?.DB_PASSWORD?.toString();
120
- const DB_USER = envVars?.DB_USER?.toString();
121
-
122
- const DB_NAME = context.environment.envVars.DB_NAME.toString();
123
-
124
83
  return {
125
84
  instanceName: context.deploy?.config.cloudSql.instanceConnectionName,
126
- DB_PASSWORD,
127
- DB_NAME,
128
- DB_USER,
85
+ DB_PASSWORD: envVars?.DB_PASSWORD?.toString(),
86
+ DB_NAME: context.environment.envVars.DB_NAME.toString(),
87
+ DB_USER: envVars?.DB_USER?.toString(),
129
88
  };
130
89
  };
90
+
91
+ const getDbUrl = (
92
+ { DB_PASSWORD, DB_NAME, DB_USER }: ProxyInfo,
93
+ localPort: string,
94
+ additional: string = "",
95
+ ) =>
96
+ `DATABASE_URL="postgresql://${DB_USER}:${DB_PASSWORD}@localhost:${localPort}/${DB_NAME}${additional}"`;
97
+
98
+ const getJdbcUrl = (
99
+ { DB_PASSWORD, DB_NAME, DB_USER }: ProxyInfo,
100
+ localPort: string,
101
+ ) =>
102
+ `DATABASE_JDBC_URL="jdbc:postgresql://localhost:${localPort}/${DB_NAME}?schema=public&user=${DB_USER}&password=${DB_PASSWORD}"`;
103
+
104
+ export default async (vorpal: Vorpal) =>
105
+ vorpal
106
+ .command("project-cloud-sql-proxy <envComponent>", "proxy to cloud sql db")
107
+ .autocomplete(await envAndComponents())
108
+ .action(async function ({ envComponent }) {
109
+ const { env, componentName } = parseChoice(envComponent);
110
+ if (!componentName) {
111
+ logWarning(this, "need componentName");
112
+ return;
113
+ }
114
+
115
+ const context = await getPipelineContextByChoice(env, componentName);
116
+
117
+ if (env === "review") {
118
+ logWarning(
119
+ this,
120
+ "connection string does not include mr information on review environments",
121
+ );
122
+ }
123
+
124
+ const { localPort } = await this.prompt({
125
+ type: "number",
126
+ name: "localPort",
127
+ default: "54320",
128
+ message: "Local port: ",
129
+ });
130
+
131
+ const proxyInfo = await getProxyInfo(context);
132
+
133
+ /**
134
+ * Env variables for libpq (e.g. psql client)
135
+ * https://www.postgresql.org/docs/current/libpq-envars.html#LIBPQ-ENVARS
136
+ */
137
+ const psqlEnvVars = {
138
+ PGPASSWORD: proxyInfo.DB_PASSWORD,
139
+ PGUSER: proxyInfo.DB_USER,
140
+ PGDATABASE: proxyInfo.DB_NAME,
141
+ PGHOST: "localhost",
142
+ PGPORT: localPort,
143
+ };
144
+ const lipbqEnvVarDeclarations = Object.entries(psqlEnvVars).map(
145
+ ([key, value]) => `${key}="${value}"`,
146
+ );
147
+ const dbUrl = getDbUrl(proxyInfo, localPort);
148
+ const jdbcUrl = getJdbcUrl(proxyInfo, localPort);
149
+
150
+ clipboard.writeSync(
151
+ [
152
+ "",
153
+ "## CloudSQL proxy connection .env variables",
154
+ dbUrl,
155
+ jdbcUrl,
156
+ "## DATABASE_URL with schema=public parameter (e. g. Prisma requires this):",
157
+ `# ${getDbUrl(proxyInfo, localPort, `?schema=public`)}`,
158
+ "## Env variables for libpq (e.g. psql client https://www.postgresql.org/docs/current/libpq-envars.html#LIBPQ-ENVARS)",
159
+ ...lipbqEnvVarDeclarations,
160
+ "",
161
+ ].join("\n"),
162
+ );
163
+
164
+ logLines(
165
+ this,
166
+ null,
167
+ "Connection strings env variables DATABASE_URL and those for the 'psql' client (copied to your clipboard for .env file):",
168
+ dbUrl,
169
+ jdbcUrl,
170
+ ...lipbqEnvVarDeclarations,
171
+ null,
172
+ "DATABASE_URL with schema=public parameter (e. g. Prisma requires this):",
173
+ getDbUrl(proxyInfo, localPort, `?schema=public`),
174
+ null,
175
+ );
176
+
177
+ try {
178
+ await startCloudSqlProxyInCurrentShell({
179
+ instanceName: proxyInfo.instanceName,
180
+ localPort,
181
+ });
182
+ } catch (error) {
183
+ if (!(error instanceof Error)) throw error;
184
+ if (error.message !== ERROR_NOT_INSTALLED) throw error;
185
+ logError(
186
+ this,
187
+ error.message,
188
+ null,
189
+ "Please install the Cloud SQL Auth Proxy from:",
190
+ `https://cloud.google.com/sql/docs/postgres/connect-auth-proxy#install`,
191
+ null,
192
+ );
193
+ }
194
+ });
@@ -69,7 +69,7 @@ const upsertGcloudServiceAccount = async (
69
69
  const memberName = `serviceAccount:${fullIdentifier}`;
70
70
  for (const role of roles) {
71
71
  await exec(
72
- `gcloud projects add-iam-policy-binding ${projectId} --member=${memberName} --role=${role} `,
72
+ `gcloud projects add-iam-policy-binding ${projectId} --member=${memberName} --role=${role} --condition=None`,
73
73
  );
74
74
  }
75
75
 
package/src/utils/log.ts CHANGED
@@ -1,13 +1,113 @@
1
+ import type { CommandInstance } from "vorpal";
2
+
3
+ type AnyMessagesFn = (message?: any, ...optionalParams: any[]) => void;
4
+ type StringMessagesFn = (message?: string, ...optionalParams: string[]) => void;
5
+ type AnyMessageFn = (message?: any) => void;
6
+ type StringMessageFn = (message?: string) => void;
7
+ type StringMessageSpreadFn = (...message: string[]) => void;
8
+ type AnyMessageSpreadFn = (...message: any[]) => void;
9
+ type SomeKindOfLogFn =
10
+ | AnyMessagesFn
11
+ | StringMessagesFn
12
+ | AnyMessageFn
13
+ | StringMessageFn
14
+ | StringMessageSpreadFn
15
+ | AnyMessageSpreadFn;
16
+ type HasSomeKindOfLogMethod = { log: SomeKindOfLogFn };
17
+
18
+ /**
19
+ * Something that has a .log method taking a string argument.
20
+ *
21
+ * null and undefined will fall back to console.log.
22
+ */
23
+ export type LoggerCmdInstance =
24
+ | CommandInstance
25
+ | Console
26
+ | HasSomeKindOfLogMethod
27
+ | null
28
+ | undefined;
29
+
30
+ type LinesArgItem =
31
+ | string
32
+ | number
33
+ | boolean
34
+ | Date
35
+ | RegExp
36
+ | null
37
+ | undefined;
38
+ type LinesArg = LinesArgItem | LinesArgItem[];
39
+
40
+ /**
41
+ * Logs multiple lines using the log method of the provided {@link LoggerCmdInstance}.
42
+ *
43
+ * Line items that are `null` or `undefined` are logged as empty lines.
44
+ *
45
+ * @param cmd - The logger instance to use. If `null` or `undefined`, `console` will be used.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * logLines(cmd, "line 1", "line 2", null, "line 3");
50
+ * logLines(cmd, ...linesArray); // arrays can be spread into the function
51
+ * logLines(cmd, linesArray); // but it will flatten the plain array for convenience
52
+ * ```
53
+ */
54
+ export function logLines(
55
+ cmd: LoggerCmdInstance = console,
56
+ ...lines: LinesArg[]
57
+ ) {
58
+ for (const line of lines.flat()) {
59
+ (cmd ?? console).log(`${line ?? ""}`);
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Logs an error message using the log method of the provided {@link LoggerCmdInstance}.
65
+ *
66
+ * Line items that are `null` or `undefined` are logged as empty lines.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * logError(cmd, "Message");
71
+ * logError(cmd, "Message", "Additional message");
72
+ * logError(cmd, "Message", "Additional message", "Another additional message line");
73
+ * logError(cmd, "Message", ...additionalMessages); // arrays can be spread into the function
74
+ * logError(cmd, "Message", additionalMessages); // but it will flatten the plain array for convenience
75
+ * ```
76
+ */
1
77
  export const logError = (
2
- cmd: any,
78
+ cmd: LoggerCmdInstance = console,
79
+ message: string,
80
+ ...additionalMessages: LinesArg[]
81
+ ) => {
82
+ logLines(cmd, "", `[ERROR] 🙀 :${message}`);
83
+ if (additionalMessages?.length) {
84
+ logLines(cmd, ...additionalMessages.flat());
85
+ }
86
+ logLines(cmd, "");
87
+ };
88
+
89
+ /**
90
+ * Logs a warning message using the log method of the provided {@link LoggerCmdInstance}.
91
+ *
92
+ * Line items that are `null` or `undefined` are logged as empty lines.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * logWarning(cmd, "Message");
97
+ * logWarning(cmd, "Message", "Additional message");
98
+ * logWarning(cmd, "Message", "Additional message", "Another additional message line");
99
+ * logWarning(cmd, "Message", ...additionalMessages); // arrays can be spread into the function
100
+ * logWarning(cmd, "Message", additionalMessages); // but it will flatten the plain array for convenience
101
+ * ```
102
+ */
103
+ export const logWarning = (
104
+ cmd: LoggerCmdInstance = console,
3
105
  message: string,
4
- additionalMessage?: any,
106
+ ...additionalMessages: LinesArg[]
5
107
  ) => {
6
- cmd.log("");
7
- cmd.log(`[ERROR] 🙀 :${message}`);
8
- cmd.log("");
9
- if (additionalMessage) {
10
- cmd.log(additionalMessage);
11
- cmd.log("");
108
+ logLines(cmd, "", `[WARNING] ⚠️ : ${message}`);
109
+ if (additionalMessages?.length) {
110
+ logLines(cmd, ...additionalMessages.flat());
12
111
  }
112
+ logLines(cmd, "");
13
113
  };