@hadcloud/deploy-cli 0.1.0 → 0.1.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.
Files changed (2) hide show
  1. package/dist/index.js +85 -8
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -11,6 +11,8 @@ Uso:
11
11
  had config
12
12
  had projects list
13
13
  had apps list <project-id>
14
+ had templates list
15
+ had apps create <project-id> --template <template-id> [--region <region-code>]
14
16
  had deploy <project-id> <app-id>
15
17
  had status <deployment-id>
16
18
  had logs <deployment-id> [tail]
@@ -24,7 +26,11 @@ async function request(path, init) {
24
26
  const credentials = await loadCredentials();
25
27
  const response = await fetch(`${credentials.apiUrl}${path}`, {
26
28
  ...init,
27
- headers: { authorization: `Bearer ${credentials.token}`, accept: "application/json", ...init?.headers },
29
+ headers: {
30
+ authorization: `Bearer ${credentials.token}`,
31
+ accept: "application/json",
32
+ ...init?.headers,
33
+ },
28
34
  });
29
35
  if (!response.ok) {
30
36
  const payload = (await response.json().catch(() => ({})));
@@ -83,7 +89,9 @@ async function promptSecret(question) {
83
89
  }
84
90
  async function login(args) {
85
91
  const apiUrlIndex = args.indexOf("--api-url");
86
- const apiInput = apiUrlIndex >= 0 ? args[apiUrlIndex + 1] : await prompt("URL da API HAD Deploy: ");
92
+ const apiInput = apiUrlIndex >= 0
93
+ ? args[apiUrlIndex + 1]
94
+ : await prompt("URL da API HAD Deploy: ");
87
95
  if (!apiInput)
88
96
  throw new Error("A URL da API é obrigatória.");
89
97
  const fromStdin = args.includes("--token-stdin");
@@ -102,10 +110,28 @@ function printRows(rows) {
102
110
  }
103
111
  const columns = Object.keys(rows[0] ?? {});
104
112
  const widths = columns.map((column) => Math.max(column.length, ...rows.map((row) => (row[column] ?? "").length)));
105
- console.log(columns.map((column, index) => column.toUpperCase().padEnd(widths[index] ?? column.length)).join(" "));
113
+ console.log(columns
114
+ .map((column, index) => column.toUpperCase().padEnd(widths[index] ?? column.length))
115
+ .join(" "));
106
116
  console.log(widths.map((width) => "-".repeat(width)).join(" "));
107
117
  for (const row of rows)
108
- console.log(columns.map((column, index) => (row[column] ?? "").padEnd(widths[index] ?? 0)).join(" "));
118
+ console.log(columns
119
+ .map((column, index) => (row[column] ?? "").padEnd(widths[index] ?? 0))
120
+ .join(" "));
121
+ }
122
+ function optionValue(args, option) {
123
+ const positions = args
124
+ .map((value, index) => (value === option ? index : -1))
125
+ .filter((index) => index >= 0);
126
+ if (positions.length > 1)
127
+ throw new Error(`A opção ${option} só pode ser informada uma vez.`);
128
+ const position = positions[0];
129
+ if (position === undefined)
130
+ return undefined;
131
+ const value = args[position + 1];
132
+ if (!value || value.startsWith("--"))
133
+ throw new Error(`Informe um valor para ${option}.`);
134
+ return value;
109
135
  }
110
136
  export async function main(args) {
111
137
  const [group, action, resourceId, extra] = args;
@@ -123,12 +149,51 @@ export async function main(args) {
123
149
  }
124
150
  if (group === "projects" && action === "list" && !resourceId) {
125
151
  const { projects } = await request("/api/v1/automation/projects");
126
- printRows(projects.map((project) => ({ id: project.id, name: project.name, status: project.status })));
152
+ printRows(projects.map((project) => ({
153
+ id: project.id,
154
+ name: project.name,
155
+ status: project.status,
156
+ })));
127
157
  return;
128
158
  }
129
159
  if (group === "apps" && action === "list" && resourceId) {
130
160
  const { applications } = await request(`/api/v1/automation/projects/${encodeURIComponent(resourceId)}/apps`);
131
- printRows(applications.map((application) => ({ id: application.id, name: application.name, framework: application.framework, status: application.status })));
161
+ printRows(applications.map((application) => ({
162
+ id: application.id,
163
+ name: application.name,
164
+ framework: application.framework,
165
+ status: application.status,
166
+ })));
167
+ return;
168
+ }
169
+ if (group === "templates" && action === "list" && !resourceId) {
170
+ const { templates } = await request("/api/v1/automation/templates");
171
+ printRows(templates.map((template) => ({
172
+ id: template.id,
173
+ name: template.name,
174
+ version: template.version ?? "latest",
175
+ tags: template.tags.join(", "),
176
+ })));
177
+ return;
178
+ }
179
+ if (group === "apps" && action === "create" && resourceId) {
180
+ const templateId = optionValue(args, "--template");
181
+ const regionCode = optionValue(args, "--region");
182
+ if (!templateId) {
183
+ throw new Error("Use --template <template-id>. Consulte os modelos com 'had templates list'.");
184
+ }
185
+ const response = await request(`/api/v1/automation/templates/${encodeURIComponent(templateId)}/services`, {
186
+ method: "POST",
187
+ headers: {
188
+ "content-type": "application/json",
189
+ "idempotency-key": crypto.randomUUID(),
190
+ },
191
+ body: JSON.stringify({
192
+ projectId: resourceId,
193
+ ...(regionCode ? { regionCode } : {}),
194
+ }),
195
+ });
196
+ console.log(`Serviço ${response.service.name} (${response.service.id}) criado: ${response.service.status}`);
132
197
  return;
133
198
  }
134
199
  if (group === "deploy" && action && resourceId && !extra) {
@@ -138,7 +203,17 @@ export async function main(args) {
138
203
  }
139
204
  if (group === "status" && action && !resourceId) {
140
205
  const { deployment } = await request(`/api/v1/automation/deployments/${encodeURIComponent(action)}`);
141
- printRows([{ id: deployment.id, status: deployment.status, trigger: deployment.trigger, queued: deployment.queuedAt, started: deployment.startedAt ?? "-", finished: deployment.finishedAt ?? "-", error: deployment.errorCode ?? "-" }]);
206
+ printRows([
207
+ {
208
+ id: deployment.id,
209
+ status: deployment.status,
210
+ trigger: deployment.trigger,
211
+ queued: deployment.queuedAt,
212
+ started: deployment.startedAt ?? "-",
213
+ finished: deployment.finishedAt ?? "-",
214
+ error: deployment.errorCode ?? "-",
215
+ },
216
+ ]);
142
217
  return;
143
218
  }
144
219
  if (group === "logs" && action && (!resourceId || /^\d+$/.test(resourceId))) {
@@ -151,6 +226,8 @@ export async function main(args) {
151
226
  usage();
152
227
  }
153
228
  main(process.argv.slice(2)).catch((error) => {
154
- console.error(error instanceof Error ? error.message : "Não foi possível concluir o comando.");
229
+ console.error(error instanceof Error
230
+ ? error.message
231
+ : "Não foi possível concluir o comando.");
155
232
  process.exitCode = 1;
156
233
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hadcloud/deploy-cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "bin": {