-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.lua
More file actions
197 lines (166 loc) · 5.65 KB
/
cli.lua
File metadata and controls
197 lines (166 loc) · 5.65 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require('./luaex.lua')
local childprocess = require('childprocess')
local fs = require('fs')
local path = require('path')
local package = require('./package.lua')
local date_format = '%Y-%m-%d-%H-%M-%S'
local mode = process.argv[2]
local exarg = process.argv[3]
if process.argv[1] and (not string.match(process.argv[1], 'main.lua')) then
mode = process.argv[1]
exarg = process.argv[2]
end
function fs.readdir_recursive(dir)
local results = {}
local dir_list = path.join(table.unpack(dir))
local files = fs.load_dir(dir_list)
table.foreach(files, function(_, name)
local small_dir = path.join(dir_list, name)
local dir_locate_status, err = fs.lstatSync(small_dir)
if err then
error(err)
end
if dir_locate_status.type ~= 'directory' then
table.insert(results, small_dir)
else
local another_time = fs.readdir_recursive({ dir_list, name })
table.foreach(another_time, function(_, value)
table.insert(results, value)
end)
end
end)
return results
end
function fs.load_dir(dir)
local files, err = fs.readdirSync(dir)
if err then
error(err)
end
return files
end
print("")
print("Rainy's android development tool: geta")
print("---------------------------------------")
print("Version | " .. package.version)
print("Description | " .. package.description)
print("License | " .. package.license)
print("---------------------------------------")
print("")
local accept = { 'dmesg', 'logcat', 'ramoops', 'clear', 'clearall' }
local clear_arg_accept = { 'dmesg', 'logcat', 'ramoops' }
local function print_manual()
print '---------------------------------------------------------------------------------------------'
print 'dmesg | get dmesg log (live)'
print 'logcat | get logcat log (live)'
print 'ramoops | get ramoops log (last dmesg log before crash)'
print 'clear [dmesg / ramoops / logcat] | clear specific log folder (folder name like mode)'
print 'clearall | clear every folder that exist'
print 'help | print all command that avaliable (you can include nothing)'
print '---------------------------------------------------------------------------------------------'
print ''
end
if (not mode) or (not table.includes(accept, mode)) then
print 'You should provide one one of these mode'
print_manual()
os.exit()
end
local function log(msg)
local date = os.date(date_format)
print(string.format('%s | %s', date, msg))
end
local function run_clean(need_arg)
if not need_arg or not table.includes(clear_arg_accept, need_arg) then
print 'clear command arg missing or mismatch, only accept: dmesg / ramoops / logcat'
print_manual()
os.exit()
end
local clean_folder_name = string.format('%s_log', need_arg)
if not fs.existsSync('./' .. clean_folder_name) then return end
log('Clearing folder name: ' .. clean_folder_name)
local all_file = fs.readdir_recursive({ clean_folder_name })
for _, file_dir in pairs(all_file) do
fs.unlinkSync(file_dir)
end
fs.rmdirSync('./' .. clean_folder_name)
log('(FINISHED) Clear folder: ' .. clean_folder_name)
end
local function run_clean_all()
log('Clearing all folders...')
run_clean('dmesg')
run_clean('logcat')
run_clean('ramoops')
log('(FINISHED) Clear all folder!')
end
if mode == 'clear' then
return run_clean(exarg)
end
if mode == 'clearall' then
return run_clean_all()
end
log('Checking folders...')
local date = os.date(date_format)
local folder_name = string.format('%s_log', mode)
local file_name = string.format('%s-%s.log', mode, date)
local full_path = './' .. folder_name .. '/' .. file_name
if not fs.existsSync(folder_name) then
log('Folder ' .. folder_name .. ' not exists, creating...')
fs.mkdirSync(folder_name)
log('Created folder: ' .. folder_name)
end
log('Checking adb ...')
childprocess.exec('adb --version', function (data)
if data then
log("Your pc doesn't have adb binary, please install it from here: https://developer.android.com/tools/releases/platform-tools")
os.exit()
end
end)
log('adb command exists! Now checking if have any device avaliable')
childprocess.exec('adb devices', function (_, res)
local filtered_break = string.split(res, 'device')
if #filtered_break == 1 then
log("No device avaliable, Exit binary")
os.exit()
end
end)
log(string.format('Device avaliable, now running log recorder... [%s]', mode))
log(string.format('File avaliable in: %s', full_path))
local function run_dmesg_recorder(file)
local childProcess = require('childprocess')
local child = childProcess.spawn('adb', {'shell', 'su', '-c', 'dmesg', '-w'}, {})
child.stdout:on('data', function (data)
fs.writeSync(file, -1, data)
end)
child.stdout:on('close', function ()
log('(FINISHED)')
end)
end
local function run_logcat_recorder(file)
local childProcess = require('childprocess')
local child = childProcess.spawn('adb', {'shell', 'su', '-c', 'logcat'}, {})
child.stdout:on('data', function (data)
fs.writeSync(file, -1, data)
end)
child.stdout:on('close', function ()
log('(FINISHED)')
end)
end
local function run_ramoops_recorder(file)
local childProcess = require('childprocess')
local child = childProcess.spawn('adb', {'shell', 'su', '-c', "'cat /sys/fs/pstore/console-ramoops-0'"}, {})
child.stdout:on('data', function (data)
fs.writeSync(file, -1, data)
end)
child.stdout:on('close', function ()
log('(FINISHED)')
end)
end
local file = fs.openSync(full_path, 'a')
if mode == 'dmesg' then
return run_dmesg_recorder(file)
end
if mode == 'logcat' then
return run_logcat_recorder(file)
end
if mode == 'ramoops' then
return run_ramoops_recorder(file)
end