@enerlence/suntropy-cli 0.9.0 → 0.11.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.
@@ -398,6 +398,51 @@ function registerAuthCommands(program2) {
398
398
  outputError(handleApiError(err));
399
399
  }
400
400
  });
401
+ auth.command("logout").description(
402
+ "Clear stored credentials for the active profile (or --profile). Removes the\ntoken and account identity but keeps the profile and its server URL.\nUse --all to log out of every profile."
403
+ ).option("--all", "Clear credentials for ALL profiles").action(async (opts) => {
404
+ try {
405
+ const global = getGlobalOpts(auth);
406
+ const config = loadConfig();
407
+ const clearProfile = (name) => {
408
+ const profile = config.profiles[name];
409
+ if (!profile) return false;
410
+ const hadToken2 = !!profile.token;
411
+ delete profile.token;
412
+ delete profile.authMethod;
413
+ delete profile.email;
414
+ delete profile.clientUID;
415
+ delete profile.userUID;
416
+ return hadToken2;
417
+ };
418
+ if (opts.all) {
419
+ const names = Object.keys(config.profiles);
420
+ const clearedWithToken = names.filter(clearProfile);
421
+ saveConfig(config);
422
+ output({
423
+ success: true,
424
+ message: `Logged out of ${names.length} profile(s).`,
425
+ profiles: names,
426
+ hadCredentials: clearedWithToken
427
+ }, global);
428
+ return;
429
+ }
430
+ const profileName = global.profile || config.activeProfile;
431
+ if (!config.profiles[profileName]) {
432
+ outputError(new Error(`Profile "${profileName}" not found. Available: ${Object.keys(config.profiles).join(", ")}`));
433
+ return;
434
+ }
435
+ const hadToken = clearProfile(profileName);
436
+ saveConfig(config);
437
+ output({
438
+ success: true,
439
+ profile: profileName,
440
+ message: hadToken ? `Logged out of profile "${profileName}". Credentials cleared (server kept).` : `Profile "${profileName}" had no stored credentials.`
441
+ }, global);
442
+ } catch (err) {
443
+ outputError(err);
444
+ }
445
+ });
401
446
  }
402
447
 
403
448
  // src/commands/config/shared.ts
@@ -4347,8 +4392,168 @@ function registerGeocodeCommands(program2) {
4347
4392
  });
4348
4393
  }
4349
4394
 
