-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
157 lines (130 loc) · 5.05 KB
/
cli.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
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
import os
import requests
import yaml
import time
class Cli(object):
commands: list = [
'push',
'pull',
]
pizzaHeaders: dict = {}
def __init__(self):
pizzaToken = os.environ.get('PIZZA_TOKEN')
if pizzaToken is None:
raise Exception('ENV var PIZZA_TOKEN is not set')
self.pizzaHeaders = {
'Authorization': 'Bearer {}'.format(pizzaToken)
}
def getCommands(self) -> list:
return self.commands
def dispatch(self, arguments: dict = {}) -> bool:
self.force = arguments.force
self.dryrun = arguments.dryrun
match arguments.command:
case 'push':
self.push()
case 'pull':
self.pull()
case _:
raise Exception('Command "{}" not implemented'.format(arguments.command))
return False
def pull(self):
dataApi = self.getApiData()
dataLocal = self.getLocalData()
if dataApi == dataLocal:
print('No changes to pull')
return
else:
self.writeToDisk(dataApi)
def push(self):
dataApi = self.getApiData()
dataLocal = self.getLocalData()
if dataApi == dataLocal:
print('No changes to push')
return
else:
print('changes to push')
updateID = False
# compare local to remote for updates and creates
for redirect in dataLocal:
if redirect.get('id') is None:
self.createRedirect(redirect)
updateID = True
else:
redirectApi = self.findRedirect(dataApi, redirect.get('id'))
if redirectApi != redirect:
self.updateRedirect(redirect)
# compare remote to local for deletes
for redirect in dataApi:
redirectLocal = self.findRedirect(dataLocal, redirect.get('id'))
if redirectLocal == {}:
self.deleteRedirect(redirect)
if updateID:
# requires a second request to get the ID
time.sleep(1)
dataApi = self.getApiData()
self.writeToDisk(dataApi)
def findRedirect(self, data: list, id: str) -> dict:
for redirect in data:
if redirect.get('id') == id:
return redirect
return {}
def ask(self, question: str = '') -> bool:
if self.force:
print(question+' [Y/n] y')
return True
answer = input(question+' [Y/n] ')
if answer == 'y' or answer == 'Y' or answer == '':
return True
else:
return False
def updateRedirect(self, changedItem: dict):
if self.ask(f"update redirect {changedItem.get('destination')} {changedItem.get('id')}?"):
if not self.dryrun:
requests.put('https://redirect.pizza/api/v1/redirects/{}'.format(changedItem.get('id')), headers=self.pizzaHeaders, json=changedItem)
print("done")
else:
print("dryrun")
def deleteRedirect(self, changedItem: dict):
if self.ask(f"delete redirect {changedItem.get('destination')} {changedItem.get('id')}?"):
if not self.dryrun:
requests.delete('https://redirect.pizza/api/v1/redirects/{}'.format(changedItem.get('id')), headers=self.pizzaHeaders)
print("done")
else:
print("dryrun")
def createRedirect(self, changedItem: dict):
if self.ask(f"create redirect {changedItem.get('destination')}?"):
if not self.dryrun:
requests.post('https://redirect.pizza/api/v1/redirects', headers=self.pizzaHeaders, json=changedItem)
print("done")
else:
print("dryrun")
def getApiData(self):
redirects = requests.get('https://redirect.pizza/api/v1/redirects?per_page=500', headers=self.pizzaHeaders)
data = redirects.json().get('data')
# remove dynamic data from the response
for destination in data:
del destination['updated_at']
del destination['created_at']
del destination['domains']
sources = []
for source in destination['sources']:
sources.append(source['url'])
destination['sources'] = sources
return data
def getLocalData(self):
f = open("redirects.yaml", "r")
data = yaml.load(f, Loader=yaml.FullLoader)
return data
def writeToDisk(self, data: dict):
if self.ask("Update local file?"):
if not self.dryrun:
dataYaml = yaml.dump(data)
f = open("redirects.yaml", "w")
f.write(dataYaml)
f.close()
print('changes written to disk')
else:
print('changes written to disk: dryrun')
else :
print('no changes written to disk')