-
Notifications
You must be signed in to change notification settings - Fork 1
/
skvtool.lua
executable file
·121 lines (107 loc) · 2.28 KB
/
skvtool.lua
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
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: skvtool.lua $
--
-- Author: Markus Stenberg <[email protected]>
--
-- Copyright (c) 2012 cisco Systems, Inc.
--
-- Created: Thu Oct 4 13:18:34 2012 mstenber
-- Last modified: Mon Oct 28 14:05:13 2013 mstenber
-- Edit time: 74 min
--
-- minimalist tool for playing with SKV
-- features:
-- -l dump contents
-- -s set key=value
-- -g get key
-- -w wait for server to go up (forever)
require 'mst'
require 'mst_cliargs'
require 'skv'
require 'skvtool_core'
require 'socket'
local json = require "dkjson"
local args = mst_cliargs.parse{
options={
{name='d',
flag=1,
desc='enable debugging (spammy)'},
{name='l',
flag=1,
alias='list-lua',
desc='list all key-value pairs [Lua]'},
{name='L',
flag=1,
alias='list-json',
desc='list all key-value pairs [json]'},
{name='version',
flag=1, desc="prints the program's version and exits"},
{name='w',
flag=1,
desc='wait for other end to go up'},
{name='r',
flag=1,
desc='read key=value pairs from stdin, in -l format'},
{name='debug',
value='file',
desc='enable debugging (to stdout if value 1, otherwise to given file)'
},
{value='keys',
desc='list of keys, or key=value pairs (=set)',
max=999},
}
}
-- handle debugging
if args.debug then mst.set_enable_debug(args.debug) end
if args.d
then
mst.enable_debug = true
mst.d('enabling debug')
end
if args.v
then
return print('skvtool.lua 0.1')
end
local setted = false
-- ok, we're on a mission. get skv to ~stable state
local s
while true
do
s = skv.skv:new{long_lived=false}
local r, err = s:connect()
if r
then
break
end
if not args.w
then
return print('connection failure', err)
end
s:done()
socket.sleep(1)
end
local stc = skvtool_core.stc:new{skv=s}
if args.l
then
stc:list_all(function (x)
return mst.repr(x)
end)
return
end
if args.L
then
stc:list_all()
return
end
if args.r
then
local str = io.read()
for i, line in ipairs(mst.string_split(str, '\n'))
do
stc:process_key(s, line)
end
stc:wait_in_sync()
end
stc:process_keys(args.keys or {})