berget 0.0.2 → 0.0.4

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.
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ApiKeyService = void 0;
13
+ const client_1 = require("../client");
14
+ const error_handler_1 = require("../utils/error-handler");
15
+ class ApiKeyService {
16
+ constructor() {
17
+ this.client = (0, client_1.createAuthenticatedClient)();
18
+ }
19
+ static getInstance() {
20
+ if (!ApiKeyService.instance) {
21
+ ApiKeyService.instance = new ApiKeyService();
22
+ }
23
+ return ApiKeyService.instance;
24
+ }
25
+ listApiKeys() {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ try {
28
+ const { data, error } = yield this.client.GET('/v1/api-keys');
29
+ if (error) {
30
+ // Check if this is an authentication error
31
+ const errorObj = typeof error === 'string' ? JSON.parse(error) : error;
32
+ if (errorObj.status === 401) {
33
+ throw new Error(JSON.stringify({
34
+ error: "Authentication failed. Your session may have expired.",
35
+ code: "AUTH_FAILED",
36
+ details: "Please run 'berget login' to authenticate again."
37
+ }));
38
+ }
39
+ throw new Error(JSON.stringify(error));
40
+ }
41
+ return data || [];
42
+ }
43
+ catch (error) {
44
+ (0, error_handler_1.handleError)('Failed to list API keys', error);
45
+ throw error;
46
+ }
47
+ });
48
+ }
49
+ createApiKey(options) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ try {
52
+ const { data, error } = yield this.client.POST('/v1/api-keys', {
53
+ body: options
54
+ });
55
+ if (error)
56
+ throw new Error(JSON.stringify(error));
57
+ return data;
58
+ }
59
+ catch (error) {
60
+ console.error('Failed to create API key:', error);
61
+ throw error;
62
+ }
63
+ });
64
+ }
65
+ deleteApiKey(id) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ try {
68
+ const { error } = yield this.client.DELETE('/v1/api-keys/{id}', {
69
+ params: { path: { id } }
70
+ });
71
+ if (error)
72
+ throw new Error(JSON.stringify(error));
73
+ return true;
74
+ }
75
+ catch (error) {
76
+ console.error('Failed to delete API key:', error);
77
+ throw error;
78
+ }
79
+ });
80
+ }
81
+ rotateApiKey(id) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ try {
84
+ const { data, error } = yield this.client.PUT('/v1/api-keys/{id}/rotate', {
85
+ params: { path: { id } }
86
+ });
87
+ if (error)
88
+ throw new Error(JSON.stringify(error));
89
+ return data;
90
+ }
91
+ catch (error) {
92
+ console.error('Failed to rotate API key:', error);
93
+ throw error;
94
+ }
95
+ });
96
+ }
97
+ getApiKeyUsage(id) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ try {
100
+ const { data, error } = yield this.client.GET('/v1/api-keys/{id}/usage', {
101
+ params: { path: { id } }
102
+ });
103
+ if (error)
104
+ throw new Error(JSON.stringify(error));
105
+ return data;
106
+ }
107
+ catch (error) {
108
+ console.error('Failed to get API key usage:', error);
109
+ throw error;
110
+ }
111
+ });
112
+ }
113
+ }
114
+ exports.ApiKeyService = ApiKeyService;
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AuthService = void 0;
16
+ const client_1 = require("../client");
17
+ const open_1 = __importDefault(require("open"));
18
+ const chalk_1 = __importDefault(require("chalk"));
19
+ const error_handler_1 = require("../utils/error-handler");
20
+ class AuthService {
21
+ constructor() {
22
+ this.client = (0, client_1.createAuthenticatedClient)();
23
+ }
24
+ static getInstance() {
25
+ if (!AuthService.instance) {
26
+ AuthService.instance = new AuthService();
27
+ }
28
+ return AuthService.instance;
29
+ }
30
+ login() {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ try {
33
+ // Clear any existing token to ensure a fresh login
34
+ (0, client_1.clearAuthToken)();
35
+ console.log(chalk_1.default.blue('Initiating login process...'));
36
+ // Step 1: Initiate device authorization
37
+ const { data: deviceData, error: deviceError } = yield client_1.apiClient.POST('/v1/auth/device', {});
38
+ if (deviceError || !deviceData) {
39
+ throw new Error(deviceError ? JSON.stringify(deviceError) : 'Failed to get device authorization data');
40
+ }
41
+ // Display information to user
42
+ console.log(chalk_1.default.cyan('\nTo complete login:'));
43
+ console.log(chalk_1.default.cyan(`1. Open this URL: ${chalk_1.default.bold(deviceData.verification_url || 'https://auth.berget.ai/device')}`));
44
+ console.log(chalk_1.default.cyan(`2. Enter this code: ${chalk_1.default.bold(deviceData.user_code || '')}\n`));
45
+ // Try to open browser automatically
46
+ try {
47
+ if (deviceData.verification_url) {
48
+ yield (0, open_1.default)(deviceData.verification_url);
49
+ console.log(chalk_1.default.dim('Browser opened automatically. If it didn\'t open, please use the URL above.'));
50
+ }
51
+ }
52
+ catch (error) {
53
+ console.log(chalk_1.default.yellow('Could not open browser automatically. Please open the URL manually.'));
54
+ }
55
+ console.log(chalk_1.default.dim('\nWaiting for authentication to complete...'));
56
+ // Step 2: Poll for completion
57
+ const startTime = Date.now();
58
+ const expiresIn = deviceData.expires_in !== undefined ? deviceData.expires_in : 900;
59
+ const expiresAt = startTime + (expiresIn * 1000);
60
+ let pollInterval = (deviceData.interval || 5) * 1000;
61
+ const spinner = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
62
+ let spinnerIdx = 0;
63
+ while (Date.now() < expiresAt) {
64
+ // Wait for the polling interval
65
+ yield new Promise(resolve => setTimeout(resolve, pollInterval));
66
+ // Update spinner
67
+ process.stdout.write(`\r${chalk_1.default.blue(spinner[spinnerIdx])} Waiting for authentication...`);
68
+ spinnerIdx = (spinnerIdx + 1) % spinner.length;
69
+ // Check if authentication is complete
70
+ const deviceCode = deviceData.device_code || '';
71
+ const { data: tokenData, error: tokenError } = yield client_1.apiClient.POST('/v1/auth/device/token', {
72
+ body: {
73
+ device_code: deviceCode
74
+ }
75
+ });
76
+ if (tokenError) {
77
+ // Parse the error to get status and other details
78
+ const errorObj = typeof tokenError === 'string'
79
+ ? JSON.parse(tokenError)
80
+ : tokenError;
81
+ const status = errorObj.status || 0;
82
+ const errorCode = errorObj.code || '';
83
+ if (status === 401 || errorCode === 'AUTHORIZATION_PENDING') {
84
+ // Still waiting for user to complete authorization
85
+ continue;
86
+ }
87
+ else if (status === 429) {
88
+ // Slow down
89
+ pollInterval *= 2;
90
+ continue;
91
+ }
92
+ else if (status === 400) {
93
+ // Error or expired
94
+ if (errorCode === 'EXPIRED_TOKEN') {
95
+ console.log(chalk_1.default.red('\n\nAuthentication timed out. Please try again.'));
96
+ }
97
+ else if (errorCode !== 'AUTHORIZATION_PENDING') {
98
+ // Only show error if it's not the expected "still waiting" error
99
+ const errorMessage = errorObj.message || JSON.stringify(errorObj);
100
+ console.log(chalk_1.default.red(`\n\nError: ${errorMessage}`));
101
+ return false;
102
+ }
103
+ else {
104
+ // If it's AUTHORIZATION_PENDING, continue polling
105
+ continue;
106
+ }
107
+ return false;
108
+ }
109
+ else {
110
+ // For any other error, log it but continue polling
111
+ // This makes the flow more resilient to temporary issues
112
+ if (process.env.DEBUG) {
113
+ console.log(chalk_1.default.yellow(`\n\nReceived error: ${JSON.stringify(errorObj)}`));
114
+ console.log(chalk_1.default.yellow('Continuing to wait for authentication...'));
115
+ process.stdout.write(`\r${chalk_1.default.blue(spinner[spinnerIdx])} Waiting for authentication...`);
116
+ }
117
+ continue;
118
+ }
119
+ }
120
+ else if (tokenData && tokenData.token) {
121
+ // Success!
122
+ (0, client_1.saveAuthToken)(tokenData.token);
123
+ process.stdout.write('\r' + ' '.repeat(50) + '\r'); // Clear the spinner line
124
+ console.log(chalk_1.default.green('✓ Successfully logged in to Berget'));
125
+ if (tokenData.user) {
126
+ const user = tokenData.user;
127
+ console.log(chalk_1.default.green(`Logged in as ${user.name || user.email || 'User'}`));
128
+ }
129
+ return true;
130
+ }
131
+ }
132
+ console.log(chalk_1.default.red('\n\nAuthentication timed out. Please try again.'));
133
+ return false;
134
+ }
135
+ catch (error) {
136
+ (0, error_handler_1.handleError)('Login failed', error);
137
+ return false;
138
+ }
139
+ });
140
+ }
141
+ isAuthenticated() {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ try {
144
+ // Call an API endpoint that requires authentication
145
+ const { data, error } = yield this.client.GET('/v1/users/me');
146
+ return !!data && !error;
147
+ }
148
+ catch (_a) {
149
+ return false;
150
+ }
151
+ });
152
+ }
153
+ getUserProfile() {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ try {
156
+ const { data, error } = yield this.client.GET('/v1/users/me');
157
+ if (error)
158
+ throw new Error(JSON.stringify(error));
159
+ return data;
160
+ }
161
+ catch (error) {
162
+ (0, error_handler_1.handleError)('Failed to get user profile', error);
163
+ throw error;
164
+ }
165
+ });
166
+ }
167
+ }
168
+ exports.AuthService = AuthService;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ClusterService = void 0;
13
+ const client_1 = require("../client");
14
+ class ClusterService {
15
+ constructor() {
16
+ this.client = (0, client_1.createAuthenticatedClient)();
17
+ }
18
+ static getInstance() {
19
+ if (!ClusterService.instance) {
20
+ ClusterService.instance = new ClusterService();
21
+ }
22
+ return ClusterService.instance;
23
+ }
24
+ getClusterUsage(clusterId) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ try {
27
+ const { data, error } = yield this.client.GET('/v1/clusters/{clusterId}/usage', {
28
+ params: { path: { clusterId } },
29
+ });
30
+ if (error)
31
+ throw new Error(JSON.stringify(error));
32
+ return data;
33
+ }
34
+ catch (error) {
35
+ console.error('Failed to get cluster usage:', error);
36
+ throw error;
37
+ }
38
+ });
39
+ }
40
+ listClusters() {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ try {
43
+ const { data, error } = yield this.client.GET('/v1/clusters');
44
+ if (error)
45
+ throw new Error(JSON.stringify(error));
46
+ return (data === null || data === void 0 ? void 0 : data.data) || [];
47
+ }
48
+ catch (error) {
49
+ console.error('Failed to list clusters:', error);
50
+ throw error;
51
+ }
52
+ });
53
+ }
54
+ }
55
+ exports.ClusterService = ClusterService;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.CollaboratorService = void 0;
13
+ const client_1 = require("../client");
14
+ class CollaboratorService {
15
+ constructor() {
16
+ this.client = (0, client_1.createAuthenticatedClient)();
17
+ }
18
+ static getInstance() {
19
+ if (!CollaboratorService.instance) {
20
+ CollaboratorService.instance = new CollaboratorService();
21
+ }
22
+ return CollaboratorService.instance;
23
+ }
24
+ // This endpoint is not available in the API
25
+ addCollaborator(clusterId, githubUsername) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ throw new Error('This functionality is not available in the API');
28
+ });
29
+ }
30
+ // This endpoint is not available in the API
31
+ listCollaborators(clusterId) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ throw new Error('This functionality is not available in the API');
34
+ });
35
+ }
36
+ }
37
+ exports.CollaboratorService = CollaboratorService;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.FluxService = void 0;
13
+ const client_1 = require("../client");
14
+ class FluxService {
15
+ constructor() {
16
+ this.client = (0, client_1.createAuthenticatedClient)();
17
+ }
18
+ static getInstance() {
19
+ if (!FluxService.instance) {
20
+ FluxService.instance = new FluxService();
21
+ }
22
+ return FluxService.instance;
23
+ }
24
+ // This endpoint is not available in the API
25
+ installFlux(options) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ throw new Error('This functionality is not available in the API');
28
+ });
29
+ }
30
+ // This endpoint is not available in the API
31
+ bootstrapFlux(options) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ throw new Error('This functionality is not available in the API');
34
+ });
35
+ }
36
+ }
37
+ exports.FluxService = FluxService;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.HelmService = void 0;
13
+ const client_1 = require("../client");
14
+ class HelmService {
15
+ constructor() {
16
+ this.client = (0, client_1.createAuthenticatedClient)();
17
+ }
18
+ static getInstance() {
19
+ if (!HelmService.instance) {
20
+ HelmService.instance = new HelmService();
21
+ }
22
+ return HelmService.instance;
23
+ }
24
+ // This endpoint is not available in the API
25
+ addRepo(options) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ throw new Error('This functionality is not available in the API');
28
+ });
29
+ }
30
+ // This endpoint is not available in the API
31
+ installChart(options) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ throw new Error('This functionality is not available in the API');
34
+ });
35
+ }
36
+ }
37
+ exports.HelmService = HelmService;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.KubectlService = void 0;
13
+ const client_1 = require("../client");
14
+ class KubectlService {
15
+ constructor() {
16
+ this.client = (0, client_1.createAuthenticatedClient)();
17
+ }
18
+ static getInstance() {
19
+ if (!KubectlService.instance) {
20
+ KubectlService.instance = new KubectlService();
21
+ }
22
+ return KubectlService.instance;
23
+ }
24
+ // This endpoint is not available in the API
25
+ createNamespace(name) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ throw new Error('This functionality is not available in the API');
28
+ });
29
+ }
30
+ // This endpoint is not available in the API
31
+ applyConfiguration(filename) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ throw new Error('This functionality is not available in the API');
34
+ });
35
+ }
36
+ // This endpoint is not available in the API
37
+ getResources(resource, namespace) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ throw new Error('This functionality is not available in the API');
40
+ });
41
+ }
42
+ }
43
+ exports.KubectlService = KubectlService;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.handleError = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ /**
9
+ * Formats and prints error messages in a consistent way
10
+ */
11
+ function handleError(message, error) {
12
+ console.error(chalk_1.default.red(`Error: ${message}`));
13
+ // If the error is a string (like JSON.stringify(error))
14
+ if (typeof error === 'string') {
15
+ try {
16
+ // Try to parse it as JSON
17
+ const parsedError = JSON.parse(error);
18
+ if (parsedError.error) {
19
+ console.error(chalk_1.default.dim(`Details: ${parsedError.error}`));
20
+ if (parsedError.code) {
21
+ console.error(chalk_1.default.dim(`Code: ${parsedError.code}`));
22
+ }
23
+ }
24
+ else {
25
+ console.error(chalk_1.default.dim(`Details: ${error}`));
26
+ }
27
+ }
28
+ catch (_a) {
29
+ // If it's not valid JSON, just print the string
30
+ console.error(chalk_1.default.dim(`Details: ${error}`));
31
+ }
32
+ }
33
+ else if (error && error.message) {
34
+ // If it's an Error object
35
+ console.error(chalk_1.default.dim(`Details: ${error.message}`));
36
+ }
37
+ // Check for authentication errors
38
+ if ((typeof error === 'string' && error.includes('Unauthorized')) ||
39
+ (error && error.message && error.message.includes('Unauthorized')) ||
40
+ (error && error.code && error.code === 401)) {
41
+ console.error(chalk_1.default.yellow('\nYou need to be logged in to use this command.'));
42
+ console.error(chalk_1.default.yellow('Run `berget login` to authenticate.'));
43
+ }
44
+ }
45
+ exports.handleError = handleError;