|
1 | | -import { commands, Disposable } from "vscode"; |
| 1 | +import os from "os"; |
| 2 | +import { |
| 3 | + commands, |
| 4 | + Disposable, |
| 5 | + StatusBarAlignment, |
| 6 | + ThemeColor, |
| 7 | + window, |
| 8 | + workspace, |
| 9 | +} from "vscode"; |
2 | 10 | import type { Core } from "./core"; |
3 | 11 | import { AppAction, createOpenOPHandler } from "./url-utils"; |
4 | 12 | import { COMMANDS } from "./constants"; |
| 13 | +import { existsSync } from "fs"; |
| 14 | +import path from "path"; |
| 15 | +import { execFile } from "child_process"; |
5 | 16 |
|
6 | 17 | export class Environments { |
| 18 | + private disposables: Disposable[] = []; |
| 19 | + private item = window.createStatusBarItem( |
| 20 | + "op.mounted", |
| 21 | + StatusBarAlignment.Left, |
| 22 | + 1000, |
| 23 | + ); |
| 24 | + private refreshPending = false; |
| 25 | + private intervalHandle?: NodeJS.Timeout; |
| 26 | + |
7 | 27 | public constructor(private core: Core) { |
8 | 28 | commands.registerCommand(COMMANDS.IMPORT_PROJECT, async () => |
9 | 29 | createOpenOPHandler(this.core)({ |
10 | 30 | action: AppAction.ImportProject, |
11 | 31 | }), |
12 | 32 | ); |
| 33 | + |
| 34 | + const isWindows = os.platform() === "win32"; |
| 35 | + if (isWindows) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + this.item.name = "1Password: Mount Status"; |
| 40 | + core.context.subscriptions.push(this); |
| 41 | + |
| 42 | + // Start polling every 500ms |
| 43 | + this.intervalHandle = setInterval(() => { |
| 44 | + void this.refresh(); |
| 45 | + }, 500); |
| 46 | + this.disposables.push({ |
| 47 | + dispose: () => this.intervalHandle && clearInterval(this.intervalHandle), |
| 48 | + }); |
| 49 | + |
| 50 | + void this.refresh(); |
| 51 | + } |
| 52 | + |
| 53 | + public dispose(): void { |
| 54 | + for (const d of this.disposables) { |
| 55 | + d.dispose(); |
| 56 | + } |
| 57 | + this.item.dispose(); |
| 58 | + } |
| 59 | + |
| 60 | + private async refresh(): Promise<void> { |
| 61 | + if (this.refreshPending) { |
| 62 | + return; |
| 63 | + } |
| 64 | + this.refreshPending = true; |
| 65 | + |
| 66 | + try { |
| 67 | + const firstFolder = workspace.workspaceFolders?.[0]; |
| 68 | + const firstFsPath = firstFolder?.uri.fsPath; |
| 69 | + |
| 70 | + if (!firstFsPath) { |
| 71 | + this.setNoEnvironment(); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const dbPath = path.join( |
| 76 | + os.homedir(), |
| 77 | + "Library", |
| 78 | + "Group Containers", |
| 79 | + "2BUA8C4S2C.com.1password", |
| 80 | + "Library", |
| 81 | + "Application Support", |
| 82 | + "1Password", |
| 83 | + "Data", |
| 84 | + "debug", |
| 85 | + "1password.sqlite", |
| 86 | + ); |
| 87 | + |
| 88 | + if (!existsSync(dbPath)) { |
| 89 | + this.setNoEnvironment(); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const mounts = await this.getEnvironmentMounts(dbPath); |
| 94 | + const matching = mounts.find((m) => { |
| 95 | + const mountPath = m.mountPath.split("/").slice(0, -1).join("/"); |
| 96 | + return mountPath === firstFsPath; |
| 97 | + }); |
| 98 | + |
| 99 | + if (!matching) { |
| 100 | + this.setNoEnvironment(); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (!matching.isEnabled) { |
| 105 | + this.item.text = "$(close) Environment Disabled"; |
| 106 | + this.item.tooltip = `Mount to ${matching.environmentName} is disabled`; |
| 107 | + this.item.backgroundColor = new ThemeColor( |
| 108 | + "statusBarItem.warningBackground", |
| 109 | + ); |
| 110 | + this.item.show(); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + this.item.text = `$(check) Environment: ${matching.environmentName}`; |
| 115 | + this.item.tooltip = `Mounted to ${matching.environmentName}`; |
| 116 | + this.item.backgroundColor = undefined; |
| 117 | + this.item.show(); |
| 118 | + } catch { |
| 119 | + this.setNoEnvironment(); |
| 120 | + } finally { |
| 121 | + this.refreshPending = false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private setNoEnvironment(): void { |
| 126 | + this.item.text = "$(close) No Environment"; |
| 127 | + this.item.tooltip = |
| 128 | + "No 1Password environments were found for this workspace. Click to import."; |
| 129 | + this.item.backgroundColor = new ThemeColor( |
| 130 | + "statusBarItem.warningBackground", |
| 131 | + ); |
| 132 | + this.item.command = COMMANDS.IMPORT_PROJECT; |
| 133 | + this.item.show(); |
| 134 | + } |
| 135 | + |
| 136 | + private async getEnvironmentMounts(dbPath: string): Promise< |
| 137 | + { |
| 138 | + mountPath: string; |
| 139 | + environmentName: string; |
| 140 | + isEnabled: boolean; |
| 141 | + }[] |
| 142 | + > { |
| 143 | + const sqliteBin = "/usr/bin/sqlite3"; |
| 144 | + const sql = |
| 145 | + "SELECT key_name, hex(data) FROM objects WHERE key_name LIKE 'dev-environment-mount/%';"; |
| 146 | + |
| 147 | + const stdout = await new Promise<string>((resolve, reject) => { |
| 148 | + const args = ["-batch", "-noheader", "-cmd", ".mode tabs", dbPath, sql]; |
| 149 | + execFile( |
| 150 | + sqliteBin, |
| 151 | + args, |
| 152 | + { encoding: "utf8", maxBuffer: 10 * 1024 * 1024 }, |
| 153 | + (err, out, stderr) => { |
| 154 | + void stderr; |
| 155 | + if (err) { |
| 156 | + reject(err); |
| 157 | + return; |
| 158 | + } |
| 159 | + resolve(out); |
| 160 | + }, |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + const mounts: { |
| 165 | + mountPath: string; |
| 166 | + environmentName: string; |
| 167 | + isEnabled: boolean; |
| 168 | + }[] = []; |
| 169 | + const lines = stdout.split(/\r?\n/).filter((l) => l.trim().length > 0); |
| 170 | + for (const line of lines) { |
| 171 | + const [keyName, dataHex] = line.split("\t"); |
| 172 | + if (!keyName || !dataHex) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + try { |
| 177 | + const jsonStr = Buffer.from(dataHex.trim(), "hex").toString("utf8"); |
| 178 | + const parsed = JSON.parse(jsonStr) as { |
| 179 | + mountPath?: string; |
| 180 | + environmentName?: string; |
| 181 | + isEnabled?: boolean; |
| 182 | + }; |
| 183 | + if ( |
| 184 | + typeof parsed.mountPath === "string" && |
| 185 | + typeof parsed.environmentName === "string" && |
| 186 | + typeof parsed.isEnabled === "boolean" |
| 187 | + ) { |
| 188 | + mounts.push({ |
| 189 | + mountPath: parsed.mountPath, |
| 190 | + environmentName: parsed.environmentName, |
| 191 | + isEnabled: parsed.isEnabled, |
| 192 | + }); |
| 193 | + } |
| 194 | + } catch { |
| 195 | + // ignore malformed rows |
| 196 | + } |
| 197 | + } |
| 198 | + return mounts; |
13 | 199 | } |
14 | 200 | } |
0 commit comments