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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
# File 'lib/shai/commands/sync.rb', line 11
def self.included(base)
base.class_eval do
desc "init", "Initialize a new configuration"
def init
require_auth!
if File.exist?(SHAIRC_FILE)
ui.error("A #{SHAIRC_FILE} file already exists in this directory.")
ui.info("Use `shai push` to upload changes to the existing configuration.")
exit EXIT_INVALID_INPUT
end
name = ui.ask("Configuration name:")
description = ui.ask("Description (optional):")
visibility = ui.select("Visibility:", %w[private public], default: "private")
include_patterns = ui.ask("Include paths (glob patterns, comma-separated):", default: ".claude/**,.cursor/**")
ui.blank
begin
response = ui.spinner("Creating configuration...") do
api.create_configuration(
name: name,
description: description.to_s.empty? ? nil : description,
visibility: visibility
)
end
config = response["configuration"] || response
slug = config["slug"]
username = credentials.username
shairc_content = <<~YAML
# .shairc - Shai configuration
slug: #{slug}
include:
#{include_patterns.split(",").map { |p| " - #{p.strip}" }.join("\n")}
exclude:
- "**/*.local.*"
- "**/.env"
YAML
File.write(SHAIRC_FILE, shairc_content)
ui.success("Created #{slug}")
ui.indent("Remote: #{Shai.configuration.api_url}/#{username}/#{slug}")
ui.blank
ui.info("Next steps:")
ui.indent("1. Add or modify files matching your include patterns")
ui.indent("2. Run `shai push` to upload your configuration")
rescue InvalidConfigurationError => e
ui.error(e.message)
exit EXIT_INVALID_INPUT
rescue NetworkError => e
ui.error(e.message)
exit EXIT_NETWORK_ERROR
end
end
desc "push", "Push local changes to remote"
option :dry_run, type: :boolean, default: false, desc: "Show what would be pushed"
option :message, type: :string, aliases: "-m", desc: "Add a message (future feature)"
def push
require_auth!
shairc = load_shairc
slug = shairc["slug"]
tree = build_local_tree(shairc)
if tree.empty?
ui.warning("No files found matching include patterns.")
ui.info("Check your .shairc include patterns.")
exit EXIT_INVALID_INPUT
end
display_name = "#{credentials.username}/#{slug}"
if options[:dry_run]
ui.("Would push to #{display_name}:")
tree.each { |node| ui.display_file_operation(:uploaded, node[:path]) }
ui.blank
ui.info("No changes made (dry run)")
return
end
ui.("Pushing to #{display_name}...")
ui.blank
tree.each { |node| ui.display_file_operation(:uploaded, node[:path]) }
begin
ui.spinner("Uploading...") do
api.update_tree(slug, tree)
end
ui.blank
ui.success("Pushed #{tree.length} items")
ui.indent("View at: #{Shai.configuration.api_url}/#{credentials.username}/#{slug}")
rescue NotFoundError
ui.error("Configuration '#{slug}' not found. Run `shai init` first.")
exit EXIT_NOT_FOUND
rescue PermissionDeniedError
ui.error("You don't have permission to modify '#{display_name}'.")
exit EXIT_PERMISSION_DENIED
rescue NetworkError => e
ui.error(e.message)
exit EXIT_NETWORK_ERROR
end
end
desc "status", "Show local changes"
def status
require_auth!
shairc = load_shairc
slug = shairc["slug"]
display_name = "#{credentials.username}/#{slug}"
begin
remote_tree = ui.spinner("Fetching remote state...") do
api.get_tree(slug)["tree"]
end
local_tree = build_local_tree(shairc)
remote_files = remote_tree.select { |n| n["kind"] == "file" }
.each_with_object({}) { |n, h| h[n["path"]] = n["content"] }
local_files = local_tree.select { |n| n[:kind] == "file" }
.each_with_object({}) { |n, h| h[n[:path]] = n[:content] }
modified = []
new_files = []
deleted = []
local_files.each do |path, content|
if remote_files.key?(path)
modified << path if remote_files[path] != content
else
new_files << path
end
end
remote_files.each_key do |path|
deleted << path unless local_files.key?(path)
end
ui.("Configuration: #{display_name}")
if modified.empty? && new_files.empty? && deleted.empty?
ui.info("Status: Up to date")
ui.blank
ui.info("No local changes detected.")
else
ui.info("Status: Local changes")
ui.blank
if modified.any?
ui.info("Modified:")
modified.each { |path| ui.indent(path) }
ui.blank
end
if new_files.any?
ui.info("New:")
new_files.each { |path| ui.indent(path) }
ui.blank
end
if deleted.any?
ui.info("Deleted (remote only):")
deleted.each { |path| ui.indent(path) }
ui.blank
end
ui.info("Run `shai push` to upload changes.")
end
rescue NotFoundError
ui.error("Configuration '#{slug}' not found on remote.")
exit EXIT_NOT_FOUND
rescue NetworkError => e
ui.error(e.message)
exit EXIT_NETWORK_ERROR
end
end
desc "pull", "Pull remote changes to local"
option :dry_run, type: :boolean, default: false, desc: "Show what would be pulled"
option :force, type: :boolean, default: false, aliases: "-f", desc: "Overwrite local files without prompting"
def pull
require_auth!
shairc = load_shairc
slug = shairc["slug"]
display_name = "#{credentials.username}/#{slug}"
begin
remote_tree = ui.spinner("Fetching remote state...") do
api.get_tree(slug)["tree"]
end
validate_tree_paths!(remote_tree, Dir.pwd)
local_tree = build_local_tree(shairc)
remote_files = remote_tree.select { |n| n["kind"] == "file" }
.each_with_object({}) { |n, h| h[n["path"]] = n["content"] }
local_files = local_tree.select { |n| n[:kind] == "file" }
.each_with_object({}) { |n, h| h[n[:path]] = n[:content] }
to_create = []
to_update = []
remote_files.each do |path, content|
if local_files.key?(path)
to_update << {path: path, content: content} if local_files[path] != content
else
to_create << {path: path, content: content}
end
end
if to_create.empty? && to_update.empty?
ui.info("Already up to date with #{display_name}")
return
end
if options[:dry_run]
ui.("Would pull from #{display_name}:")
ui.blank
to_create.each { |f| ui.display_file_operation(:would_create, f[:path]) }
to_update.each { |f| ui.display_file_operation(:would_update, f[:path]) }
ui.blank
ui.info("No changes made (dry run)")
return
end
unless options[:force] || to_update.empty?
ui.warning("The following local files will be overwritten:")
to_update.each { |f| ui.indent(f[:path]) }
ui.blank
unless ui.yes?("Continue and overwrite these files?")
ui.info("Pull cancelled.")
return
end
end
ui.("Pulling from #{display_name}...")
ui.blank
all_folders = Set.new
(to_create + to_update).each do |file|
parts = File.dirname(file[:path]).split("/")
parts.each_with_index do |_, i|
folder_path = parts[0..i].join("/")
all_folders << folder_path unless folder_path == "."
end
end
all_folders.sort.each do |folder|
unless Dir.exist?(folder)
FileUtils.mkdir_p(folder)
ui.display_file_operation(:created, folder)
end
end
to_create.each do |file|
File.write(file[:path], file[:content])
ui.display_file_operation(:created, file[:path])
end
to_update.each do |file|
File.write(file[:path], file[:content])
ui.display_file_operation(:updated, file[:path])
end
ui.blank
ui.success("Pulled #{to_create.length + to_update.length} items from #{display_name}")
rescue NotFoundError
ui.error("Configuration '#{slug}' not found on remote.")
exit EXIT_NOT_FOUND
rescue PermissionDeniedError
ui.error("You don't have permission to access '#{display_name}'.")
exit EXIT_PERMISSION_DENIED
rescue NetworkError => e
ui.error(e.message)
exit EXIT_NETWORK_ERROR
end
end
desc "diff", "Show diff between local and remote"
def diff
require_auth!
shairc = load_shairc
slug = shairc["slug"]
begin
remote_tree = ui.spinner("Fetching remote state...") do
api.get_tree(slug)["tree"]
end
local_tree = build_local_tree(shairc)
remote_files = remote_tree.select { |n| n["kind"] == "file" }
.each_with_object({}) { |n, h| h[n["path"]] = n["content"] }
local_files = local_tree.select { |n| n[:kind] == "file" }
.each_with_object({}) { |n, h| h[n[:path]] = n[:content] }
has_diff = false
local_files.each do |path, content|
if remote_files.key?(path) && remote_files[path] != content
has_diff = true
ui.info("--- remote #{path}")
ui.info("+++ local #{path}")
file_diff = Diffy::Diff.new(remote_files[path], content, context: 3)
ui.diff(file_diff.to_s)
ui.blank
end
end
local_files.each do |path, content|
next if remote_files.key?(path)
has_diff = true
ui.info("--- /dev/null")
ui.info("+++ local #{path}")
file_diff = Diffy::Diff.new("", content, context: 3)
ui.diff(file_diff.to_s)
ui.blank
end
remote_files.each do |path, content|
next if local_files.key?(path)
has_diff = true
ui.info("--- remote #{path}")
ui.info("+++ /dev/null")
file_diff = Diffy::Diff.new(content, "", context: 3)
ui.diff(file_diff.to_s)
ui.blank
end
ui.info("No differences found.") unless has_diff
rescue NotFoundError
ui.error("Configuration '#{slug}' not found on remote.")
exit EXIT_NOT_FOUND
rescue NetworkError => e
ui.error(e.message)
exit EXIT_NETWORK_ERROR
end
end
end
end
|