Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
188 lines (173 loc) · 5.48 KB
/
Copy pathModel.js
File metadata and controls
188 lines (173 loc) · 5.48 KB
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
// omarchy-geode-plugin — Model.js (G0)
//
// Status model for the bar widget. Exports the `geode` command shape and
// parses the run result; the QML side owns the actual process spawn.
//
// Command: a human verb + the vault path + `--output json`.
//
// Red lines (SPEC-omarchy G0): no token flag, no token environment
// the plugin, and none of the agent/token/serve/seal/open/mount verbs.
// Missing binary (exit 127) and an empty vault path are a failed status
// object the bar can show — never a throw, never an empty success.
.pragma library
function pluginId() {
return "virtualmachinist.geode";
}
// The exact argv for a status listing. Human verb + vault + JSON output.
// Nothing else — no token flag, no token environment, no key material.
function listArgs(vaultPath) {
return ["geode", "list", vaultPath, "--output", "json"];
}
// True when the widget has a vault path set.
function hasVault(vaultPath) {
return typeof vaultPath === "string" && vaultPath.trim() !== "";
}
// Failed status the bar can show. Never throws, never ok:true.
function statusForEmptyVault() {
return {
ok: false,
reason: "vault path is not set — set it in the widget settings",
vault: "",
entries: [],
raw: ""
};
}
// Parse one geode run into a status object.
//
// code — process exit status (127 = binary not found)
// stdout — captured stdout (expected JSON when code === 0)
// stderr — captured stderr
//
// Returns:
// { ok: true, vault, entries, raw } — exit 0, stdout parsed, and the
// summary document says ok:true
// { ok: false, reason, ... } — non-zero exit, exit 127, stdout
// not JSON, or no ok:true summary
//
// stdout is either one JSON document (older geode) or newline-delimited
// geode.event.v1 documents (geode 1.2.x): one per entry carrying `path`,
// then a summary carrying `files`, which is a count. `entries` is always an
// array of path strings; the `files` count is never stored in it.
// Never throws. Never returns ok:true without parsed JSON.
function statusFromRun(code, stdout, stderr) {
var out = typeof stdout === "string" ? stdout : "";
var errText = typeof stderr === "string" ? stderr : "";
if (code === 127) {
return {
ok: false,
reason: "geode not found — install it with: cargo install geode-cli --version 1.2.1",
vault: "",
entries: [],
raw: out
};
}
if (code !== 0) {
var why = "geode list exited with status " + code;
if (errText.trim() !== "") {
why += ": " + firstLine(errText);
}
return {
ok: false,
reason: why,
vault: "",
entries: [],
raw: out
};
}
var docs = parseDocs(out);
if (docs === null) {
return {
ok: false,
reason: "geode list printed unparsable JSON",
vault: "",
entries: [],
raw: out
};
}
var entries = [];
var summary = null;
for (var i = 0; i < docs.length; i++) {
var doc = docs[i];
if (typeof doc.path === "string") {
entries.push(doc.path);
continue;
}
if (Array.isArray(doc.entries)) {
for (var j = 0; j < doc.entries.length; j++) {
var e = doc.entries[j];
if (typeof e === "string") {
entries.push(e);
} else if (e !== null && typeof e === "object" && typeof e.path === "string") {
entries.push(e.path);
}
}
}
summary = doc;
}
if (summary === null || summary.ok !== true) {
return {
ok: false,
reason: "geode list printed no ok summary",
vault: "",
entries: [],
raw: out
};
}
return {
ok: true,
entries: entries,
raw: out
};
}
// Convenience for the bar: full status from a run result.
function statusFor(vaultPath, code, stdout, stderr) {
if (!hasVault(vaultPath)) {
return statusForEmptyVault();
}
var st = statusFromRun(code, stdout, stderr);
if (st.ok) {
st.vault = vaultPath;
}
return st;
}
// ---- helpers ------------------------------------------------------------
// JSON objects in `out`: the whole text as one document if it parses,
// otherwise one per non-empty line. null when nothing parses, or when any
// line is not a JSON object.
function parseDocs(out) {
var whole = tryParse(out);
if (whole !== null) {
return [whole];
}
var lines = String(out).split(/\r?\n/);
var docs = [];
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim() === "") {
continue;
}
var doc = tryParse(lines[i]);
if (doc === null) {
return null;
}
docs.push(doc);
}
return docs.length === 0 ? null : docs;
}
// One JSON object, or null. Arrays and scalars are not status documents.
function tryParse(text) {
var doc = null;
try {
doc = JSON.parse(text);
} catch (e) {
return null;
}
if (doc === null || typeof doc !== "object" || Array.isArray(doc)) {
return null;
}
return doc;
}
function firstLine(s) {
var str = String(s);
var i = str.indexOf("\n");
return i === -1 ? str : str.substring(0, i);
}