-
Notifications
You must be signed in to change notification settings - Fork 3
/
clearskies
executable file
·260 lines (204 loc) · 5.95 KB
/
clearskies
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env ruby
#
# ClearSkies, a two-way synchronization program
#
#
# This is the command-line user-interface to the clearskies daemon. It
# launches the daemon and can issue commands to it.
require_relative 'lib/control_client'
require_relative 'lib/conf'
# Parse command-line arguments. We have subcommands, which themselves have
# their own set of options, as well as a few global options which we'll always
# interject.
command = ARGV.find { |arg| arg !~ /^-/ }
require 'optparse'
options = {}
OptionParser.new do |opts|
if !command
opts.banner = "Usage: clearskies [command] [args...]"
else
opts.banner = "Usage: clearskies #{command}"
end
extra_help = nil
case command
when nil
when "start"
extra_help = "
Start daemon in the background. The daemon needs to be running in order for
files to sync to other devices.
"
opts.on( '-F', '--no-fork', 'Do not start in background' ) do |f|
options[:no_fork] = !f
end
opts.on( '--trackers TRACKER', 'HTTP addresses of trackers' ) do |t|
Conf.trackers = t.split ','
end
# FIXME Instead of having to pass this at start time, let the user
# reconfigure it dynamically via the CLI
opts.on( '-p', '--listen-port NUMBER', 'Port for incoming connections' ) do |p|
Conf.listen_port = p.to_i
end
# FIXME Instead of having to pass this at start time, let the user
# reconfigure it dynamically via the CLI
opts.on( '-d', '--udp-port NUMBER', 'Port for shared STUN/uTP UDP socket' ) do |p|
Conf.udp_port = p.to_i
end
when "stop"
extra_help = "Cause background program to exit."
when "restart"
extra_help = "Restart background program."
when "status"
extra_help = "
Show detailed program status.
FIXME explain what status details mean here.
"
when "logcat"
extra_help = "Display the contents of the log and exit."
when "logtail"
extra_help = "Show new log messages until canceled."
when "create"
opts.banner += " [path]"
extra_help = "Create share from existing directory."
when "list"
extra_help = "List all shares and sync status."
when "share"
opts.banner += " [path]"
opts.on( '-m', '--mode', 'Share mode' )
extra_help = "
Share existing directory with peer. If the directory is not already a share,
it will be made one automatically.
--mode should be one of 'read-write', 'read-only', and 'untrusted'. ('rw, 'ro',
'un' are also acceptable.)
"
when "attach"
opts.banner += " [code] [path]"
extra_help = "
Add access code from someone else, creating new share at [path]. The code is a
string starting with SYNC. If files already exist at path, they will be
overwritten.
"
when "detach"
opts.banner += " [path]"
extra_help = "
Stop syncing path. This does not delete any of the directory contents.
"
end
opts.on( "-h", "--help", "View this screen" ) do |h|
puts opts
puts extra_help if extra_help
if !command
puts "
Daemon management:
start Start background program
stop Cause background program to exit
restart Restart background program
status Give program status
logcat Display the contents of the log and exit
logtail Show new log messages until canceled
Share management:
create [path] Create new share
list List all shares and sync status
share [path] Make access code to be given to others
attach [code] [path] Add access code from someone else,
creating new share at [path]
detach [path] Stop syncing path
"
end
exit
end
end.parse!
command = ARGV.shift
if !command
abort "No command given, try --help"
end
case command
when "start"
require_relative 'lib/daemon'
if options[:no_fork]
Daemon.run
else
Daemon.daemonize
end
when "stop"
ControlClient.issue :stop
when "restart"
require_relative 'lib/daemon'
ControlClient.issue :stop
Daemon.daemonize
when "logcat"
logfile = Conf.path 'log'
exec "cat #{logfile}"
when "logtail"
logfile = Conf.path 'log'
exec "tail -f #{logfile}"
when "pause"
ControlClient.issue "pause"
when "resume", "unpause"
ControlClient.issue "resume"
when "status", "st", "stat"
res = ControlClient.issue :status
p res
when "create"
path = ARGV.shift
unless File.exists?(path)
abort "No such path: #{path.inspect}"
end
path = File.realpath path
ControlClient.issue :create_share, {
path: path
}
when "ls", "list"
res = ControlClient.issue :list_shares
puts "Path Status"
res[:shares].each do |share|
puts "#{share[:path]} #{share[:status]}"
end
when "share"
path = ARGV.shift
abort "Path expected, see --help" if !path
mode_input = options[:mode] || "rw"
modes = {
"rw" => "read_write",
"read-write" => "read_write",
"read_write" => "read_write",
"ro" => "read_only",
"read-only" => "read_only",
"read_only" => "read_only",
"un" => "untrusted",
"untrusted" => "untrusted",
}
mode = modes[mode_input.downcase]
if mode.nil?
abort "No such mode: #{mode_input.inspect}"
end
unless File.exists?(path)
abort "No such path: #{path.inspect}"
end
path = File.realpath(path)
res = ControlClient.issue :create_access_code, {
path: path,
mode: mode,
}
puts "clearskies:#{res[:access_code]}"
when "attach", "add"
code = ARGV.shift
path = ARGV.shift
p path
FileUtils.mkdir_p path
path = File.realpath(path)
ControlClient.issue :add_share, {
code: code,
path: path
}
when "detach", "rm"
path = ARGV.shift
unless File.exists?(path)
abort "No such path: #{path.inspect}"
end
path = File.realpath(path)
ControlClient.issue :remove_share, {
path: path
}
else
abort "Invalid command: #{command.inspect}, try --help"
end