forked from girish946/local-paste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpcli.py
111 lines (84 loc) · 3.25 KB
/
lpcli.py
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
import argparse
import requests
parser = argparse.ArgumentParser()
debug = 0
url = "http://{0}:{1}/"
def GetPaste(pasteId):
print(pasteId)
r = requests.get(url+"api/get/"+pasteId)
print(r.text)
def AddNewPaste(pasteName, pasteContent):
pasteDetails = {'name': pasteName, 'content': pasteContent}
r = requests.put(url+"api/new", json=pasteDetails)
if r.status_code == 200:
print(r.content)
else:
print("error")
def UpdateAPaste(pasteId, pasteContent,append):
req = requests.get(url+"api/get/"+pasteId)
details=req.json()
if pasteContent:
if append != True:
contentToUpdate=pasteContent
else:
contentToUpdate=details['Content']+" "+pasteContent
pasteDetails = {'name': details['Name'], 'content': contentToUpdate}
r=requests.put(url+"/api/update/"+pasteId,json=pasteDetails)
def DeleteAPaste(pasteId):
r = requests.delete(url+"api/delete/"+pasteId)
print(r.json()['delete'])
def SearchPaste(keyword):
print("ID Name ")
for i in keyword:
res = requests.get(url+'api/search/'+i)
pasteList = res.json()
for paste in pasteList[i]:
print(paste['Id']+" "+paste['Name'])
def ShowAllPastes():
res = requests.get(url+'api/selectDb')
pasteList = res.json()
print("ID Name ")
for paste in pasteList['pastes']:
print(paste['Id']+" "+paste['Name'])
if __name__ == '__main__':
parser.add_argument("action", nargs='?',
help="\
Action: [get, new, search, createDb, delete, showAll,update]")
parser.add_argument("--pasteId", default="1",
help="sets the pasteId for operation", required=False)
parser.add_argument("--port", help="server port", default="8000")
parser.add_argument("--ip", help="server ip", default="127.0.0.1")
parser.add_argument("--keyword", help="Keyword for searching",
type=str, required=False)
parser.add_argument("--name", help="PasteName", type=str,
required=False)
parser.add_argument("--content", help="PasteContent", type=str,
required=False)
parser.add_argument("--append",required=False)
arg = parser.parse_args()
url = url.format(arg.ip, arg.port)
if arg.action == 'showAll':
ShowAllPastes()
if arg.action == 'search':
if arg.keyword:
SearchPaste(arg.keyword)
else:
print("please enter paste name of keyword to serch the paste")
elif arg.action == 'get':
if arg.pasteId:
GetPaste(arg.pasteId)
elif arg.action == 'delete':
if arg.pasteId:
DeleteAPaste(arg.pasteId)
elif arg.action == 'update':
print("inside update")
if arg.pasteId!="1":
print("there is some pasteid "+arg.pasteId)
UpdateAPaste(arg.pasteId,arg.content,arg.append)
else:
print("Please provide paste id to update")
if arg.action == 'new':
if arg.name and arg.content:
AddNewPaste(arg.name, arg.content)
else:
print("paste should have a name")