-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetcd_runner.js
208 lines (170 loc) · 5.99 KB
/
etcd_runner.js
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
const {ipcRenderer} = require('electron');
const util = require('util');
const uuid = require('uuid');
const Datastore = require('nedb');
const db = new Datastore({ filename: __dirname + '/saved_connections.store', autoload: true });
const TABLE_KEY_PREFIX = 'e9e6285c';
const HIDDEN_KEY_ID_SELECTOR = '#selected_key';
let valueRegistry = {};
let tableIdRegistry = {};
let savedConnections = null;
db.find({}, function (err, docs) {
savedConnections = docs;
docs.forEach(function (doc) {
$('#saved_connections_selector').append($('<option>', {
value: doc._id,
text: doc._id
}));
});
});
ipcRenderer.on('createKeyResponse', function(event, response){
if (response.err) {
alert('Request to create Key failed: ' + JSON.stringify(response.err));
} else {
alert('Key was added');
}
});
ipcRenderer.on('updateKeyResponse', function(event, response){
if (response.err) {
alert('Request to updated Key failed: ' + JSON.stringify(response.err));
} else {
alert('Key was updated');
}
});
ipcRenderer.on('getKeyResponse', function(event, response){
if (response.err) {
alert('Request to get Key Failed: ' + JSON.stringify(response.err));
} else {
valueRegistry = {};
tableIdRegistry = {};
$('#key_details_data').html('<pre></pre>');
$('#selected_key').val('');
$('#keys_name_table').html(generateKeysTable(response.result));
}
});
function initalizeConnection () {
const connectionDetails = {};
$('.connection_form_input').each(function () {
connectionDetails[$(this).attr('name')] = $(this).val();
});
$('.connection_form_input_check_box').each(function () {
connectionDetails[$(this).attr('name')] = $(this).is(':checked');
});
connectionDetails._id = connectionDetails.connectionName ? connectionDetails.connectionName.trim() : uuid.v4();
if (connectionDetails.saveDetails) {
db.update({_id : connectionDetails._id}, connectionDetails, { upsert: true });
}
ipcRenderer.once('connectionResponse', function(event, response){
if (response) {
alert('Connection failed: ' + JSON.stringify(response));
} else {
$('#connection_form').hide();
$('.main-app').css('z-index', 1);
$('#footer_title').html('Connected to: ' + (connectionDetails.connectionString || '127.0.0.1:2379'));
}
});
ipcRenderer.send('createConnection', connectionDetails);
}
function getKeyTableId (key) {
return tableIdRegistry[key];
}
function generateKeysTable (kvs) {
let responseHtml = '<tbody>';
for(let i = 0; i < kvs.length; i++) {
valueRegistry[kvs[i].key] = kvs[i].value;
tableIdRegistry[kvs[i].key] = uuid.v4();
responseHtml += util.format('<tr id="%s" onclick="switchSelectedKey(\'%s\')"><td>%s</td></tr>', getKeyTableId(kvs[i].key), kvs[i].key, kvs[i].key);
}
return responseHtml + '</tbody>';
}
function getKeys () {
const params = {
key : $('#search_bar_input').val()
};
ipcRenderer.send('getKey', params);
}
function switchSelectedKey(key) {
const actualId = '#' + getKeyTableId(key);
let value = valueRegistry[key];
if ($(HIDDEN_KEY_ID_SELECTOR).val()) {
const oldSelectedId = '#' + getKeyTableId($(HIDDEN_KEY_ID_SELECTOR).val());
$(oldSelectedId).removeClass('selected_key');
}
$(HIDDEN_KEY_ID_SELECTOR).val(key);
$(actualId).addClass('selected_key');
if (value && value.indexOf('{') === 0) {
try {
const temp = JSON.parse(value);
value = JSON.stringify(temp, undefined , 2);
} catch (err) {}
}
$('#key_details_data').html(value);
$(actualId).select();
}
function showCreateKeyModalForm() {
$('#create_key_form_container').dialog();
}
function createKey() {
const keyDetails = {};
$('.create_key_form_input').each(function () {
keyDetails[$(this).attr('name')] = $(this).val().trim();
$(this).val('');
});
$('#create_key_form_container').dialog('destroy');
if (window.confirm('Do you want to create: ' + JSON.stringify(keyDetails)) == true) {
ipcRenderer.send('createKey', keyDetails);
} else {
return false;
}
}
function showUpdateKeyModalForm() {
$('#update_key_form_container').dialog();
}
function updateKey() {
let updatedValue = $('#update_key_form_key_value').val();
try {
const temp = JSON.parse(updatedValue);
updatedValue = JSON.stringify(temp);
} catch (err) {}
const keyDetails = {
key : $(HIDDEN_KEY_ID_SELECTOR).val(),
value : updatedValue
};
$('#update_key_form_container').dialog('destroy');
if (window.confirm('Do you want to update: ' + JSON.stringify(keyDetails)) == true) {
ipcRenderer.send('updateKey', keyDetails);
}
}
function deleteKey () {
const requestedKey = $(HIDDEN_KEY_ID_SELECTOR).val();
if (window.confirm('Do you want to delete: ' + requestedKey) == true) {
ipcRenderer.once('deleteKeyResponse', function(event, response){
if (response.err) {
alert('Request to delete Key failed: ' + JSON.stringify(response.err));
} else {
alert('Key was deleted');
$(HIDDEN_KEY_ID_SELECTOR).val();
$('#' + getKeyTableId(requestedKey)).remove();
}
});
ipcRenderer.send('deleteKey', {key : requestedKey});
}
}
function loadSavedConnection () {
const selectedConnection = $('#saved_connections_selector').val();
savedConnections.forEach(function (savedDetails) {
if (savedDetails._id === selectedConnection) {
$('.connection_form_input').each(function () {
$(this).val(savedDetails[$(this).attr('name')]);
});
}
});
}
function openFileSelector () {
ipcRenderer.send('openFileSelector');
ipcRenderer.once('openFileSelectorResponse', function(event, response){
if (response) {
$('#ca_file_path').val(response);
}
});
}