forked from SenoOh/fgalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
113 lines (94 loc) · 3.68 KB
/
main.js
File metadata and controls
113 lines (94 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import inquirer from 'inquirer';
import chalk from 'chalk';
// 作成処理のインポート
import createMain from './src/create/index.js';
// 更新処理のインポート
import { fetchAndSaveData } from './src/update/api/openFgaClient.js';
import { analyzeStatistics, printStatistics } from './src/update/cli/statisticsAnalyzer.js';
import { runInteractiveCLI } from './src/update/cli/interactiveCli.js';
import dotenv from 'dotenv';
// 環境変数を読み込み
dotenv.config({ quiet: true });
async function main() {
try {
console.log(chalk.blue.bold('=== OpenFGA 設定管理ツール ==='));
console.log('');
// ユーザーに操作を選択させる
const { operation } = await inquirer.prompt([
{
type: 'list',
name: 'operation',
message: '実行する操作を選択してください:',
choices: [
{
name: '🆕 新しいOpenFGA設定を作成する',
value: 'create'
},
{
name: '🔄 既存のOpenFGA設定を更新・分析する',
value: 'update'
}
]
}
]);
console.log('');
if (operation === 'create') {
// 作成処理を実行
console.log(chalk.green('新しいOpenFGA設定の作成を開始します...'));
console.log('');
await createMain();
} else if (operation === 'update') {
// 更新処理を実行
await runUpdateProcess();
}
} catch (error) {
console.error(chalk.red('エラーが発生しました:'), error.message);
process.exit(1);
}
}
async function runUpdateProcess() {
try {
// 環境変数から設定を取得
const apiUrl = process.env.FGA_API_URL;
const storeId = process.env.FGA_STORE_ID;
const apiToken = process.env.FGA_API_TOKEN;
console.log(chalk.blue.bold('=== OpenFGA データ取得・保存・分析ツール ==='));
console.log(chalk.gray(`API URL: ${apiUrl}`));
console.log(chalk.gray(`Store ID: ${storeId}`));
console.log('');
// データを取得してファイルに保存
console.log(chalk.yellow('OpenFGAからデータを取得中...'));
const allData = await fetchAndSaveData(apiUrl, storeId, apiToken);
// 分析モジュールで統計情報を分析・表示
const statistics = analyzeStatistics(allData.relationshipTuples, allData.authorizationModel, './file/json/matter');
printStatistics(statistics);
console.log('');
console.log(chalk.green('=== 取得結果サマリー ==='));
console.log(chalk.gray(`Authorization Model Schema Version: ${allData.authorizationModel?.schema_version}`));
console.log(chalk.gray(`Relationship Tuples 数: ${allData.relationshipTuples?.length || 0}`));
console.log('');
console.log(chalk.green('ファイル保存が完了しました:'));
console.log(chalk.gray('- ./file/update/model.fga (Authorization Model DSL)'));
console.log(chalk.gray('- ./file/update/tuple.json (Relationship Tuples JSON)'));
// CLI対話モードの開始
console.log(chalk.magenta('\n=== 対話モード開始 ==='));
// OpenFGAデータを統合してCLIに渡す
const openFGAData = {
authorizationModel: allData.authorizationModel,
relationshipTuples: allData.relationshipTuples,
statistics: statistics
};
// OpenFGA API設定
const openFGAConfig = {
apiUrl: apiUrl,
storeId: storeId,
apiToken: apiToken
};
await runInteractiveCLI(statistics, openFGAData, openFGAConfig);
} catch (error) {
console.error(chalk.red('更新処理でエラーが発生しました:'), error.message);
throw error;
}
}
// メイン関数を実行
main();