4395
+ // src/access.ts
4396
+ var COMMAND_TIERS = ["read", "write", "delete"];
4397
+ var TIER_VALUE = { read: 0, write: 1, delete: 2 };
4398
+ var DEFAULT_TIER = "delete";
4399
+ var ENV_VAR = "SUNTROPY_COMMAND_PROFILE";
4400
+ function tierValue(t) {
4401
+ return TIER_VALUE[t];
4402
+ }
4403
+ function isValidTier(value) {
4404
+ return typeof value === "string" && COMMAND_TIERS.includes(value);
4405
+ }
4406
+ function getActiveCommandProfile() {
4407
+ const envRaw = process.env[ENV_VAR]?.trim();
4408
+ if (envRaw) {
4409
+ const envNorm = envRaw.toLowerCase();
4410
+ if (isValidTier(envNorm)) return { tier: envNorm, source: "env" };
4411
+ const fallback = resolveFromConfig();
4412
+ return { ...fallback, invalidEnv: envRaw };
4413
+ }
4414
+ return resolveFromConfig();
4415
+ }
4416
+ function resolveFromConfig() {
4417
+ try {
4418
+ const cfg = loadConfig();
4419
+ if (isValidTier(cfg.commandProfile)) return { tier: cfg.commandProfile, source: "config" };
4420
+ } catch {
4421
+ }
4422
+ return { tier: DEFAULT_TIER, source: "default" };
4423
+ }
4424
+ function setStoredCommandProfile(tier) {
4425
+ const cfg = loadConfig();
4426
+ if (tier === null) delete cfg.commandProfile;
4427
+ else cfg.commandProfile = tier;
4428
+ saveConfig(cfg);
4429
+ }
4430
+ var DELETE_VERBS = /* @__PURE__ */ new Set(["delete", "delete-batch", "archive"]);
4431
+ var WRITE_VERBS = /* @__PURE__ */ new Set([
4432
+ "create",
4433
+ "update",
4434
+ "edit",
4435
+ "set",
4436
+ "add",
4437
+ "remove",
4438
+ "assemble",
4439
+ "featured",
4440
+ "save",
4441
+ "init",
4442
+ "init-default",
4443
+ "add-comment",
4444
+ "comment",
4445
+ "calculate-results",
4446
+ "optimize-peakpower"
4447
+ ]);
4448
+ var PATH_OVERRIDES = {
4449
+ "studies calculate production": "write"
4450
+ };
4451
+ function classifyPath(segs) {
4452
+ const key = segs.join(" ");
4453
+ if (PATH_OVERRIDES[key]) return PATH_OVERRIDES[key];
4454
+ let tier = "read";
4455
+ for (const seg of segs) {
4456
+ if (DELETE_VERBS.has(seg)) return "delete";
4457
+ if (WRITE_VERBS.has(seg)) tier = "write";
4458
+ }
4459
+ return tier;
4460
+ }
4461
+ var EXEMPT_CONFIG_LEAVES = /* @__PURE__ */ new Set(["set", "get", "list", "create-profile", "use"]);
4462
+ function isExemptPath(segs) {
4463
+ if (segs[0] === "auth") return true;
4464
+ if (segs[0] === "command-profile") return true;
4465
+ if (segs[0] === "config" && segs.length === 2 && EXEMPT_CONFIG_LEAVES.has(segs[1])) return true;
4466
+ return false;
4467
+ }
4468
+ function applyCommandProfile(program2) {
4469
+ const resolved = getActiveCommandProfile();
4470
+ pruneChildren(program2, [], tierValue(resolved.tier));
4471
+ return resolved;
4472
+ }
4473
+ function pruneChildren(parent, parentSegs, max) {
4474
+ const kept = parent.commands.filter((child) => {
4475
+ const segs = [...parentSegs, child.name()];
4476
+ return keepCommand(child, segs, max);
4477
+ });
4478
+ const mutable = parent.commands;
4479
+ mutable.length = 0;
4480
+ mutable.push(...kept);
4481
+ }
4482
+ function keepCommand(cmd, segs, max) {
4483
+ if (cmd.commands.length > 0) {
4484
+ pruneChildren(cmd, segs, max);
4485
+ if (cmd.commands.length > 0) return true;
4486
+ return isExemptPath(segs);
4487
+ }
4488
+ if (isExemptPath(segs)) return true;
4489
+ return tierValue(classifyPath(segs)) <= max;
4490
+ }
4491
+
4492
+ // src/commands/command-profile.ts
4493
+ function getGlobalOpts16(cmd) {
4494
+ let root = cmd;
4495
+ while (root.parent) root = root.parent;
4496
+ return root.opts();
4497
+ }
4498
+ function registerCommandProfileCommand(program2) {
4499
+ program2.command("command-profile [tier]", { hidden: true }).description("Admin: get/set the command access profile (read | write | delete | reset)").action((tier) => {
4500
+ try {
4501
+ const global = getGlobalOpts16(program2);
4502
+ if (!tier) {
4503
+ const resolved = getActiveCommandProfile();
4504
+ output(
4505
+ {
4506
+ commandProfile: resolved.tier,
4507
+ source: resolved.source,
4508
+ ...resolved.invalidEnv ? { warning: `Ignoring invalid ${ENV_VAR}="${resolved.invalidEnv}" (expected: ${COMMAND_TIERS.join(" | ")})` } : {},
4509
+ available: COMMAND_TIERS,
4510
+ envVar: ENV_VAR
4511
+ },
4512
+ global
4513
+ );
4514
+ return;
4515
+ }
4516
+ const value = tier.trim().toLowerCase();
4517
+ if (value === "reset") {
4518
+ setStoredCommandProfile(null);
4519
+ const resolved = getActiveCommandProfile();
4520
+ output(
4521
+ {
4522
+ success: true,
4523
+ message: `Stored command profile cleared. Effective tier: ${resolved.tier} (${resolved.source}).`,
4524
+ commandProfile: resolved.tier,
4525
+ source: resolved.source
4526
+ },
4527
+ global
4528
+ );
4529
+ return;
4530
+ }
4531
+ if (!isValidTier(value)) {
4532
+ outputError(new Error(`Invalid tier "${tier}". Expected one of: ${COMMAND_TIERS.join(", ")}, or "reset".`));
4533
+ return;
4534
+ }
4535
+ setStoredCommandProfile(value);
4536
+ const envOverride = getActiveCommandProfile();
4537
+ const message = envOverride.source === "env" ? `Stored command profile set to "${value}", but ${ENV_VAR}="${process.env[ENV_VAR]}" overrides it (effective: ${envOverride.tier}). Unset the env var for the stored value to apply.` : `Command profile set to "${value}". Takes effect on the next command.`;
4538
+ output(
4539
+ {
4540
+ success: true,
4541
+ commandProfile: value,
4542
+ effectiveTier: envOverride.tier,
4543
+ effectiveSource: envOverride.source,
4544
+ default: DEFAULT_TIER,
4545
+ message
4546
+ },
4547
+ global
4548
+ );
4549
+ } catch (err) {
4550
+ outputError(err);
4551
+ }
4552
+ });
4553
+ }
4554
+
4350
4555
  // src/index.ts
4351
- var CLI_VERSION = true ? "0.9.0" : "0.0.0-dev";
4556
+ var CLI_VERSION = true ? "0.11.0" : "0.0.0-dev";
4352
4557
  function createProgram() {
4353
4558
  const program2 = new Command3();
4354
4559
  program2.name("suntropy").description("Agent-first CLI for Suntropy solar platform. Optimized for programmatic data manipulation and progressive exploration.").version(CLI_VERSION).option("--format <format>", "Output format: json (default), human, csv", "json").option("--fields <fields>", "Comma-separated fields to include in output").option("--server <url>", "Override API server URL").option("--token <jwt>", "Override authentication token").option("--profile <name>", "Use a specific config profile").option("--verbose", "Show HTTP request/response details on stderr").option("--quiet", "Suppress non-data output").option("--save <file>", "Save output to file (also writes to stdout)");
@@ -4363,6 +4568,8 @@ function createProgram() {
4363
4568
  registerShareableCommands(program2);
4364
4569
  registerTemplatesCommands(program2);
4365
4570
  registerGeocodeCommands(program2);
4571
+ registerCommandProfileCommand(program2);
4572
+ applyCommandProfile(program2);
4366
4573
  return program2;
4367
4574
  }
4368
4575