elestio 1.0.3 → 1.2.0

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.
@@ -1,146 +1,146 @@
1
- import { loadConfig, saveConfig, saveCredentials, getCredentials, CREDENTIALS_PATH } from '../config.js';
2
- import { authenticate } from '../api.js';
3
- import { log, colors, outputJson } from '../utils.js';
4
-
5
- export async function configure(email, token, json) {
6
- if (!email || !token) {
7
- throw new Error('Both --email and --token are required');
8
- }
9
-
10
- log('info', 'Testing authentication...');
11
- const auth = await authenticate(email, token);
12
-
13
- saveCredentials(email, token);
14
-
15
- const config = loadConfig();
16
- config.jwt = auth.jwt;
17
- config.jwtExpiry = auth.jwtExpiry;
18
- saveConfig(config);
19
-
20
- if (json) {
21
- outputJson({ status: 'ok', email });
22
- return true;
23
- }
24
-
25
- log('success', `Configured for ${email}`);
26
- log('info', `Credentials saved to ~/.elestio/credentials`);
27
- return true;
28
- }
29
-
30
- export function showConfig(json) {
31
- const config = loadConfig();
32
- const creds = getCredentials();
33
-
34
- if (json) {
35
- outputJson({
36
- email: creds.email,
37
- hasToken: !!creds.apiToken,
38
- hasJwt: !!config.jwt,
39
- jwtExpiry: config.jwtExpiry ? new Date(config.jwtExpiry).toISOString() : null,
40
- defaultProject: config.defaultProject,
41
- defaults: config.defaults
42
- });
43
- return;
44
- }
45
-
46
- console.log(`\n${colors.bold}Elestio Configuration${colors.reset}\n`);
47
- console.log(` Email: ${creds.email || '(not set)'}`);
48
- console.log(` API Token: ${creds.apiToken ? creds.apiToken.slice(0, 8) + '...' : '(not set)'}`);
49
- console.log(` Credentials in: ~/.elestio/credentials`);
50
- console.log(` JWT: ${config.jwt ? 'present (expires ' + new Date(config.jwtExpiry).toISOString() + ')' : '(not set)'}`);
51
- console.log(` Default Project: ${config.defaultProject || '(not set)'}`);
52
- console.log(`\n${colors.bold}Defaults${colors.reset}`);
53
- console.log(` Provider: ${config.defaults?.provider || 'netcup'}`);
54
- console.log(` Datacenter: ${config.defaults?.datacenter || 'nbg'}`);
55
- console.log(` Server Type: ${config.defaults?.serverType || 'MEDIUM-2C-4G'}`);
56
- console.log(` Support: ${config.defaults?.support || 'level1'}`);
57
- console.log('');
58
- }
59
-
60
- export function setDefaultProject(projectId) {
61
- const config = loadConfig();
62
- config.defaultProject = String(projectId);
63
- saveConfig(config);
64
- log('success', `Default project set to ${projectId}`);
65
- }
66
-
67
- export function setDefaults(provider, datacenter, serverType, support) {
68
- const config = loadConfig();
69
- if (!config.defaults) config.defaults = {};
70
- if (provider) config.defaults.provider = provider;
71
- if (datacenter) config.defaults.datacenter = datacenter;
72
- if (serverType) config.defaults.serverType = serverType;
73
- if (support) config.defaults.support = support;
74
- saveConfig(config);
75
- log('success', 'Defaults updated');
76
- }
77
-
78
- export async function testAuth(json) {
79
- const creds = getCredentials();
80
-
81
- if (!creds.email || !creds.apiToken) {
82
- throw new Error('Not configured. Run: elestio login');
83
- }
84
-
85
- log('info', `Testing authentication for ${creds.email}...`);
86
-
87
- const auth = await authenticate(creds.email, creds.apiToken);
88
- const config = loadConfig();
89
- config.jwt = auth.jwt;
90
- config.jwtExpiry = auth.jwtExpiry;
91
- saveConfig(config);
92
-
93
- if (json) {
94
- outputJson({ status: 'ok', email: creds.email, jwtExpiry: new Date(auth.jwtExpiry).toISOString() });
95
- return true;
96
- }
97
-
98
- log('success', `Authenticated as ${creds.email}`);
99
- log('info', `JWT valid until ${new Date(auth.jwtExpiry).toISOString()}`);
100
- return true;
101
- }
102
-
103
- export async function getDefaultProject() {
104
- const { apiRequest } = await import('../api.js');
105
- const config = loadConfig();
106
-
107
- if (config.defaultProject) {
108
- return config.defaultProject;
109
- }
110
-
111
- const response = await apiRequest('/api/projects/getList');
112
- if (response.status === 'OK' && response.data?.projects?.length > 0) {
113
- const firstProject = response.data.projects[0];
114
- config.defaultProject = String(firstProject.projectID);
115
- saveConfig(config);
116
- log('info', `Using project "${firstProject.project_name}" (${firstProject.projectID}) as default`);
117
- return config.defaultProject;
118
- }
119
-
120
- throw new Error('No projects found. Create a project first.');
121
- }
122
-
123
- export function whoami(json) {
124
- const creds = getCredentials();
125
- const config = loadConfig();
126
-
127
- if (!creds.email) {
128
- throw new Error('Not logged in. Run: elestio login');
129
- }
130
-
131
- if (json) {
132
- outputJson({
133
- email: creds.email,
134
- defaultProject: config.defaultProject,
135
- provider: config.defaults?.provider,
136
- datacenter: config.defaults?.datacenter
137
- });
138
- return;
139
- }
140
-
141
- console.log(`\n${colors.bold}Logged in as${colors.reset} ${colors.cyan}${creds.email}${colors.reset}`);
142
- if (config.defaultProject) {
143
- console.log(` Default project: ${config.defaultProject}`);
144
- }
145
- console.log('');
146
- }
1
+ import { loadConfig, saveConfig, saveCredentials, getCredentials } from '../config.js';
2
+ import { authenticate } from '../api.js';
3
+ import { log, colors, outputJson } from '../utils.js';
4
+
5
+ export async function configure(email, token, json) {
6
+ if (!email || !token) {
7
+ throw new Error('Both --email and --token are required');
8
+ }
9
+
10
+ log('info', 'Testing authentication...');
11
+ const auth = await authenticate(email, token);
12
+
13
+ saveCredentials(email, token);
14
+
15
+ const config = loadConfig();
16
+ config.jwt = auth.jwt;
17
+ config.jwtExpiry = auth.jwtExpiry;
18
+ saveConfig(config);
19
+
20
+ if (json) {
21
+ outputJson({ status: 'ok', email });
22
+ return true;
23
+ }
24
+
25
+ log('success', `Configured for ${email}`);
26
+ log('info', `Credentials saved to ~/.elestio/credentials`);
27
+ return true;
28
+ }
29
+
30
+ export function showConfig(json) {
31
+ const config = loadConfig();
32
+ const creds = getCredentials();
33
+
34
+ if (json) {
35
+ outputJson({
36
+ email: creds.email,
37
+ hasToken: !!creds.apiToken,
38
+ hasJwt: !!config.jwt,
39
+ jwtExpiry: config.jwtExpiry ? new Date(config.jwtExpiry).toISOString() : null,
40
+ defaultProject: config.defaultProject,
41
+ defaults: config.defaults
42
+ });
43
+ return;
44
+ }
45
+
46
+ console.log(`\n${colors.bold}Elestio Configuration${colors.reset}\n`);
47
+ console.log(` Email: ${creds.email || '(not set)'}`);
48
+ console.log(` API Token: ${creds.apiToken ? creds.apiToken.slice(0, 8) + '...' : '(not set)'}`);
49
+ console.log(` Credentials in: ~/.elestio/credentials`);
50
+ console.log(` JWT: ${config.jwt ? 'present (expires ' + new Date(config.jwtExpiry).toISOString() + ')' : '(not set)'}`);
51
+ console.log(` Default Project: ${config.defaultProject || '(not set)'}`);
52
+ console.log(`\n${colors.bold}Defaults${colors.reset}`);
53
+ console.log(` Provider: ${config.defaults?.provider || 'netcup'}`);
54
+ console.log(` Datacenter: ${config.defaults?.datacenter || 'nbg'}`);
55
+ console.log(` Server Type: ${config.defaults?.serverType || 'MEDIUM-2C-4G'}`);
56
+ console.log(` Support: ${config.defaults?.support || 'level1'}`);
57
+ console.log('');
58
+ }
59
+
60
+ export function setDefaultProject(projectId) {
61
+ const config = loadConfig();
62
+ config.defaultProject = String(projectId);
63
+ saveConfig(config);
64
+ log('success', `Default project set to ${projectId}`);
65
+ }
66
+
67
+ export function setDefaults(provider, datacenter, serverType, support) {
68
+ const config = loadConfig();
69
+ if (!config.defaults) config.defaults = {};
70
+ if (provider) config.defaults.provider = provider;
71
+ if (datacenter) config.defaults.datacenter = datacenter;
72
+ if (serverType) config.defaults.serverType = serverType;
73
+ if (support) config.defaults.support = support;
74
+ saveConfig(config);
75
+ log('success', 'Defaults updated');
76
+ }
77
+
78
+ export async function testAuth(json) {
79
+ const creds = getCredentials();
80
+
81
+ if (!creds.email || !creds.apiToken) {
82
+ throw new Error('Not configured. Run: elestio login');
83
+ }
84
+
85
+ log('info', `Testing authentication for ${creds.email}...`);
86
+
87
+ const auth = await authenticate(creds.email, creds.apiToken);
88
+ const config = loadConfig();
89
+ config.jwt = auth.jwt;
90
+ config.jwtExpiry = auth.jwtExpiry;
91
+ saveConfig(config);
92
+
93
+ if (json) {
94
+ outputJson({ status: 'ok', email: creds.email, jwtExpiry: new Date(auth.jwtExpiry).toISOString() });
95
+ return true;
96
+ }
97
+
98
+ log('success', `Authenticated as ${creds.email}`);
99
+ log('info', `JWT valid until ${new Date(auth.jwtExpiry).toISOString()}`);
100
+ return true;
101
+ }
102
+
103
+ export async function getDefaultProject() {
104
+ const { apiRequest } = await import('../api.js');
105
+ const config = loadConfig();
106
+
107
+ if (config.defaultProject) {
108
+ return config.defaultProject;
109
+ }
110
+
111
+ const response = await apiRequest('/api/projects/getList');
112
+ if (response.status === 'OK' && response.data?.projects?.length > 0) {
113
+ const firstProject = response.data.projects[0];
114
+ config.defaultProject = String(firstProject.projectID);
115
+ saveConfig(config);
116
+ log('info', `Using project "${firstProject.project_name}" (${firstProject.projectID}) as default`);
117
+ return config.defaultProject;
118
+ }
119
+
120
+ throw new Error('No projects found. Create a project first.');
121
+ }
122
+
123
+ export function whoami(json) {
124
+ const creds = getCredentials();
125
+ const config = loadConfig();
126
+
127
+ if (!creds.email) {
128
+ throw new Error('Not logged in. Run: elestio login');
129
+ }
130
+
131
+ if (json) {
132
+ outputJson({
133
+ email: creds.email,
134
+ defaultProject: config.defaultProject,
135
+ provider: config.defaults?.provider,
136
+ datacenter: config.defaults?.datacenter
137
+ });
138
+ return;
139
+ }
140
+
141
+ console.log(`\n${colors.bold}Logged in as${colors.reset} ${colors.cyan}${creds.email}${colors.reset}`);
142
+ if (config.defaultProject) {
143
+ console.log(` Default project: ${config.defaultProject}`);
144
+ }
145
+ console.log('');
146
+ }