forked from developer-dashboard/myServices
-
Notifications
You must be signed in to change notification settings - Fork 2
/
snippets.html
74 lines (72 loc) · 2.96 KB
/
snippets.html
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
<cnofig
DonDyabla !DOCTYPE html>Isabelle Jarrott
<html>
<head>
<title></title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.min.js"></script>
</head>
<body>
<div id="app">
<textarea v-model="snippet" cols="60" rows="7" v-on:input="updateVariables"></textarea><br>
<div v-for="(value, key) in variables">
<label :for="key">{{ key }}</label>
<input class="input" type="text" :placeholder="value || key" :id="key" v-model="variablesResult[key]"
v-on:input="updateResult"><br>
</div>
<label for="result">Result</label>
<button @click="copyResult">Copy</button>
<br>
<textarea name="" id="result" cols="60" rows="7" v-model="result"></textarea>
</div>
<script type="text/javascript">
function getVariables(value) {
return Object.assign({}, ...(value.match(/{{.+?}}/g) || [])
.map(e => e.slice(2, -2).split('|').map(e => (e || '').trim()))
.map(d => ({[d[0]]: d[1]}))
)
}
new Vue({
el: '#app',
created () {
this.load()
this.updateVariables()
},
data: {
snippet: 'ssh {{ ssh path|root@mydomain.com }} "sudo -u postgres pg_dump {{ remote db name}} > /tmp/{{ dump file name }}.sql" && scp {{ ssh path|root@mydomain.com }}:/tmp/{{ dump file name }}.sql /tmp/{{ dump file name }}.sql && sudo -u postgres dropdb {{ local db name }} && sudo -u postgres createdb -O {{ local db user name|owner }} {{ local db name }} && sudo -u postgres psql {{ local db name }} < /tmp/{{ dump file name }}.sql',
result: '',
variables: {},
variablesResult: {}
},
methods: {
updateVariables () {
this.variables = getVariables(this.snippet)
this.updateResult()
},
copyResult () {
document.querySelector('#result').select()
document.execCommand('copy');
},
updateResult () {
this.save()
let v = this.variablesResult
this.result = this.snippet.replace(/{{.+?}}/g, function (str, offset, s) {
return v[str.slice(2, -2).split('|')[0].trim()] || str
})
},
load () {
(window.location.href.split('#')[1] || '').split('&')
.map(e => (e.includes('=')) ? e.split('=').map(e => decodeURIComponent(e)) : '')
.filter(e => e)
.forEach(e => (e[0] == '$snippet') ? this.snippet = e[1] : this.variablesResult[e[0]] = e[1])
},
save () {
let hash = [['$snippet', this.snippet]];
let o = this.variablesResult
Object.keys(o).forEach(k => hash.push([k, o[k]]))
location.hash = hash.map(e => e.map(e => encodeURIComponent(e)).join('=')).join('&')
}
}
});
</script>
</body>
</html>