Skip to content

Commit 75659da

Browse files
authored
Update main.cr
1 parent e3528fe commit 75659da

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

cli/main.cr

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
require "json"
22
require "file_utils"
33
require "process"
4-
54
# Define the temporary directory for JSON communication
65
TMP_DIR = "/tmp/Security-Mode"
7-
86
# Ensure the tmp directory exists
97
def ensure_tmp_dir
108
FileUtils.mkdir_p(TMP_DIR) unless Dir.exists?(TMP_DIR)
119
end
12-
1310
# Write a JSON file to the tmp directory
1411
def write_json_file(filename : String, data : Hash(String, JSON::Any))
1512
ensure_tmp_dir
@@ -19,34 +16,31 @@ def write_json_file(filename : String, data : Hash(String, JSON::Any))
1916
rescue ex
2017
puts "Error writing JSON: #{ex.message}"
2118
end
22-
2319
# Read a JSON file from the tmp directory
2420
def read_json_file(filename : String) : Hash(String, JSON::Any)?
2521
ensure_tmp_dir
2622
path = File.join(TMP_DIR, filename)
2723
if File.exists?(path)
2824
json_str = File.read(path)
29-
JSON.parse(json_str).as_h.transform_values { |v| JSON::Any.new(v) }
25+
JSON.parse(json_str).as_h
3026
else
3127
nil
3228
end
3329
rescue ex
3430
puts "Error reading JSON: #{ex.message}"
3531
nil
3632
end
37-
3833
# Launch the UI binary
3934
def launch_ui
4035
ui_path = File.expand_path("~/.hackeros/Security-Mode/bin/ui")
41-
if File.exists?(ui_path) && File.executable?(ui_path)
36+
if File.exists?(ui_path) && LibC.access(ui_path.to_unsafe, LibC::X_OK) == 0
4237
# Launch using cage or gamescope, as per description. Assuming cage for simplicity.
4338
# You may need to adjust based on installed tools.
4439
Process.run("cage", args: [ui_path], output: STDOUT, error: STDERR)
4540
else
4641
puts "UI binary not found or not executable: #{ui_path}"
4742
end
4843
end
49-
5044
# Handle 'start' command: start a profile
5145
def handle_start(profile : String)
5246
valid_profiles = ["agresywny", "bezpieczny", "monitor-only"]
@@ -61,7 +55,6 @@ def handle_start(profile : String)
6155
puts "Invalid profile: #{profile}. Valid: #{valid_profiles.join(", ")}"
6256
end
6357
end
64-
6558
# Handle 'stop' command
6659
def handle_stop
6760
data = {
@@ -70,7 +63,6 @@ def handle_stop
7063
}
7164
write_json_file("stop.json", data)
7265
end
73-
7466
# Handle 'status' command: read status from JSON
7567
def handle_status
7668
status_data = read_json_file("status.json")
@@ -83,7 +75,6 @@ def handle_status
8375
puts "No status available."
8476
end
8577
end
86-
8778
# Handle 'logs' command: request logs
8879
def handle_logs
8980
data = {
@@ -93,22 +84,19 @@ def handle_logs
9384
write_json_file("logs_request.json", data)
9485
puts "Logs requested. Check logs.json later."
9586
end
96-
9787
# Main CLI parser
9888
def main
9989
if ARGV.empty?
10090
puts "Usage: security <command> [args]"
10191
puts "Commands:"
102-
puts " ui - Launch the UI"
103-
puts " start <profile> - Start Security Mode with profile (agresywny, bezpieczny, monitor-only)"
104-
puts " stop - Stop Security Mode"
105-
puts " status - Get current status"
106-
puts " logs - Request logs"
92+
puts " ui - Launch the UI"
93+
puts " start <profile> - Start Security Mode with profile (agresywny, bezpieczny, monitor-only)"
94+
puts " stop - Stop Security Mode"
95+
puts " status - Get current status"
96+
puts " logs - Request logs"
10797
exit(1)
10898
end
109-
11099
command = ARGV[0].downcase
111-
112100
case command
113101
when "ui"
114102
launch_ui
@@ -128,5 +116,4 @@ def main
128116
puts "Unknown command: #{command}"
129117
end
130118
end
131-
132119
main if __FILE__ == Process.executable_path

0 commit comments

Comments
 (0)