relight-cli 0.2.0 → 0.4.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.
package/src/lib/config.js CHANGED
@@ -11,23 +11,55 @@ export var CLOUD_NAMES = {
11
11
  cf: "Cloudflare",
12
12
  gcp: "GCP",
13
13
  aws: "AWS",
14
- slicervm: "SlicerVM",
15
14
  };
16
15
 
17
16
  export var CLOUD_IDS = Object.keys(CLOUD_NAMES);
18
17
 
18
+ export var SERVICE_TYPES = {
19
+ slicervm: { layer: "compute", name: "SlicerVM" },
20
+ neon: { layer: "db", name: "Neon" },
21
+ };
22
+
19
23
  export function getConfig() {
20
24
  if (!existsSync(CONFIG_PATH)) {
21
25
  console.error("Not authenticated. Run `relight auth` first.");
22
26
  process.exit(1);
23
27
  }
24
- return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
28
+ var config = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
29
+ return migrateSlicervm(config);
30
+ }
31
+
32
+ function migrateSlicervm(config) {
33
+ if (config.clouds && config.clouds.slicervm) {
34
+ if (!config.services) config.services = {};
35
+ if (!config.services.slicervm) {
36
+ var old = config.clouds.slicervm;
37
+ config.services.slicervm = {
38
+ layer: "compute",
39
+ type: "slicervm",
40
+ ...old,
41
+ };
42
+ }
43
+ delete config.clouds.slicervm;
44
+ if (config.default_cloud === "slicervm") {
45
+ delete config.default_cloud;
46
+ }
47
+ saveConfig(config);
48
+ }
49
+ // Migrate old "addons" key to "services"
50
+ if (config.addons && !config.services) {
51
+ config.services = config.addons;
52
+ delete config.addons;
53
+ saveConfig(config);
54
+ }
55
+ return config;
25
56
  }
26
57
 
