-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_manager.lua
174 lines (138 loc) · 3.73 KB
/
file_manager.lua
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
local strutils = require("cluautils.string_utils")
FM = FM or {}
---@param file? file*
---@param mode? readmode
---@return table
function FM.get_lines_from_file(file, mode)
if file == nil then
return {}
end
local result = {}
for line in file:lines(mode or "*l") do
table.insert(result, line)
end
return result
end
---@param file_path string
---@return string?
function FM.get_file_content(file_path)
if not FM.is_file_exists(file_path) then
return nil
end
local file = io.open(file_path, IOMODE.READ)
if file == nil then
return nil
end
local content = table.concat(FM.get_lines_from_file(file), "\n")
file:close()
return content
end
---@MARK - File system manipulations
FileType = {
DIR = "d", -- directory
FILE = "f", -- regular file
CHAR = "c", -- character special
LINK = "l", -- symbolic link
FIFO = "p", -- FIFO
SOCKET = "s", -- socket
}
IOMODE = {
READ = "r",
WRITE = "w",
OVERRIDE = "w+"
}
---Writes to file with writer function
---@param file_path string
---@param mode? openmode
---@param writer fun():string
function FM.write_to_file(file_path, mode, writer)
local file = io.open(file_path, mode or IOMODE.WRITE)
if file == nil then
return
end
file:write(writer())
io.close(file)
end
---Removes file at a given path
---@param file_path string
function FM.delete_file(file_path)
os.remove(file_path)
end
---Checks if file exists at the path
---@param file_path string
---@param mode? openmode
---@return boolean
function FM.is_file_exists(file_path, mode)
local file = io.open(file_path, mode or IOMODE.READ)
if file == nil then
return false
else
io.close(file)
return true
end
end
---@class find
---@field dir_path string
---@field file_type FileType
---@field name_pattern string
---@field max_depth string|integer
---
---@enum FileType
---local FileType = {
--- DIR = "d", -- directory
--- FILE = "f", -- regular file
--- CHAR = "c", -- character special
--- LINK = "l", -- symbolic link
--- FIFO = "p", -- FIFO
--- SOCKET = "s", -- socket
---}
---
--- Find terminal command implementation
---@param param_table find
---@return table
function FM.get_dir_content(param_table)
setmetatable(param_table, { __index = {
dir_path = ".",
file_type = nil,
name_pattern = "*",
max_depth = nil,
}})
local dir_path = param_table.dir_path
local file_type = param_table.file_type
local name_pattern = param_table.name_pattern
local max_depth = param_table.max_depth
local sep = " "
local find_cmd = 'find "' .. dir_path .. '"'
local file_param = ""
local name_param = ""
local max_depth_param = ""
if not strutils.isNilOrEmpty(file_type) then
file_param = '-type ' .. file_type
end
if not strutils.isNilOrEmpty(name_pattern) then
name_param = '-name "' .. name_pattern .. '"'
end
if type(max_depth) == "string" then
if strutils.isNilOrEmpty(tostring(max_depth)) then
max_depth_param = '-maxdepth ' .. max_depth
end
elseif type(max_depth) == "number" and max_depth >= 0 then
max_depth_param = '-maxdepth ' .. tostring(max_depth)
end
local full_cmd = find_cmd .. sep .. file_param .. sep .. name_param .. sep .. max_depth_param
return FM.get_lines_from_file(io.popen(full_cmd, IOMODE.READ), "*l")
end
---@param file_path string
---@return boolean false if error occured or file already exists else true
function FM.create_file(file_path)
if FM.is_file_exists(file_path) then
return false
end
local file = io.open(file_path, IOMODE.WRITE)
if type(file) == "string" or file == nil then
return false
end
file:close()
return true
end
return FM