forked from cockpit-project/cockpit-project.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcockpit-util.html
More file actions
220 lines (216 loc) · 10 KB
/
cockpit-util.html
File metadata and controls
220 lines (216 loc) · 10 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
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
---
layout: guide
---
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>cockpit.js: Utilities</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Cockpit Guide">
<link rel="up" href="api-base1.html" title="API: base1">
<link rel="prev" href="cockpit-login.html" title="cockpit.js: User Session">
<link rel="next" href="cockpit-cache.html" title="cockpit.js: Object Cache">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="cockpit-login.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="api-base1.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">Cockpit Guide</th>
<td><a accesskey="n" href="cockpit-cache.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="cockpit-util"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2>cockpit.js: Utilities</h2>
<p>cockpit.js: Utilities — Various utility functions</p>
</td>
<td valign="top" align="right"></td>
</tr></table></div>
<div class="refsection">
<a name="cockpit-format"></a><h2>cockpit.format()</h2>
<pre class="programlisting">
string = cockpit.format(template, args)
string = cockpit.format(template, [arg, ...])
</pre>
<p>Format a string interpolating <code class="code">args</code> into <code class="code">template</code> using
shell like syntax. The <code class="code">args</code> may be either an array or javascript object.
The <code class="code">template</code> can contain fields that look like <code class="code">$name</code> or
<code class="code">${name}</code> or <code class="code">$0</code>. Numeric fields are used with array
<code class="code">args</code> and start at zero.</p>
<p>In the second form, multiple <code class="code">arg</code> arguments may be passed directly,
and interpolated as as numeric fields in the <code class="code">template</code>.</p>
<p>All falsy arguments except the numbers <code class="code">0</code> and <code class="code">0.0</code>are
replaced by an empty string.</p>
</div>
<div class="refsection">
<a name="cockpit-format-number"></a><h2>cockpit.format_number()</h2>
<pre class="programlisting">
string = cockpit.format_number(number, [precision])
</pre>
<p>Formats <code class="code">number</code> into a displayable <code class="code">string</code>. If the number is not
an integer, it is rounded to the given number of decimal places, defaulting to 3. If the number
is near zero, but not quite zero it is rounded to the smallest non-zero value of the given precision;
i.e. ±0.001 for default precision 3.</p>
<p>If <code class="code">number</code> is <code class="code">null</code> or <code class="code">undefined</code> an empty string
will be returned.</p>
</div>
<div class="refsection">
<a name="cockpit-format-bytes"></a><h2>cockpit.format_bytes()</h2>
<pre class="programlisting">
string = cockpit.format_bytes(number, [options])
</pre>
<p>Formats <code class="code">number</code> into a displayable <code class="code">string</code> with a suffix, such as
<span class="emphasis"><em>kB</em></span> or <span class="emphasis"><em>MB</em></span>.</p>
<p>By default, SI units are used. IEC units (1024-based) can be requested by including
<code class="code">base2: true</code> in <code class="code">options</code>.</p>
<p>By default, non-integer numbers will be formatted with 3 digits of precision. This can be changed
with <code class="code">options.precision</code>.</p>
<p>If <code class="code">number</code> is <code class="code">null</code> or <code class="code">undefined</code> an empty string will be
returned.</p>
</div>
<div class="refsection">
<a name="cockpit-format-bytes-per-sec"></a><h2>cockpit.format_bytes_per_sec()</h2>
<pre class="programlisting">
string = cockpit.format_bytes_per_sec(number, [options])
</pre>
<p>Format <code class="code">number</code> of bytes into a displayable speed <code class="code">string</code>.</p>
<p>This function is mostly equivalent to <code class="code">cockpit.format_bytes()</code> but the returned
value contains a unit like <span class="emphasis"><em>kB/s</em></span> or <span class="emphasis"><em>MB/s</em></span>.</p>
</div>
<div class="refsection">
<a name="cockpit-format-bits-per-sec"></a><h2>cockpit.format_bits_per_sec()</h2>
<pre class="programlisting">
string = cockpit.format_bits_per_sec(number, [options])
</pre>
<p>Format <code class="code">number</code> of bits into a displayable speed <code class="code">string</code>.</p>
<p>This function is mostly equivalent to <code class="code">cockpit.format_bytes()</code> but the returned
value contains a unit like <span class="emphasis"><em>kbps</em></span> or <span class="emphasis"><em>Mbps</em></span>.</p>
<p>This function does not support IEC units. <code class="code">base2</code> may not be passed as part of
<code class="code">options</code>.</p>
</div>
<div class="refsection">
<a name="cockpit-init"></a><h2>cockpit.init()</h2>
<pre class="programlisting">
await cockpit.init();
cockpit.init().then(() => { ... });
</pre>
<p>Requests initialization of the Cockpit client library. This will ensure that the transport is
connected and we are ready to create channels. It also populates the <code class="code">cockpit.info</code>
field.</p>
<p>This function returns a promise. Initialization isn't complete until the promise has resolved. You
can either <code class="code">await</code> it or call <code class="code">.then()</code> on it.</p>
</div>
<div class="refsection">
<a name="cockpit-info"></a><h2>cockpit.info</h2>
<pre class="programlisting">
cockpit.info.channels[payload]
cockpit.info.os_release[field]
cockpit.info.user
cockpit.info.ws.version
</pre>
<p>This object contains information about Cockpit itself. It is only available after cockpit.init() has
been called and awaited.</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><code class="code">channels</code></span></p></td>
<td><p>This is a mapping of channel payload types (keys, strings) supported by the bridge to
capabilities advertised by those channels (values, lists of strings). Channels are listed even if they
don't advertise any capabilities, making this useful as a way to determine which channel types are
supported by the bridge.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">os_release</code></span></p></td>
<td><p>This is the data from the <code class="filename">/etc/os-release</code> or
<code class="filename">/usr/lib/os-release</code> on the system that the bridge is running on. It is a mapping
from the key names to their values. See the <a class="ulink" href="https://www.freedesktop.org/software/systemd/man/latest/os-release.html" target="_top">os-release
Specification</a> for information about the available keys.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">user</code></span></p></td>
<td>
<p>Contains information about the user we're logged in as.</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><code class="code">uid</code></span></p></td>
<td><p>This is unix user id as an integer.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">gid</code></span></p></td>
<td><p>This is unix user group id as an integer.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">name</code></span></p></td>
<td><p>This is the unix user name like <code class="code">"root"</code>.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">fullname</code></span></p></td>
<td><p>This is a readable name for the user, from the GECOS field.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">group</code></span></p></td>
<td><p>This is the primary group name of the user.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">groups</code></span></p></td>
<td><p>This is an array of group names to which the user belongs. The first item in this
list is the primary group.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">home</code></span></p></td>
<td><p>This is user's home directory.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">shell</code></span></p></td>
<td><p>This is unix user shell.</p></td>
</tr>
</tbody>
</table></div>
</td>
</tr>
<tr>
<td><p><span class="term"><code class="code">ws</code></span></p></td>
<td>
<p>Contains information about the webserver Cockpit is being served with.</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><code class="code">version</code></span></p></td>
<td><p>The version of the webserver.</p></td>
</tr></tbody>
</table></div>
</td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsection">
<a name="cockpit-event-target"></a><h2>cockpit.event_target</h2>
<pre class="programlisting">
cockpit.event_target(object, [handlers])
</pre>
<p>Adds an
<a class="ulink" href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget" target="_top">EventTarget</a>
implementation to the <code class="code">object</code>. Optionally store the handlers in <code class="code">handlers</code>
if its specified.</p>
</div>
</div>
<div class="footer"><hr></div>
</body>
</html>