27
58
  export function tryGetConfig() {
28
59
  if (!existsSync(CONFIG_PATH)) return null;
29
60
  try {
30
- return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
61
+ var config = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
62
+ return migrateSlicervm(config);
31
63
  } catch {
32
64
  return null;
33
65
  }
@@ -84,15 +116,141 @@ export function resolveCloudConfig(cloudId) {
84
116
  if (cloudId === "aws") {
85
117
  return { accessKeyId: cloud.accessKeyId, secretAccessKey: cloud.secretAccessKey, region: cloud.region };
86
118
  }
87
- if (cloudId === "slicervm") {
88
- var slicerCfg = { hostGroup: cloud.hostGroup, baseDomain: cloud.baseDomain };
89
- if (cloud.socketPath) {
90
- slicerCfg.socketPath = cloud.socketPath;
119
+ return cloud;
120
+ }
121
+
122
+ export function getServiceConfig(name) {
123
+ var config = getConfig();
124
+ var service = config.services && config.services[name];
125
+ if (!service) {
126
+ console.error(
127
+ `Service '${name}' not found. Run \`relight service add\` to register it.`
128
+ );
129
+ process.exit(1);
130
+ }
131
+ return service;
132
+ }
133
+
134
+ export function tryGetServiceConfig(name) {
135
+ var config = tryGetConfig();
136
+ if (!config || !config.services) return null;
137
+ return config.services[name] || null;
138
+ }
139
+
140
+ export function getRegisteredServices() {
141
+ var config = tryGetConfig();
142
+ if (!config || !config.services) return [];
143
+ return Object.entries(config.services).map(([name, service]) => ({
144
+ name,
145
+ ...service,
146
+ }));
147
+ }
148
+
149
+ export function saveServiceConfig(name, data) {
150
+ var config = tryGetConfig() || { clouds: {} };
151
+ if (!config.services) config.services = {};
152
+ config.services[name] = data;
153
+ saveConfig(config);
154
+ }
155
+
156
+ export function removeServiceConfig(name) {
157
+ var config = tryGetConfig();
158
+ if (!config || !config.services) return;
159
+ delete config.services[name];
160
+ saveConfig(config);
161
+ }
162
+
163
+ export function getCloudMeta(cloudId, key) {
164
+ var config = tryGetConfig();
165
+ if (!config || !config.clouds || !config.clouds[cloudId]) return undefined;
166
+ var meta = config.clouds[cloudId]._meta;
167
+ if (!meta) return undefined;
168
+ return key ? meta[key] : meta;
169
+ }
170
+
171
+ export function setCloudMeta(cloudId, key, value) {
172
+ var config = getConfig();
173
+ if (!config.clouds[cloudId]._meta) config.clouds[cloudId]._meta = {};
174
+ if (value === undefined) {
175
+ delete config.clouds[cloudId]._meta[key];
176
+ if (Object.keys(config.clouds[cloudId]._meta).length === 0) {
177
+ delete config.clouds[cloudId]._meta;
178
+ }
179
+ } else {
180
+ config.clouds[cloudId]._meta[key] = value;
181
+ }
182
+ saveConfig(config);
183
+ }
184
+
185
+ export function getServiceMeta(serviceName, key) {
186
+ var config = tryGetConfig();
187
+ if (!config || !config.services || !config.services[serviceName]) return undefined;
188
+ var meta = config.services[serviceName]._meta;
189
+ if (!meta) return undefined;
190
+ return key ? meta[key] : meta;
191
+ }
192
+
193
+ export function setServiceMeta(serviceName, key, value) {
194
+ var config = getConfig();
195
+ if (!config.services || !config.services[serviceName]) {
196
+ throw new Error(`Service '${serviceName}' not found.`);
197
+ }
198
+ if (!config.services[serviceName]._meta) config.services[serviceName]._meta = {};
199
+ if (value === undefined) {
200
+ delete config.services[serviceName]._meta[key];
201
+ if (Object.keys(config.services[serviceName]._meta).length === 0) {
202
+ delete config.services[serviceName]._meta;
203
+ }
204
+ } else {
205
+ config.services[serviceName]._meta[key] = value;
206
+ }
207
+ saveConfig(config);
208
+ }
209
+
210
+ // --- Database registry ---
211
+
212
+ export function getDatabaseConfig(name) {
213
+ var config = tryGetConfig();
214
+ if (!config || !config.databases) return null;
215
+ return config.databases[name] || null;
216
+ }
217
+
218
+ export function saveDatabaseConfig(name, data) {
219
+ var config = tryGetConfig() || { clouds: {} };
220
+ if (!config.databases) config.databases = {};
221
+ config.databases[name] = data;
222
+ saveConfig(config);
223
+ }
224
+
225
+ export function removeDatabaseConfig(name) {
226
+ var config = tryGetConfig();
227
+ if (!config || !config.databases) return;
228
+ delete config.databases[name];
229
+ saveConfig(config);
230
+ }
231
+
232
+ export function listDatabases() {
233
+ var config = tryGetConfig();
234
+ if (!config || !config.databases) return [];
235
+ return Object.entries(config.databases).map(([name, data]) => ({
236
+ name,
237
+ ...data,
238
+ }));
239
+ }
240
+
241
+ export function normalizeServiceConfig(service) {
242
+ if (service.type === "slicervm") {
243
+ var cfg = { hostGroup: service.hostGroup, baseDomain: service.baseDomain };
244
+ if (service.socketPath) {
245
+ cfg.socketPath = service.socketPath;
91
246
  } else {
92
- slicerCfg.apiUrl = cloud.apiUrl;
93
- slicerCfg.apiToken = cloud.token;
247
+ cfg.apiUrl = service.apiUrl;
248
+ cfg.apiToken = service.token;
94
249
  }
95
- return slicerCfg;
250
+ return cfg;
96
251
  }
97
- return cloud;
252
+ if (service.type === "neon") {
253
+ return { apiKey: service.apiKey };
254
+ }
255
+ return service;
98
256
  }
package/src/lib/link.js CHANGED
@@ -14,8 +14,14 @@ export function readLink() {
14
14
  }
15
15
  }
16
16
 
17
- export function linkApp(name, cloud, dns, db) {
18
- var data = { app: name, cloud };
17
+ export function linkApp(name, cloud, dns, db, compute) {
18
+ var data = { app: name };
19
+ if (compute) {
20
+ data.compute = compute;
21
+ }
22
+ if (cloud) {
23
+ data.cloud = cloud;
24
+ }
19
25
  if (dns && dns !== cloud) data.dns = dns;
20
26
  if (db && db !== cloud) data.db = db;
21
27
  writeFileSync(LINK_FILE, YAML.stringify(data));
@@ -53,3 +59,8 @@ export function resolveDb() {
53
59
  var linked = readLink();
54
60
  return linked?.db || null;
55
61
  }
62
+
63
+ export function resolveCompute() {
64
+ var linked = readLink();
65
+ return linked?.compute || null;
66
+ }