Skip to content

Commit 0e1e566

Browse files
committed
more refactoring
1 parent 9654516 commit 0e1e566

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

client/fossildb-client

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,8 @@ import fossildbapi_pb2_grpc as proto_rpc
1010
from grpc_health.v1 import health_pb2
1111
from grpc_health.v1 import health_pb2_grpc
1212

13-
def main():
14-
full_address = '{}:{}'.format(args.address, args.port)
15-
16-
print('Connecting to FossilDB at', full_address)
17-
channel = grpc.insecure_channel(full_address)
18-
19-
def health():
20-
try :
21-
healthStub = health_pb2_grpc.HealthStub(channel)
22-
reply = healthStub.Check(health_pb2.HealthCheckRequest(service=''))
23-
SERVING = health_pb2._HEALTHCHECKRESPONSE_SERVINGSTATUS.values_by_name["SERVING"].number
24-
if reply.status != SERVING:
25-
raise Exception(reply.status)
26-
except Exception as e:
27-
print('Health check unsuccessful. FossilDB offline?')
28-
print(reply)
29-
print(e)
30-
sys.exit(1)
31-
32-
commands = {
33-
'backup': lambda:
34-
proto_rpc.FossilDBStub(channel).Backup(proto.BackupRequest()),
35-
'restore': lambda:
36-
proto_rpc.FossilDBStub(channel).RestoreFromBackup(proto.RestoreFromBackupRequest()),
37-
'health': health
38-
}
3913

14+
def parse_args(commands):
4015
parser = argparse.ArgumentParser()
4116
parser.add_argument(
4217
'address', metavar='address', default='localhost', nargs='?',
@@ -49,13 +24,45 @@ def main():
4924
help='command to execute, one of {}'.format(list(commands.keys())))
5025

5126
args = parser.parse_args()
52-
5327
if args.command not in commands:
5428
print("command {} is not available".format(args.command))
5529
parser.print_help()
5630
exit(20)
5731

58-
reply = commands[args.command]()
32+
return args
33+
34+
35+
def health(channel):
36+
try :
37+
healthStub = health_pb2_grpc.HealthStub(channel)
38+
reply = healthStub.Check(health_pb2.HealthCheckRequest(service=''))
39+
STATUSMAP = health_pb2._HEALTHCHECKRESPONSE_SERVINGSTATUS.values_by_name
40+
SERVING = STATUSMAP["SERVING"].number
41+
if reply.status != SERVING:
42+
raise Exception(reply.status)
43+
except Exception as e:
44+
print('Health check unsuccessful. FossilDB offline?')
45+
print(e)
46+
sys.exit(1)
47+
return reply
48+
49+
50+
def main():
51+
commands = {
52+
'backup': lambda channel:
53+
proto_rpc.FossilDBStub(channel).Backup(proto.BackupRequest()),
54+
'restore': lambda channel:
55+
proto_rpc.FossilDBStub(channel).RestoreFromBackup(proto.RestoreFromBackupRequest()),
56+
'health': health
57+
}
58+
59+
args = parse_args(commands)
60+
full_address = '{}:{}'.format(args.address, args.port)
61+
62+
print('Connecting to FossilDB at', full_address)
63+
channel = grpc.insecure_channel(full_address)
64+
65+
reply = commands[args.command](channel)
5966
print(reply)
6067
if hasattr(reply, 'success') and not reply.success:
6168
sys.exit(1)

0 commit comments

Comments
 (0)