@tamai/hub-cli 0.1.5 → 0.1.7
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/README.md +51 -2
- package/commands/infra-link.mjs +25 -0
- package/commands/infra-list.mjs +20 -0
- package/commands/infra-show.mjs +13 -0
- package/commands/infra-unlink.mjs +24 -0
- package/commands/infra-update.mjs +124 -0
- package/commands/project-update.mjs +34 -4
- package/lib/args.mjs +55 -1
- package/lib/cli.mjs +17 -0
- package/lib/format.mjs +133 -0
- package/lib/infra.mjs +110 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @tamai/hub-cli
|
|
2
2
|
|
|
3
|
-
hub(tamai
|
|
3
|
+
hub(tamai個人のパーソナルハブ)のタスク看板、プロジェクト、インフラ資産を操作する CLI。
|
|
4
4
|
状態の正本は hub の D1 であり、この CLI はそれを読み書きする唯一の道具である。
|
|
5
5
|
依存ゼロ(`node:` のみ)。**認証は tamai の Cloudflare Access service token が要る**ので、
|
|
6
6
|
それを持っていない環境では何もできない。API の契約・権限・エラーの意味は
|
|
@@ -63,10 +63,12 @@ service token を複数のエージェントで共有するので、**誰の操
|
|
|
63
63
|
hub --help
|
|
64
64
|
hub task help
|
|
65
65
|
hub project help
|
|
66
|
+
hub infra help
|
|
66
67
|
```
|
|
67
68
|
|
|
68
69
|
動詞は `task` が `new` / `add` / `claim` / `next` / `show` / `report` / `note` / `list` / `audit`、
|
|
69
|
-
`project` が `list` / `show` / `create` / `update` / `move
|
|
70
|
+
`project` が `list` / `show` / `create` / `update` / `move`、
|
|
71
|
+
`infra` が `list` / `show` / `update` / `link` / `unlink`。
|
|
70
72
|
**正確なオプションは常に `--help` を見る**(ここにも、スキルにも写経しない)。
|
|
71
73
|
全動詞が `--json` を受ける。機械が読むときは必ず `--json` を使い、人向けの整形を parse しない。
|
|
72
74
|
|
|
@@ -96,6 +98,53 @@ URL から決める。`"<ラベル>=<URL>"` の形は従来どおり最初の `=
|
|
|
96
98
|
入力した URL は資格情報を抱えていることがあるため、**どのエラー文にも再掲しない**
|
|
97
99
|
(何番目の `--add-link` か・既存側のラベルだけを出す)。
|
|
98
100
|
|
|
101
|
+
### グループ(プロジェクトの親子)
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
hub project update koyo --entity-type group # 案件 → グループへ
|
|
105
|
+
hub project update koyo-cms --parent koyo # グループに属させる
|
|
106
|
+
hub project update koyo-cms --parent none # グループから外す
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`--entity-type` は `project`(案件・既定)か `group`(グループ)。グループは案件の上の層で、
|
|
110
|
+
**自分は親を持てない**(1 段)。`--parent` は所属するグループの slug で、`none` で外す。
|
|
111
|
+
指定できるのは**グループの slug だけ**なので、`--entity-type group --parent <slug>` は
|
|
112
|
+
CLI が送る前に断る。
|
|
113
|
+
|
|
114
|
+
配下の案件がある `group` の拒否は3条件で別々である。**削除**と**案件(`project`)への変更**は
|
|
115
|
+
子が1件でもあれば拒否され、**終了(`status=closed`)は終了していない子があるときだけ**拒否される
|
|
116
|
+
(子が全部終了していればグループも終了にできる)。いずれも API が 409 `project_has_children` を
|
|
117
|
+
返し、CLI はその message をそのまま出す。
|
|
118
|
+
|
|
119
|
+
`show` / `list` は、グループのときだけ `type: group` と `children: N` の行を、親を持つ案件には
|
|
120
|
+
`parent: <slug>` の行を出す(`kind` と同じく**既定でないときだけ**出す流儀)。`show` は
|
|
121
|
+
配下の案件を `children:` の下に `- <slug> [<status>] <name>` で並べる。`--json` は応答そのまま。
|
|
122
|
+
|
|
123
|
+
### インフラ資産を読む・更新する
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
hub infra list # 自分で管理する資産だけ(既定)
|
|
127
|
+
hub infra list --management all # 他社管理を含む全件
|
|
128
|
+
hub infra show example.jp
|
|
129
|
+
hub infra update example.jp --status to_cancel --append-note '解約申請済み'
|
|
130
|
+
hub infra update example.jp --prod-platform none # 本番の置き場なし(確定値)
|
|
131
|
+
hub infra link example.jp --project hub
|
|
132
|
+
hub infra unlink example.jp --project hub
|
|
133
|
+
hub infra unlink example.jp # 紐付けを全部外す
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`show` / `update` / `link` / `unlink` は資産名の完全一致またはIDで指定し、見つからない場合は
|
|
137
|
+
近い候補を3件まで出す。`update` は指定した項目だけをPATCHし、version競合時は最新を取り直して
|
|
138
|
+
1回だけ再試行する。`link` は現在の紐付けへ追加、`unlink` は指定分を削除し、APIへはどちらも
|
|
139
|
+
`PUT /:id/projects` の全置換として送る。
|
|
140
|
+
|
|
141
|
+
`--dns-host` / `--mail-host` / `--migration-plan` / `--contract` / `--hosted-on-basis`は
|
|
142
|
+
`none`で未記入(`null`)へ戻せる。`--prod-platform none`だけは「本番の置き場は無い」という
|
|
143
|
+
確定済みのenum値を送り、未記入には戻さない。
|
|
144
|
+
|
|
145
|
+
資産の作成と削除はCLIに持たせない。正確なオプションとserver資産へ指定できない項目は
|
|
146
|
+
`hub infra help` を正本とする。
|
|
147
|
+
|
|
99
148
|
### `--repo-card` の出力先は cwd 基準
|
|
100
149
|
|
|
101
150
|
```bash
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { requireSinglePositional, usageError } from "../lib/args.mjs";
|
|
2
|
+
import { printInfraResult } from "../lib/format.mjs";
|
|
3
|
+
import { getInfraAsset, projectSlugs, requireInfraVersion } from "../lib/infra.mjs";
|
|
4
|
+
|
|
5
|
+
export const infraLinkCommand = {
|
|
6
|
+
name: "link",
|
|
7
|
+
options: { allowed: ["project"], repeatable: ["project"] },
|
|
8
|
+
async run({ options, positional, flags, dependencies, api }) {
|
|
9
|
+
const reference = requireSinglePositional(positional, "資産名またはid");
|
|
10
|
+
if (!flags.project || options.project.length === 0) {
|
|
11
|
+
throw usageError("--project <slug> を1つ以上指定してください");
|
|
12
|
+
}
|
|
13
|
+
const additions = options.project.map((slug) => slug.trim());
|
|
14
|
+
if (additions.some((slug) => slug === "")) throw usageError("--project に空文字は指定できません");
|
|
15
|
+
|
|
16
|
+
const current = await getInfraAsset(reference, api);
|
|
17
|
+
const slugs = [...projectSlugs(current.item)];
|
|
18
|
+
for (const slug of additions) if (!slugs.includes(slug)) slugs.push(slug);
|
|
19
|
+
const payload = await api(`/api/infra/assets/${encodeURIComponent(current.item.id)}/projects`, {
|
|
20
|
+
method: "PUT",
|
|
21
|
+
body: JSON.stringify({ version: requireInfraVersion(current.item), project_slugs: slugs }),
|
|
22
|
+
});
|
|
23
|
+
printInfraResult(payload, options.json, dependencies.stdout, "projects");
|
|
24
|
+
},
|
|
25
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { usageError } from "../lib/args.mjs";
|
|
2
|
+
import { printInfraResult } from "../lib/format.mjs";
|
|
3
|
+
|
|
4
|
+
export const infraListCommand = {
|
|
5
|
+
name: "list",
|
|
6
|
+
options: { allowed: ["kind", "status", "management", "project", "q"] },
|
|
7
|
+
async run({ options, positional, flags, dependencies, api }) {
|
|
8
|
+
if (positional.length !== 0) throw usageError("listに位置引数は指定できません");
|
|
9
|
+
|
|
10
|
+
const query = new URLSearchParams();
|
|
11
|
+
if (flags.kind) query.set("kind", options.kind);
|
|
12
|
+
if (flags.status) query.set("status", options.status);
|
|
13
|
+
query.set("management", flags.management ? options.management : "self");
|
|
14
|
+
if (flags.project) query.set("project", options.project);
|
|
15
|
+
if (flags.q) query.set("q", options.q);
|
|
16
|
+
|
|
17
|
+
const payload = await api(`/api/infra/assets?${query.toString()}`);
|
|
18
|
+
printInfraResult(payload, options.json, dependencies.stdout, "list");
|
|
19
|
+
},
|
|
20
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { requireSinglePositional } from "../lib/args.mjs";
|
|
2
|
+
import { printInfraResult } from "../lib/format.mjs";
|
|
3
|
+
import { getInfraAsset } from "../lib/infra.mjs";
|
|
4
|
+
|
|
5
|
+
export const infraShowCommand = {
|
|
6
|
+
name: "show",
|
|
7
|
+
options: {},
|
|
8
|
+
async run({ options, positional, dependencies, api }) {
|
|
9
|
+
const reference = requireSinglePositional(positional, "資産名またはid");
|
|
10
|
+
const payload = await getInfraAsset(reference, api);
|
|
11
|
+
printInfraResult(payload, options.json, dependencies.stdout, "show");
|
|
12
|
+
},
|
|
13
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { requireSinglePositional, usageError } from "../lib/args.mjs";
|
|
2
|
+
import { printInfraResult } from "../lib/format.mjs";
|
|
3
|
+
import { getInfraAsset, projectSlugs, requireInfraVersion } from "../lib/infra.mjs";
|
|
4
|
+
|
|
5
|
+
export const infraUnlinkCommand = {
|
|
6
|
+
name: "unlink",
|
|
7
|
+
options: { allowed: ["project"], repeatable: ["project"] },
|
|
8
|
+
async run({ options, positional, dependencies, api }) {
|
|
9
|
+
const reference = requireSinglePositional(positional, "資産名またはid");
|
|
10
|
+
const removals = options.project.map((slug) => slug.trim());
|
|
11
|
+
if (removals.some((slug) => slug === "")) throw usageError("--project に空文字は指定できません");
|
|
12
|
+
|
|
13
|
+
const current = await getInfraAsset(reference, api);
|
|
14
|
+
const removed = new Set(removals);
|
|
15
|
+
const slugs = removals.length === 0
|
|
16
|
+
? []
|
|
17
|
+
: projectSlugs(current.item).filter((slug) => !removed.has(slug));
|
|
18
|
+
const payload = await api(`/api/infra/assets/${encodeURIComponent(current.item.id)}/projects`, {
|
|
19
|
+
method: "PUT",
|
|
20
|
+
body: JSON.stringify({ version: requireInfraVersion(current.item), project_slugs: slugs }),
|
|
21
|
+
});
|
|
22
|
+
printInfraResult(payload, options.json, dependencies.stdout, "projects");
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { isConflictError, withGuidance } from "../lib/api.mjs";
|
|
2
|
+
import { cliError, requireSinglePositional, usageError } from "../lib/args.mjs";
|
|
3
|
+
import { printInfraResult } from "../lib/format.mjs";
|
|
4
|
+
import {
|
|
5
|
+
INFRA_DOMAIN_ONLY_OPTIONS,
|
|
6
|
+
getInfraAsset,
|
|
7
|
+
getInfraAssetById,
|
|
8
|
+
requireInfraVersion,
|
|
9
|
+
} from "../lib/infra.mjs";
|
|
10
|
+
|
|
11
|
+
const OPTION_FIELDS = {
|
|
12
|
+
status: "status",
|
|
13
|
+
"auto-renew": "auto_renew",
|
|
14
|
+
expires: "expires_on",
|
|
15
|
+
"dns-host": "dns_host",
|
|
16
|
+
"mail-host": "mail_host",
|
|
17
|
+
"migration-plan": "migration_plan",
|
|
18
|
+
management: "management",
|
|
19
|
+
purpose: "purpose",
|
|
20
|
+
provider: "provider",
|
|
21
|
+
"prod-platform": "prod_platform",
|
|
22
|
+
"prod-ref": "prod_ref",
|
|
23
|
+
contract: "contract",
|
|
24
|
+
amount: "amount_yen",
|
|
25
|
+
payer: "payer",
|
|
26
|
+
holder: "holder",
|
|
27
|
+
account: "account",
|
|
28
|
+
"account-ref": "account_ref",
|
|
29
|
+
note: "note",
|
|
30
|
+
"hosted-on-basis": "hosted_on_basis",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const ALLOWED_OPTIONS = [...Object.keys(OPTION_FIELDS), "append-note", "hosted-on"];
|
|
34
|
+
const NULLABLE_VALUE_OPTIONS = new Set([
|
|
35
|
+
"dns-host",
|
|
36
|
+
"mail-host",
|
|
37
|
+
"migration-plan",
|
|
38
|
+
"contract",
|
|
39
|
+
"hosted-on-basis",
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
function amountValue(value) {
|
|
43
|
+
if (value === "none") return null;
|
|
44
|
+
if (!/^\d+$/.test(value)) throw usageError("--amount は0以上の整数か none を指定してください");
|
|
45
|
+
const parsed = Number(value);
|
|
46
|
+
if (!Number.isSafeInteger(parsed)) throw usageError("--amount は安全な整数の範囲で指定してください");
|
|
47
|
+
return parsed;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function bodyFor(current, prepared, appendNote) {
|
|
51
|
+
const body = { version: requireInfraVersion(current), ...prepared };
|
|
52
|
+
if (appendNote !== null) {
|
|
53
|
+
const note = typeof current.note === "string" ? current.note.trim() : "";
|
|
54
|
+
body.note = note === "" ? appendNote : `${note} / ${appendNote}`;
|
|
55
|
+
}
|
|
56
|
+
return body;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const infraUpdateCommand = {
|
|
60
|
+
name: "update",
|
|
61
|
+
options: { allowed: ALLOWED_OPTIONS },
|
|
62
|
+
async run({ options, positional, flags, dependencies, api }) {
|
|
63
|
+
const reference = requireSinglePositional(positional, "資産名またはid");
|
|
64
|
+
if (!ALLOWED_OPTIONS.some((name) => flags[name])) {
|
|
65
|
+
throw usageError(`更新するオプションを指定してください: ${ALLOWED_OPTIONS.map((name) => `--${name}`).join(" / ")}`);
|
|
66
|
+
}
|
|
67
|
+
if (flags.note && flags["append-note"]) {
|
|
68
|
+
throw usageError("--note と --append-note は同時に指定できません");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let currentPayload = await getInfraAsset(reference, api);
|
|
72
|
+
let current = currentPayload.item;
|
|
73
|
+
const forbidden = INFRA_DOMAIN_ONLY_OPTIONS.filter((name) => flags[name]);
|
|
74
|
+
if (current.kind === "server" && forbidden.length > 0) {
|
|
75
|
+
throw usageError(`server資産にはドメイン専用オプションを指定できません: ${forbidden.map((name) => `--${name}`).join(" / ")}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const prepared = {};
|
|
79
|
+
for (const [option, field] of Object.entries(OPTION_FIELDS)) {
|
|
80
|
+
if (!flags[option] || option === "note") continue;
|
|
81
|
+
const value = options[option].trim();
|
|
82
|
+
if (option === "expires") prepared[field] = value === "none" ? null : value;
|
|
83
|
+
else if (option === "amount") prepared[field] = amountValue(value);
|
|
84
|
+
else if (NULLABLE_VALUE_OPTIONS.has(option) && value === "none") prepared[field] = null;
|
|
85
|
+
else prepared[field] = value;
|
|
86
|
+
}
|
|
87
|
+
if (flags.note) prepared.note = options.note.trim();
|
|
88
|
+
|
|
89
|
+
let appendNote = null;
|
|
90
|
+
if (flags["append-note"]) {
|
|
91
|
+
appendNote = options["append-note"].trim();
|
|
92
|
+
if (appendNote === "") throw usageError("--append-note に空文字は指定できません");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (flags["hosted-on"]) {
|
|
96
|
+
const hostReference = options["hosted-on"].trim();
|
|
97
|
+
if (hostReference === "none") {
|
|
98
|
+
prepared.hosted_on = null;
|
|
99
|
+
} else {
|
|
100
|
+
const host = await getInfraAsset(hostReference, api, { kind: "server" });
|
|
101
|
+
if (host.item.kind !== "server") throw cliError("--hosted-on にはserver資産の名前またはidを指定してください");
|
|
102
|
+
prepared.hosted_on = host.item.id;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
107
|
+
try {
|
|
108
|
+
const payload = await api(`/api/infra/assets/${encodeURIComponent(current.id)}`, {
|
|
109
|
+
method: "PATCH",
|
|
110
|
+
body: JSON.stringify(bodyFor(current, prepared, appendNote)),
|
|
111
|
+
});
|
|
112
|
+
printInfraResult(payload, options.json, dependencies.stdout, "update");
|
|
113
|
+
return;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (!isConflictError(error)) throw error;
|
|
116
|
+
if (attempt === 1) {
|
|
117
|
+
throw withGuidance(error, "再取得後にも競合しました。最新の内容を確認してから、もう一度実行してください");
|
|
118
|
+
}
|
|
119
|
+
currentPayload = await getInfraAssetById(current.id, api);
|
|
120
|
+
current = currentPayload.item;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
};
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
PROJECT_ENTITY_TYPES,
|
|
3
|
+
PROJECT_KINDS,
|
|
4
|
+
PROJECT_LINK_ROLES,
|
|
5
|
+
PROJECT_SLUG_PATTERN,
|
|
6
|
+
cliError,
|
|
7
|
+
requireSinglePositional,
|
|
8
|
+
usageError,
|
|
9
|
+
} from "../lib/args.mjs";
|
|
2
10
|
import { printProjectResult, toProjectLinkInput } from "../lib/format.mjs";
|
|
3
11
|
|
|
4
12
|
/** `--add-link` の値がURL単独かどうか。**= で割るより先に見る**(指示 §5.6・Astra High 4)。 */
|
|
@@ -14,7 +22,10 @@ const ordinal = (index) => `${index + 1} 番目`;
|
|
|
14
22
|
export const projectUpdateCommand = {
|
|
15
23
|
name: "update",
|
|
16
24
|
options: {
|
|
17
|
-
allowed: [
|
|
25
|
+
allowed: [
|
|
26
|
+
"name", "repo", "slug", "kind", "entity-type", "parent",
|
|
27
|
+
"add-link", "link-role", "remove-link", "remove-link-url", "clear-links",
|
|
28
|
+
],
|
|
18
29
|
boolean: ["clear-links"],
|
|
19
30
|
repeatable: ["add-link", "remove-link", "remove-link-url"],
|
|
20
31
|
},
|
|
@@ -31,12 +42,28 @@ export const projectUpdateCommand = {
|
|
|
31
42
|
}
|
|
32
43
|
|
|
33
44
|
const hasLinkChange = flags["add-link"] || flags["remove-link"] || flags["remove-link-url"] || flags["clear-links"];
|
|
34
|
-
if (
|
|
35
|
-
|
|
45
|
+
if (
|
|
46
|
+
!flags.name && !flags.repo && !flags.slug && !flags.kind
|
|
47
|
+
&& !flags["entity-type"] && !flags.parent && !hasLinkChange
|
|
48
|
+
) {
|
|
49
|
+
throw usageError("--name / --repo / --slug / --kind / --entity-type / --parent / --add-link / --remove-link / --remove-link-url / --clear-links のいずれかを指定してください");
|
|
36
50
|
}
|
|
37
51
|
if (flags.kind && !PROJECT_KINDS.includes(options.kind)) {
|
|
38
52
|
throw usageError(`--kind は ${PROJECT_KINDS.join(" / ")} のいずれかです`);
|
|
39
53
|
}
|
|
54
|
+
// 種類とグループ(記録180)。**語彙と形だけ**を先に見る——親が本当に group か、
|
|
55
|
+
// 配下の案件が居るかは Worker が書き込みと同じ文の中で見る。
|
|
56
|
+
if (flags["entity-type"] && !PROJECT_ENTITY_TYPES.includes(options["entity-type"])) {
|
|
57
|
+
throw usageError(`--entity-type は ${PROJECT_ENTITY_TYPES.join(" / ")} のいずれかです`);
|
|
58
|
+
}
|
|
59
|
+
const parentValue = flags.parent ? options.parent.trim() : null;
|
|
60
|
+
if (parentValue !== null && parentValue !== "none" && !PROJECT_SLUG_PATTERN.test(parentValue)) {
|
|
61
|
+
throw usageError("--parent は英小文字・数字で始まるslugか none を指定してください");
|
|
62
|
+
}
|
|
63
|
+
// グループは親を持てない(1段)。APIも400にするが、往復を省く
|
|
64
|
+
if (options["entity-type"] === "group" && flags.parent && parentValue !== "none") {
|
|
65
|
+
throw usageError("--entity-type group と --parent <slug> は同時に指定できません");
|
|
66
|
+
}
|
|
40
67
|
if (flags["clear-links"] && (flags["add-link"] || flags["remove-link"] || flags["remove-link-url"])) {
|
|
41
68
|
throw usageError("--clear-links と --add-link / --remove-link / --remove-link-url は同時に指定できません");
|
|
42
69
|
}
|
|
@@ -90,6 +117,9 @@ export const projectUpdateCommand = {
|
|
|
90
117
|
body.slug = options.slug.trim();
|
|
91
118
|
}
|
|
92
119
|
if (flags.kind) body.kind = options.kind;
|
|
120
|
+
if (flags["entity-type"]) body.entity_type = options["entity-type"];
|
|
121
|
+
// `none` は**文字列ではなく null**(「グループを外す」の指定・変異表 G17)
|
|
122
|
+
if (flags.parent) body.parent_slug = parentValue === "none" ? null : parentValue;
|
|
93
123
|
if (hasLinkChange) {
|
|
94
124
|
// 応答形(system / role_inferred 付き)のまま送り返さない。§3.5 の入力形へ戻してから触る
|
|
95
125
|
const currentLinks = Array.isArray(current.item.links) ? current.item.links : [];
|
package/lib/args.mjs
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
/** プロジェクトの区分。共有範囲のvisibility(private|team)とは別の軸。 */
|
|
2
2
|
export const PROJECT_KINDS = ["work", "private"];
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* 行の種類(記録180)。`group`=グループ(案件の上の層)で、`kind` とも別の軸。
|
|
6
|
+
*
|
|
7
|
+
* **CLIが持つのは語彙だけ**で、親子の規則(親は group だけ・1段・子がいる group は
|
|
8
|
+
* 閉じない)は Worker が見る。ここに置くのは「GETより前に打ち間違いを弾く」ためだけ。
|
|
9
|
+
* 語が増えたら Worker の `PROJECT_ENTITY_TYPES` と同時に直すこと。
|
|
10
|
+
*/
|
|
11
|
+
export const PROJECT_ENTITY_TYPES = ["project", "group"];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `--parent` のslugの形(Worker の `SLUG_PATTERN` と同じ)。
|
|
15
|
+
* `none` は「グループを外す」の指定で、これに当てはめない。
|
|
16
|
+
*/
|
|
17
|
+
export const PROJECT_SLUG_PATTERN = /^[a-z0-9][a-z0-9_-]{0,99}$/;
|
|
18
|
+
|
|
4
19
|
/**
|
|
5
20
|
* 参照リンクの役割(`2026-09-16 プロジェクト参照リンクの実装指示.md` §1.2 の9語)。
|
|
6
21
|
*
|
|
@@ -53,6 +68,7 @@ export const PROJECT_USAGE = `使い方:
|
|
|
53
68
|
hub project create <slug> --name <text> [--kind work|private] [--repo <text>] [--json]
|
|
54
69
|
hub project update <slug> [--name <text>] [--repo <text>] [--slug <new-slug>]
|
|
55
70
|
[--kind work|private]
|
|
71
|
+
[--entity-type project|group] [--parent <slug>|none]
|
|
56
72
|
[--add-link <URL>]... [--add-link "<ラベル>=<URL>"]...
|
|
57
73
|
[--link-role <role>]
|
|
58
74
|
[--remove-link "<ラベル>"]... [--remove-link-url <URL>]...
|
|
@@ -73,15 +89,53 @@ export const PROJECT_USAGE = `使い方:
|
|
|
73
89
|
--remove-link-url はURL完全一致で1件だけ消す(該当なし・複数該当はエラー)。
|
|
74
90
|
--clear-links は全消し(--add-link / --remove-link / --remove-link-url と同時指定不可)。
|
|
75
91
|
--kind は仕事/プライベートの区分(既定 work)。共有範囲のvisibilityとは別。
|
|
92
|
+
--entity-type は案件(project)かグループ(group)か。グループは案件の上の層で、
|
|
93
|
+
親を持てない(1段)。配下の案件があるグループは、削除と案件(project)への変更が
|
|
94
|
+
1件でも子があれば拒否され、終了(status=closed)は**終了していない子**があるときだけ
|
|
95
|
+
拒否される(子が全部終了していればグループも終了にできる)。
|
|
96
|
+
--parent は所属するグループのslug。--parent none で外す。指定できるのは group だけで、
|
|
97
|
+
--entity-type group と --parent <slug> は同時に指定できない。
|
|
76
98
|
list は既定で全件出す(--kind を付けたときだけ絞る)。
|
|
77
99
|
オプションは --name=value の形でも書ける(値が -- で始まるときはこの形で渡す)。
|
|
78
100
|
`;
|
|
79
101
|
|
|
102
|
+
export const INFRA_USAGE = `使い方:
|
|
103
|
+
hub infra list [--kind server|domain] [--status <status[,status...]>] [--management self|external|all]
|
|
104
|
+
[--project <slug>] [--q <text>] [--json]
|
|
105
|
+
hub infra show <name|id> [--json]
|
|
106
|
+
hub infra update <name|id> [--status <status>] [--auto-renew on|off|unknown]
|
|
107
|
+
[--expires YYYY-MM-DD|none] [--dns-host <value>|none]
|
|
108
|
+
[--mail-host <value>|none] [--migration-plan <value>|none]
|
|
109
|
+
[--management self|external] [--purpose <text>]
|
|
110
|
+
[--provider <value>] [--prod-platform <value>]
|
|
111
|
+
[--prod-ref <text>] [--contract <value>|none]
|
|
112
|
+
[--amount <円>|none] [--payer <text>] [--holder <text>]
|
|
113
|
+
[--account <text>] [--account-ref <text>]
|
|
114
|
+
[--note <text>] [--append-note <text>]
|
|
115
|
+
[--hosted-on <server name|id>|none]
|
|
116
|
+
[--hosted-on-basis <value>|none] [--json]
|
|
117
|
+
hub infra link <name|id> --project <slug> [--project <slug>]... [--json]
|
|
118
|
+
hub infra unlink <name|id> [--project <slug>]... [--json]
|
|
119
|
+
|
|
120
|
+
list は期限の近い順。既定は --management self で、--management all は全件を返す。
|
|
121
|
+
list の --status はカンマ区切りで複数指定できる。
|
|
122
|
+
show / update / link / unlink の資産は名前の完全一致またはidで指定する。
|
|
123
|
+
update は指定した項目だけを更新する。version競合時は最新を取り直して1回だけ再試行する。
|
|
124
|
+
--append-note は既存noteの末尾へ「 / 」で追記する。
|
|
125
|
+
server資産には dns-host / mail-host / migration-plan / hosted-on / hosted-on-basis /
|
|
126
|
+
prod-platform / prod-ref を指定できない。
|
|
127
|
+
link は現在の紐付けへ追加し、unlink は指定分を外す。unlink の --project 省略は全解除。
|
|
128
|
+
作成・削除はCLIから行えない。
|
|
129
|
+
オプションは --name=value の形でも書ける(値が -- で始まるときはこの形で渡す)。
|
|
130
|
+
`;
|
|
131
|
+
|
|
80
132
|
export const USAGE = `使い方:
|
|
81
133
|
hub task <verb> ... タスク操作(new / add / claim / next / show / report / note / list)
|
|
82
134
|
hub task help
|
|
83
135
|
hub project <verb> ... プロジェクト操作(list / show / create / update / move)
|
|
84
136
|
hub project help
|
|
137
|
+
hub infra <verb> ... インフラ資産操作(list / show / update / link / unlink)
|
|
138
|
+
hub infra help
|
|
85
139
|
hub --version 入っているCLIの版を出す(hub version も同じ)
|
|
86
140
|
|
|
87
141
|
環境変数:
|
|
@@ -118,7 +172,7 @@ export function usageError(message) {
|
|
|
118
172
|
}
|
|
119
173
|
|
|
120
174
|
export function usageLineFor(group, verb) {
|
|
121
|
-
const usage = group === "task" ? TASK_USAGE : PROJECT_USAGE;
|
|
175
|
+
const usage = group === "task" ? TASK_USAGE : group === "project" ? PROJECT_USAGE : INFRA_USAGE;
|
|
122
176
|
const lines = usage.split("\n");
|
|
123
177
|
const prefix = ` hub ${group} ${verb}`;
|
|
124
178
|
const start = lines.findIndex((line) => line === prefix || line.startsWith(`${prefix} `));
|
package/lib/cli.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//
|
|
5
5
|
// hub task <verb> ... タスク操作
|
|
6
6
|
// hub project <verb> ... プロジェクト操作(読み取り + 作成・更新)
|
|
7
|
+
// hub infra <verb> ... インフラ資産操作(読み取り + 更新・紐付け)
|
|
7
8
|
//
|
|
8
9
|
// 全サブコマンドが `--json` を受け付け、付けると生のAPI応答をそのまま出力する。
|
|
9
10
|
//
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
redactSecrets,
|
|
23
24
|
} from "./api.mjs";
|
|
24
25
|
import {
|
|
26
|
+
INFRA_USAGE,
|
|
25
27
|
PROJECT_USAGE,
|
|
26
28
|
TASK_USAGE,
|
|
27
29
|
USAGE,
|
|
@@ -29,6 +31,11 @@ import {
|
|
|
29
31
|
parseCommandArgs,
|
|
30
32
|
usageLineFor,
|
|
31
33
|
} from "./args.mjs";
|
|
34
|
+
import { infraLinkCommand } from "../commands/infra-link.mjs";
|
|
35
|
+
import { infraListCommand } from "../commands/infra-list.mjs";
|
|
36
|
+
import { infraShowCommand } from "../commands/infra-show.mjs";
|
|
37
|
+
import { infraUnlinkCommand } from "../commands/infra-unlink.mjs";
|
|
38
|
+
import { infraUpdateCommand } from "../commands/infra-update.mjs";
|
|
32
39
|
import { projectCreateCommand } from "../commands/project-create.mjs";
|
|
33
40
|
import { projectListCommand } from "../commands/project-list.mjs";
|
|
34
41
|
import { projectMoveCommand } from "../commands/project-move.mjs";
|
|
@@ -87,6 +94,16 @@ const COMMAND_GROUPS = {
|
|
|
87
94
|
projectUpdateCommand,
|
|
88
95
|
]),
|
|
89
96
|
},
|
|
97
|
+
infra: {
|
|
98
|
+
usage: INFRA_USAGE,
|
|
99
|
+
commands: commandMap([
|
|
100
|
+
infraListCommand,
|
|
101
|
+
infraShowCommand,
|
|
102
|
+
infraUpdateCommand,
|
|
103
|
+
infraLinkCommand,
|
|
104
|
+
infraUnlinkCommand,
|
|
105
|
+
]),
|
|
106
|
+
},
|
|
90
107
|
};
|
|
91
108
|
|
|
92
109
|
/**
|
package/lib/format.mjs
CHANGED
|
@@ -51,6 +51,14 @@ export function projectLines(project, { includeLinks = false } = {}) {
|
|
|
51
51
|
else if (project.default_agent) lines.push(`agents: -(既定 ${project.default_agent})`);
|
|
52
52
|
if (project.last_activity_at) lines.push(`last_activity: ${project.last_activity_at}`);
|
|
53
53
|
if (project.kind && project.kind !== "work") lines.push(`kind: ${project.kind}`);
|
|
54
|
+
// 種類とグループ(記録180)。`kind` と同じ流儀で**既定でないときだけ**出す
|
|
55
|
+
// ——案件で親なしの行に毎回2行足すと、一覧が読みにくくなる。
|
|
56
|
+
// `children` は0件でも出す(「グループなのに空」が分かる。画面の帯と同じ)。
|
|
57
|
+
if (project.entity_type === "group") {
|
|
58
|
+
lines.push("type: group");
|
|
59
|
+
lines.push(`children: ${Number(project.children_count ?? 0)}`);
|
|
60
|
+
}
|
|
61
|
+
if (project.parent_slug) lines.push(`parent: ${project.parent_slug}`);
|
|
54
62
|
if (project.repo) lines.push(`repo: ${project.repo}`);
|
|
55
63
|
if (project.memory_home) lines.push(`memory: ${project.memory_home}`);
|
|
56
64
|
if (project.auto_policy && project.auto_policy !== "off") lines.push(`auto: ${project.auto_policy}`);
|
|
@@ -66,6 +74,35 @@ export function projectLines(project, { includeLinks = false } = {}) {
|
|
|
66
74
|
return lines;
|
|
67
75
|
}
|
|
68
76
|
|
|
77
|
+
/**
|
|
78
|
+
* `hub project show` の `deploy:` の1行(記録181・指示書 §5)。
|
|
79
|
+
*
|
|
80
|
+
* - 予約サイト [vercel/tamaitakamasa] discover-fuke running https://… (domain: fuke.example?)
|
|
81
|
+
*
|
|
82
|
+
* `platform_ref` が無ければ `(未照合)`——空文字にすると「名前が無い」ように見えて、
|
|
83
|
+
* 「まだ突き合わせていない」という事実が消える。台帳は**候補**なので `?` を付ける
|
|
84
|
+
* (links の `role?` と同じ流儀)。`url` と `repo` は無ければ省く。
|
|
85
|
+
*/
|
|
86
|
+
function deployLine(target) {
|
|
87
|
+
const name = String(target?.name ?? "").replace(/\s+/g, " ").trim();
|
|
88
|
+
const platform = String(target?.platform ?? "").trim();
|
|
89
|
+
const account = String(target?.account_label ?? target?.account_key ?? "").trim();
|
|
90
|
+
const ref = typeof target?.platform_ref === "string" && target.platform_ref.trim() !== ""
|
|
91
|
+
? target.platform_ref.trim()
|
|
92
|
+
: "(未照合)";
|
|
93
|
+
const parts = [name, `[${platform}/${account}]`, ref, String(target?.state ?? "").trim()];
|
|
94
|
+
const url = typeof target?.url === "string" ? target.url.trim() : "";
|
|
95
|
+
if (url !== "") parts.push(url);
|
|
96
|
+
const repo = typeof target?.repo === "string" ? target.repo.trim() : "";
|
|
97
|
+
if (repo !== "") {
|
|
98
|
+
const repoPath = typeof target?.repo_path === "string" ? target.repo_path.trim() : "";
|
|
99
|
+
parts.push(`repo: ${repo}${repoPath === "" ? "" : ` ${repoPath}`}`);
|
|
100
|
+
}
|
|
101
|
+
const domain = target?.domain_candidate;
|
|
102
|
+
if (domain && typeof domain.name === "string") parts.push(`(domain: ${domain.name}?)`);
|
|
103
|
+
return parts.filter((part) => part !== "").join(" ");
|
|
104
|
+
}
|
|
105
|
+
|
|
69
106
|
/**
|
|
70
107
|
* `[system/role]` の印(指示 §5.6)。role がWorkerの候補(`role_inferred: true`)なら `?` を付ける。
|
|
71
108
|
* 印が付かないのは system も role も無いときだけ —— 旧いAPIの応答をそのまま読めるようにしてある。
|
|
@@ -117,6 +154,21 @@ export function printProjectResult(payload, json, stdout, kind) {
|
|
|
117
154
|
}
|
|
118
155
|
const lines = projectLines(payload.item, { includeLinks: kind === "show" });
|
|
119
156
|
if (kind === "update") lines.push(`updated_tasks: ${payload.updated_tasks}`);
|
|
157
|
+
// 配下の案件(記録180)。`show` の応答にだけ載るので、あるときだけ並べる。
|
|
158
|
+
// 1行の形は `list` の見出し行と同じ(`<slug> [<status>] <name>`)
|
|
159
|
+
const children = Array.isArray(payload.children) ? payload.children : [];
|
|
160
|
+
if (children.length > 0) {
|
|
161
|
+
lines.push("children:");
|
|
162
|
+
for (const child of children) {
|
|
163
|
+
lines.push(`- ${child.slug} [${child.status}] ${child.name}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// デプロイ先(記録181)。`children:` の後・`activity:` の前。対象が無ければ行ごと出ない
|
|
167
|
+
const deployTargets = kind === "show" && Array.isArray(payload.deploy_targets) ? payload.deploy_targets : [];
|
|
168
|
+
if (deployTargets.length > 0) {
|
|
169
|
+
lines.push("deploy:");
|
|
170
|
+
for (const target of deployTargets) lines.push(`- ${deployLine(target)}`);
|
|
171
|
+
}
|
|
120
172
|
const activity = Array.isArray(payload.activity) ? payload.activity : [];
|
|
121
173
|
if (activity.length > 0) {
|
|
122
174
|
lines.push("", "activity:");
|
|
@@ -158,3 +210,84 @@ function formatEventValue(value) {
|
|
|
158
210
|
if (typeof value === "boolean") return value ? "オン" : "オフ";
|
|
159
211
|
return String(value) || "なし";
|
|
160
212
|
}
|
|
213
|
+
|
|
214
|
+
function displayValue(value) {
|
|
215
|
+
if (value === null || value === undefined || value === "") return "-";
|
|
216
|
+
if (typeof value === "object") return JSON.stringify(value);
|
|
217
|
+
return String(value).replace(/\s+/g, " ").trim();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function remainingDays(expiresOn, now = new Date()) {
|
|
221
|
+
if (typeof expiresOn !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(expiresOn)) return "-";
|
|
222
|
+
const today = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate());
|
|
223
|
+
const expiry = Date.parse(`${expiresOn}T00:00:00.000Z`);
|
|
224
|
+
if (Number.isNaN(expiry)) return "-";
|
|
225
|
+
const days = Math.round((expiry - today) / 86_400_000);
|
|
226
|
+
if (days < 0) return `${Math.abs(days)}日超過`;
|
|
227
|
+
if (days === 0) return "本日期限";
|
|
228
|
+
return `残${days}日`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function infraListLine(item, now = new Date()) {
|
|
232
|
+
const projects = Array.isArray(item?.projects)
|
|
233
|
+
? item.projects.map((project) => displayValue(project?.slug)).filter((slug) => slug !== "-")
|
|
234
|
+
: [];
|
|
235
|
+
const external = item?.management === "external" ? " [他社管理]" : "";
|
|
236
|
+
return [
|
|
237
|
+
displayValue(item?.name),
|
|
238
|
+
`[${displayValue(item?.kind)}]`,
|
|
239
|
+
`provider:${displayValue(item?.provider)}`,
|
|
240
|
+
`status:${displayValue(item?.status)}`,
|
|
241
|
+
`auto-renew:${displayValue(item?.auto_renew)}`,
|
|
242
|
+
`expires:${displayValue(item?.expires_on)} (${remainingDays(item?.expires_on, now)})`,
|
|
243
|
+
`projects:${projects.join(",") || "-"}${external}`,
|
|
244
|
+
].join(" ");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function infraAssetLines(item) {
|
|
248
|
+
const fields = [
|
|
249
|
+
"id", "provider", "status", "management", "auto_renew", "expires_on", "purpose",
|
|
250
|
+
"dns_host", "mail_host", "migration_plan", "hosted_on_basis", "prod_platform", "prod_ref",
|
|
251
|
+
"contract", "amount_yen", "payer", "holder", "account", "account_ref", "note", "version",
|
|
252
|
+
"created_at", "updated_at",
|
|
253
|
+
];
|
|
254
|
+
const lines = [`${displayValue(item?.name)} [${displayValue(item?.kind)}]`];
|
|
255
|
+
for (const field of fields) lines.push(`${field}: ${displayValue(item?.[field])}`);
|
|
256
|
+
if (item?.hosted_on !== undefined) {
|
|
257
|
+
const hosted = item.hosted_on === null
|
|
258
|
+
? "-"
|
|
259
|
+
: `${displayValue(item.hosted_on.name)} (${displayValue(item.hosted_on.id)})`;
|
|
260
|
+
lines.splice(11, 0, `hosted_on: ${hosted}`);
|
|
261
|
+
}
|
|
262
|
+
const projects = Array.isArray(item?.projects)
|
|
263
|
+
? item.projects.map((project) => `${displayValue(project?.slug)}(${displayValue(project?.name)})`)
|
|
264
|
+
: [];
|
|
265
|
+
lines.push(`projects: ${projects.join(", ") || "-"}`);
|
|
266
|
+
if (item?.kind === "server") lines.push(`hosted_count: ${displayValue(item?.hosted_count)}`);
|
|
267
|
+
return lines;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function printInfraResult(payload, json, stdout, kind) {
|
|
271
|
+
if (json) {
|
|
272
|
+
stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (kind === "list") {
|
|
276
|
+
const items = payload?.items ?? [];
|
|
277
|
+
if (items.length === 0) {
|
|
278
|
+
stdout.write("インフラ資産はありません。\n");
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
stdout.write(`${items.map((item) => infraListLine(item)).join("\n")}\n`);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const lines = infraAssetLines(payload?.item);
|
|
285
|
+
if (kind === "show" && Array.isArray(payload?.events) && payload.events.length > 0) {
|
|
286
|
+
lines.push("", "events:");
|
|
287
|
+
for (const event of payload.events.slice(0, 5)) {
|
|
288
|
+
const note = event?.note ? ` — ${displayValue(event.note)}` : "";
|
|
289
|
+
lines.push(`- ${displayValue(event?.at)} ${displayValue(event?.kind)} (${displayValue(event?.actor)})${note}`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
stdout.write(`${lines.join("\n")}\n`);
|
|
293
|
+
}
|
package/lib/infra.mjs
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { cliError } from "./args.mjs";
|
|
2
|
+
|
|
3
|
+
const ULID_PATTERN = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
4
|
+
|
|
5
|
+
/** server 行では API の PATCH に送れない、domain 専用のCLIオプション。 */
|
|
6
|
+
export const INFRA_DOMAIN_ONLY_OPTIONS = [
|
|
7
|
+
"dns-host",
|
|
8
|
+
"mail-host",
|
|
9
|
+
"migration-plan",
|
|
10
|
+
"hosted-on",
|
|
11
|
+
"hosted-on-basis",
|
|
12
|
+
"prod-platform",
|
|
13
|
+
"prod-ref",
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
function oneLine(value) {
|
|
17
|
+
return String(value ?? "").replace(/\s+/g, " ").trim();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function levenshtein(left, right) {
|
|
21
|
+
const previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
22
|
+
for (let leftIndex = 0; leftIndex < left.length; leftIndex += 1) {
|
|
23
|
+
const current = [leftIndex + 1];
|
|
24
|
+
for (let rightIndex = 0; rightIndex < right.length; rightIndex += 1) {
|
|
25
|
+
current.push(Math.min(
|
|
26
|
+
current[rightIndex] + 1,
|
|
27
|
+
previous[rightIndex + 1] + 1,
|
|
28
|
+
previous[rightIndex] + (left[leftIndex] === right[rightIndex] ? 0 : 1),
|
|
29
|
+
));
|
|
30
|
+
}
|
|
31
|
+
previous.splice(0, previous.length, ...current);
|
|
32
|
+
}
|
|
33
|
+
return previous[right.length];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function candidateLines(items, reference) {
|
|
37
|
+
const query = reference.toLowerCase();
|
|
38
|
+
return items
|
|
39
|
+
.map((item) => {
|
|
40
|
+
const name = oneLine(item.name);
|
|
41
|
+
const normalized = name.toLowerCase();
|
|
42
|
+
const contains = normalized.includes(query) || query.includes(normalized) ? 0 : 1;
|
|
43
|
+
return { item, name, contains, distance: levenshtein(normalized, query) };
|
|
44
|
+
})
|
|
45
|
+
.sort((left, right) => left.contains - right.contains
|
|
46
|
+
|| left.distance - right.distance
|
|
47
|
+
|| left.name.localeCompare(right.name, "ja"))
|
|
48
|
+
.slice(0, 3)
|
|
49
|
+
.map(({ item, name }) => `- ${name} [${oneLine(item.kind)}] (${oneLine(item.id)})`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function assertItem(item) {
|
|
53
|
+
if (item === null || typeof item !== "object" || Array.isArray(item)
|
|
54
|
+
|| typeof item.id !== "string" || typeof item.name !== "string") {
|
|
55
|
+
throw cliError("infra API応答のitemが不正です");
|
|
56
|
+
}
|
|
57
|
+
return item;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function requireInfraVersion(item) {
|
|
61
|
+
if (!Number.isSafeInteger(item?.version) || item.version < 1) {
|
|
62
|
+
throw cliError("infra API応答のversionが不正です");
|
|
63
|
+
}
|
|
64
|
+
return item.version;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* CLIが受ける `<name|id>` を資産IDへ解決する。
|
|
69
|
+
*
|
|
70
|
+
* 名前は一覧全体(management=all)に対する完全一致だけを採用する。同じ名前が server / domain
|
|
71
|
+
* の両方にあれば曖昧なので勝手に選ばない。候補は表示密度を守るため近い順の3件まで。
|
|
72
|
+
*/
|
|
73
|
+
export async function resolveInfraAssetId(reference, api, { kind } = {}) {
|
|
74
|
+
const target = reference.trim();
|
|
75
|
+
if (ULID_PATTERN.test(target)) return target.toUpperCase();
|
|
76
|
+
|
|
77
|
+
const payload = await api("/api/infra/assets?management=all");
|
|
78
|
+
if (!Array.isArray(payload?.items)) throw cliError("infra一覧のAPI応答が不正です");
|
|
79
|
+
const items = payload.items.filter((item) => item !== null && typeof item === "object"
|
|
80
|
+
&& typeof item.id === "string" && typeof item.name === "string"
|
|
81
|
+
&& (kind === undefined || item.kind === kind));
|
|
82
|
+
const normalized = target.toLowerCase();
|
|
83
|
+
const exact = items.filter((item) => item.name.toLowerCase() === normalized);
|
|
84
|
+
if (exact.length === 1) return exact[0].id;
|
|
85
|
+
|
|
86
|
+
const candidates = candidateLines(exact.length > 1 ? exact : items, target);
|
|
87
|
+
const reason = exact.length > 1
|
|
88
|
+
? `同名の資産が複数あります: ${target}`
|
|
89
|
+
: kind === "server"
|
|
90
|
+
? `サーバーの資産を指定してください: ${target}`
|
|
91
|
+
: `資産が見つかりません: ${target}`;
|
|
92
|
+
throw cliError(`${reason}\n候補:\n${candidates.join("\n") || "- 該当なし"}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function getInfraAssetById(id, api) {
|
|
96
|
+
const payload = await api(`/api/infra/assets/${encodeURIComponent(id)}`);
|
|
97
|
+
return { ...payload, item: assertItem(payload?.item) };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function getInfraAsset(reference, api, options = {}) {
|
|
101
|
+
const id = await resolveInfraAssetId(reference, api, options);
|
|
102
|
+
return await getInfraAssetById(id, api);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function projectSlugs(item) {
|
|
106
|
+
if (!Array.isArray(item?.projects)) return [];
|
|
107
|
+
return item.projects
|
|
108
|
+
.map((project) => oneLine(project?.slug))
|
|
109
|
+
.filter((slug, index, all) => slug !== "" && all.indexOf(slug) === index);
|
|
110
|
+
}
|