Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 217x 217x 217x 217x 217x 217x 217x 2x 2x 1x 1x 174x 174x 174x 43x 43x 43x 21x 21x 21x 21x 3x 18x 3x 15x 15x 15x 15x 757x 757x 757x 63x 63x 63x 2x 2x 2x 2x 76x 76x 1x 75x 60x 60x 60x 14x 14x 1x 13x 9x 9x 1x 8x 2x 2x 2x 3x 3x 3x 1x 1x | /**
* Persistent CLI defaults stored in the metadata KV table.
*
* All defaults use metadata keys prefixed with `defaults.`:
* - `defaults.org` — default organization slug
* - `defaults.project` — default project slug
* - `defaults.telemetry` — telemetry preference (`"on"` / `"off"`)
* - `defaults.url` — Sentry instance URL (for self-hosted)
*/
import { getDatabase } from "./index.js";
import { clearMetadata, getMetadata, setMetadata } from "./utils.js";
const DEFAULTS_ORG = "defaults.org";
const DEFAULTS_PROJECT = "defaults.project";
const DEFAULTS_TELEMETRY = "defaults.telemetry";
const DEFAULTS_URL = "defaults.url";
const DEFAULTS_HEADERS = "defaults.headers";
const DEFAULTS_CA_CERT = "defaults.ca-cert";
/** All metadata keys used for defaults (for bulk operations) */
const ALL_DEFAULTS_KEYS = [
DEFAULTS_ORG,
DEFAULTS_PROJECT,
DEFAULTS_TELEMETRY,
DEFAULTS_URL,
DEFAULTS_HEADERS,
DEFAULTS_CA_CERT,
];
/** State of all persistent defaults */
export type DefaultsState = {
/** Default organization slug, or null if unset */
organization: string | null;
/** Default project slug, or null if unset */
project: string | null;
/** Telemetry preference: "on", "off", or null (= default enabled) */
telemetry: "on" | "off" | null;
/** Default Sentry instance URL, or null if unset */
url: string | null;
/** Custom HTTP headers for self-hosted proxy auth, or null if unset */
headers: string | null;
/** Path to a PEM file with custom CA certificates, or null if unset */
"ca-cert": string | null;
};
/** Parse a raw telemetry metadata value to a typed "on" | "off" | null. */
function parseTelemetryValue(val: string | undefined): "on" | "off" | null {
Iif (val === "on") {
return "on";
}
if (val === "off") {
return "off";
}
return null;
}
// ---------------------------------------------------------------------------
// Getters
// ---------------------------------------------------------------------------
/** Get the default organization slug, or null if not set. */
export function getDefaultOrganization(): string | null {
const db = getDatabase();
const m = getMetadata(db, [DEFAULTS_ORG]);
return m.get(DEFAULTS_ORG) ?? null;
}
/** Get the default project slug, or null if not set. */
export function getDefaultProject(): string | null {
const db = getDatabase();
const m = getMetadata(db, [DEFAULTS_PROJECT]);
return m.get(DEFAULTS_PROJECT) ?? null;
}
/**
* Get the persistent telemetry preference.
*
* @returns `true` if explicitly enabled, `false` if explicitly disabled,
* `undefined` if no preference is stored (callers should default to enabled)
*/
export function getTelemetryPreference(): boolean | undefined {
const db = getDatabase();
const m = getMetadata(db, [DEFAULTS_TELEMETRY]);
const val = m.get(DEFAULTS_TELEMETRY);
if (val === "on") {
return true;
}
if (val === "off") {
return false;
}
return;
}
/** Get the default Sentry instance URL, or null if not set. */
export function getDefaultUrl(): string | null {
const db = getDatabase();
const m = getMetadata(db, [DEFAULTS_URL]);
return m.get(DEFAULTS_URL) ?? null;
}
/**
* Get the default custom headers string, or null if not set.
* Format: semicolon-separated `Name: Value` pairs.
*/
export function getDefaultHeaders(): string | null {
const db = getDatabase();
const m = getMetadata(db, [DEFAULTS_HEADERS]);
return m.get(DEFAULTS_HEADERS) ?? null;
}
/**
* Get the stored CA certificate file path, or null if not set.
* Used by `custom-ca.ts` as the highest-priority CA source.
*/
export function getDefaultCaCert(): string | null {
const db = getDatabase();
const m = getMetadata(db, [DEFAULTS_CA_CERT]);
return m.get(DEFAULTS_CA_CERT) ?? null;
}
/**
* Get all persistent defaults as a structured object.
* Used by the `sentry cli defaults` show mode and JSON output.
*/
export function getAllDefaults(): DefaultsState {
const db = getDatabase();
const m = getMetadata(db, ALL_DEFAULTS_KEYS);
const telVal = m.get(DEFAULTS_TELEMETRY);
return {
organization: m.get(DEFAULTS_ORG) ?? null,
project: m.get(DEFAULTS_PROJECT) ?? null,
telemetry: parseTelemetryValue(telVal),
url: m.get(DEFAULTS_URL) ?? null,
headers: m.get(DEFAULTS_HEADERS) ?? null,
"ca-cert": m.get(DEFAULTS_CA_CERT) ?? null,
};
}
// ---------------------------------------------------------------------------
// Setters (null = clear the value)
// ---------------------------------------------------------------------------
/** Set or clear the default organization. Pass `null` to clear. */
export function setDefaultOrganization(value: string | null): void {
const db = getDatabase();
if (value === null) {
clearMetadata(db, [DEFAULTS_ORG]);
} else {
setMetadata(db, { [DEFAULTS_ORG]: value });
}
}
/** Set or clear the default project. Pass `null` to clear. */
export function setDefaultProject(value: string | null): void {
const db = getDatabase();
Iif (value === null) {
clearMetadata(db, [DEFAULTS_PROJECT]);
} else {
setMetadata(db, { [DEFAULTS_PROJECT]: value });
}
}
/**
* Set or clear the persistent telemetry preference.
* Pass `null` to remove the preference (callers will default to enabled).
*/
export function setTelemetryPreference(enabled: boolean | null): void {
const db = getDatabase();
if (enabled === null) {
clearMetadata(db, [DEFAULTS_TELEMETRY]);
} else {
setMetadata(db, { [DEFAULTS_TELEMETRY]: enabled ? "on" : "off" });
}
}
/** Set or clear the default Sentry instance URL. Pass `null` to clear. */
export function setDefaultUrl(url: string | null): void {
const db = getDatabase();
if (url === null) {
clearMetadata(db, [DEFAULTS_URL]);
} else {
setMetadata(db, { [DEFAULTS_URL]: url });
}
}
/**
* Set or clear the default custom headers. Pass `null` to clear.
* Value should be semicolon-separated `Name: Value` pairs.
*/
export function setDefaultHeaders(value: string | null): void {
const db = getDatabase();
Iif (value === null) {
clearMetadata(db, [DEFAULTS_HEADERS]);
} else {
setMetadata(db, { [DEFAULTS_HEADERS]: value });
}
}
/**
* Set or clear the stored CA certificate file path. Pass `null` to clear.
* The path should point to a PEM file containing CA certificates.
*/
export function setDefaultCaCert(path: string | null): void {
const db = getDatabase();
Iif (path === null) {
clearMetadata(db, [DEFAULTS_CA_CERT]);
} else {
setMetadata(db, { [DEFAULTS_CA_CERT]: path });
}
}
// ---------------------------------------------------------------------------
// Bulk operations
// ---------------------------------------------------------------------------
/** Clear all persistent defaults (org, project, telemetry, url). */
export function clearAllDefaults(): void {
const db = getDatabase();
clearMetadata(db, ALL_DEFAULTS_KEYS);
}
|