-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cr
114 lines (92 loc) · 2.84 KB
/
main.cr
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
require "option_parser"
require "http/client"
require "json"
SERVER_URL="https://yelst-backend.herokuapp.com"
CMD = "sh"
OptionParser.parse do |parser|
parser.on "-v", "--version", "Show version" do
puts "version 1.0"
exit
end
parser.on "-h", "--help", "Show help" do
puts parser
exit
end
parser.on "sign_up", "Sign up" do
puts "Write email"
print "> "
email = gets
puts "Write password"
print "> "
password = gets
response = HTTP::Client.post "#{SERVER_URL}/users/sign_up", headers: nil, form: {email: email, password: password}.to_json
write_to_token_file(JSON.parse(response.body)["result"].to_s)
exit
end
parser.on "sign_in", "Sign in" do
puts "Write email"
print "> "
email = gets
puts "Write password"
print "> "
password = gets
response = HTTP::Client.post "#{SERVER_URL}/users/sign_in", headers: nil, form: {email: email, password: password}.to_json
write_to_token_file(JSON.parse(response.body)["result"].to_s)
exit
end
parser.on "scan", "Scan packages" do
args = [] of String
args << "-c" << "pacman -Qq"
io = IO::Memory.new
Process.run(CMD, args, shell: true, output: io)
list = io.to_s.split("\n")
args = [] of String
args << "-c" << "cat ~/.yelts_token"
io = IO::Memory.new
Process.run(CMD, args, shell: true, output: io)
token = io.to_s.sub("\n", "")
headers = HTTP::Headers.new.add("Authorization", value: "Bearer #{token}")
response = HTTP::Client.post "#{SERVER_URL}/packages/set_list", headers: headers, form: {list: list, hostname: hostname}.to_json
puts response.status
exit
end
parser.on "list", "List of saved packages" do
puts packages
exit
end
parser.on "restore", "Restore packages from list" do
io = IO::Memory.new
args = [] of String
pacman_string = "yay -S "
list_of_packages = packages.join(" ")
pacman_string += list_of_packages
pacman_string += " --noconfirm --needed"
args << "-c" << pacman_string
Process.run(CMD, args, shell: true, output: io)
puts io.to_s
exit
end
end
def packages
args = [] of String
args << "-c" << "cat ~/.yelts_token"
io = IO::Memory.new
Process.run(CMD, args, shell: true, output: io)
token = io.to_s.sub("\n", "")
headers = HTTP::Headers.new.add("Authorization", value: "Bearer #{token}")
response = HTTP::Client.get "#{SERVER_URL}/packages/get_list", headers: headers, form: {hostname: hostname}.to_json
JSON.parse(response.body)["result"].to_s.split(" ")
end
def write_to_token_file(text)
args = [] of String
token = "echo " + text + " > ~/.yelts_token"
args << "-c" << token
Process.run(CMD, args, shell: true)
end
def hostname
io = IO::Memory.new
args = [] of String
args << "-c" << "hostname"
Process.run(CMD, args, shell: true, output: io)
io.to_s.sub("\n", "")
end