Class: RailsMarkup::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/rails_markup/cli.rb,
lib/rails_markup/cli/base.rb,
lib/rails_markup/cli/setup_wizard.rb,
lib/rails_markup/cli/initializer_writer.rb

Defined Under Namespace

Classes: InitializerWriter, SetupWizard

Constant Summary collapse

HEADER_STYLE =

── Lipgloss styles ────────────────────────────────────────

Lipgloss::Style.new.bold(true).foreground("#FFFFFF").background("#5C4AE4").padding(0, 1)
ODD_STYLE =
Lipgloss::Style.new.foreground("#E2E2E2").padding(0, 1)
EVEN_STYLE =
Lipgloss::Style.new.foreground("#A0A0A0").padding(0, 1)
MASKED_STYLE =
Lipgloss::Style.new.foreground("#6B7280").padding(0, 1)
LABEL_STYLE =
Lipgloss::Style.new.bold(true).foreground("#FFFFFF")
HINT_STYLE =
Lipgloss::Style.new.foreground("#6B7280")
SUCCESS_STYLE =
Lipgloss::Style.new.bold(true).foreground("#22C55E")
ERROR_STYLE =
Lipgloss::Style.new.bold(true).foreground("#EF4444")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


532
533
534
# File 'lib/rails_markup/cli.rb', line 532

def self.exit_on_failure?
  true
end

Instance Method Details

#acknowledge(id = nil) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
# File 'lib/rails_markup/cli.rb', line 406

def acknowledge(id = nil)
  return say("#{ERROR_STYLE.render("Error:")} annotation ID is required") unless id

  env = resolve_env(options[:production])
  return unless env

  result = patch_annotation(env, id, "acknowledge")
  return unless result

  $stdout.puts "#{SUCCESS_STYLE.render("Acknowledged")} ##{id}"
end

#configureObject



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
# File 'lib/rails_markup/cli.rb', line 114

def configure
  env_updates = McpConfig::ENV_KEYS.each_with_object({}) do |(opt, env_key), hash|
    hash[env_key] = options[opt] if options[opt]
  end

  if env_updates.empty?
    say "No options provided. Usage:", :yellow
    say ""
    say "  bin/markup configure --dev-url http://localhost:3000"
    say "  bin/markup configure --prod-url URL --prod-token TOKEN"
    say "  bin/markup configure --prod-url URL --global     # Claude Code global"
    say "  bin/markup configure --prod-url URL --codex      # Codex CLI global"
    say ""
    say "  Dev needs only a URL (no token). Production requires both."
    say ""
    say "  Or run: bin/markup setup-production --url=https://yourapp.com"
    say ""
    return
  end

  config = McpConfig.new(scope: resolve_scope)
  config.update_env(env_updates)
  $stdout.puts "#{LABEL_STYLE.render("Updated #{config.scope_label}")}  #{HINT_STYLE.render("(#{config.path})")}"
  $stdout.puts env_table(config.display_env, label: config.scope_label)
end

#dismiss(id = nil) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rails_markup/cli.rb', line 359

def dismiss(id = nil)
  return say("#{ERROR_STYLE.render("Error:")} annotation ID is required") unless id

  env = resolve_env(options[:production])
  return unless env

  result = patch_annotation(env, id, "dismiss", reason: options[:reason])
  return unless result

  $stdout.puts "#{SUCCESS_STYLE.render("Dismissed")} ##{id}"
end

#fetch(env_arg = nil) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/rails_markup/cli.rb', line 435

def fetch(env_arg = nil)
  production = env_arg == "production" || options[:production] || options[:environment] == "production"
  env = resolve_env(production)
  return unless env

  annotations = fetch_pending_from(env)
  return unless annotations

  env_label = production ? "Production" : "Development"
  $stdout.puts ""
  $stdout.puts "#{LABEL_STYLE.render(" #{env_label} ")} #{HINT_STYLE.render(env[:base_url])}"
  $stdout.puts ""

  if annotations.empty?
    say "  No pending annotations.", :yellow
    say ""
    return
  end

  $stdout.puts annotation_table(annotations)
  say ""
  annotations.each { |ann| render_annotation(ann) }
end

#initObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails_markup/cli.rb', line 57

def init
  wizard = Cli::SetupWizard.new(dir: Dir.pwd)
  Bubbletea.run(wizard)
  if wizard.completed
    say ""
    say "Setup complete!", :green
  else
    say ""
    say "Setup cancelled.", :yellow
  end
end

#manObject



521
522
523
524
# File 'lib/rails_markup/cli.rb', line 521

def man
  require_relative "version"
  $stdout.puts render_man_page
end

#mcpObject



80
81
82
83
# File 'lib/rails_markup/cli.rb', line 80

def mcp
  srv = RailsMarkup::Server.new(port: options[:port], mcp_only: true)
  srv.start
end

#pendingObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rails_markup/cli.rb', line 261

def pending
  env = resolve_env(options[:production])
  return unless env

  annotations = fetch_pending_from(env)
  return unless annotations

  env_label = options[:production] ? "Production" : "Development"
  $stdout.puts ""
  $stdout.puts "#{LABEL_STYLE.render(" #{env_label} ")} #{HINT_STYLE.render(env[:base_url])}"
  $stdout.puts ""

  if annotations.empty?
    say "  No pending annotations.", :yellow
    say ""
    return
  end

  $stdout.puts annotation_table(annotations)
  say ""
  annotations.each { |ann| render_annotation(ann) }
end

#reply(id = nil, message = nil) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/rails_markup/cli.rb', line 382

def reply(id = nil, message = nil)
  return say("#{ERROR_STYLE.render("Error:")} annotation ID is required") unless id
  return say("#{ERROR_STYLE.render("Error:")} message is required") unless message

  env = resolve_env(options[:production])
  return unless env

  result = patch_annotation(env, id, "reply", message: message)
  return unless result

  $stdout.puts "#{SUCCESS_STYLE.render("Reply sent")} to ##{id}"
end

#resolve(id = nil) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
# File 'lib/rails_markup/cli.rb', line 335

def resolve(id = nil)
  return say("#{ERROR_STYLE.render("Error:")} annotation ID is required") unless id

  env = resolve_env(options[:production])
  return unless env

  result = patch_annotation(env, id, "resolve", summary: options[:summary])
  return unless result

  $stdout.puts "#{SUCCESS_STYLE.render("Resolved")} ##{id}"
end

#resolve_allObject



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
# File 'lib/rails_markup/cli.rb', line 296

def resolve_all
  env = resolve_env(options[:production])
  return unless env

  annotations = fetch_pending_from(env)
  return unless annotations

  if annotations.empty?
    say "No pending annotations.", :yellow
    return
  end

  resolved = 0
  annotations.each do |ann|
    result = patch_annotation(env, ann["id"], "resolve", summary: options[:summary])
    if result
      $stdout.puts "#{SUCCESS_STYLE.render("Resolved")} ##{ann["id"]}  #{HINT_STYLE.render(ann["content"].to_s[0, 50])}"
      resolved += 1
    else
      $stdout.puts "#{ERROR_STYLE.render("Failed")}  ##{ann["id"]}"
    end
  end

  say ""
  say "#{resolved}/#{annotations.size} annotations resolved.", :green
end

#serverObject



42
43
44
45
# File 'lib/rails_markup/cli.rb', line 42

def server
  srv = RailsMarkup::Server.new(port: options[:port])
  srv.start
end

#session(id = nil) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/rails_markup/cli.rb', line 186

def session(id = nil)
  say "Sessions are only available via MCP tools (in-memory store).", :yellow
  say ""
  say "  Use the rails_markup_session MCP tool in your AI editor."
  say ""
  say "  To view pending annotations: bin/markup pending"
end

#sessionsObject



175
176
177
178
179
180
181
182
183
# File 'lib/rails_markup/cli.rb', line 175

def sessions
  say "Sessions are only available via MCP tools (in-memory store).", :yellow
  say ""
  say "  Use the rails_markup_sessions MCP tool in your AI editor."
  say "  The CLI talks to the Rails API which has database-backed annotations,"
  say "  not session-based ones."
  say ""
  say "  To view pending annotations: bin/markup pending"
end

#setup_productionObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/rails_markup/cli.rb', line 475

def setup_production
  require "securerandom"

  prod_url = options[:url]
  unless prod_url
    say "Usage: bin/markup setup-production --url=https://yourapp.com", :red
    return
  end

  token = SecureRandom.hex(24)

  config = McpConfig.new(scope: resolve_scope)
  config.update_env({
    "RAILS_MARKUP_PROD_URL" => prod_url,
    "RAILS_MARKUP_PROD_TOKEN" => token
  })

  say ""
  say "Production token generated!", :green
  say ""
  say "Token: #{token}"
  say ""
  say "Saved to #{config.scope_label} — CLI and MCP tools are ready."
  say ""
  say "Now add the same token to your Rails app:"
  say ""
  say "  Option A: Rails credentials (recommended)"
  say "    rails credentials:edit"
  say "    # Add:"
  say "    # rails_markup:"
  say "    #   api_token: #{token}"
  say ""
  say "  Option B: Environment variable"
  say "    RAILS_MARKUP_API_TOKEN=#{token}"
  say ""
  say "Then in config/initializers/rails_markup.rb:"
  say '  config.api_token = Rails.application.credentials.dig(:rails_markup, :api_token)'
  say "  # or"
  say '  config.api_token = ENV["RAILS_MARKUP_API_TOKEN"]'
  say ""
  say "Deploy, then verify:"
  say "  bin/markup pending --production"
  say ""
end

#statusObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rails_markup/cli.rb', line 147

def status
  found = false

  McpConfig::SCOPES.each do |scope|
    config = McpConfig.new(scope: scope)
    next unless config.exist?

    env = config.display_env
    next if env.empty?

    found = true
    say ""
    $stdout.puts env_table(env, label: config.scope_label)
  end

  unless found
    say ""
    say "No MCP config found. Run:", :yellow
    say "  rails-markup configure --prod-url URL --prod-token TOKEN"
    say "  rails-markup configure --prod-url URL --global   # Claude Code global"
    say "  rails-markup configure --prod-url URL --codex    # Codex CLI global"
  end
  say ""
end

#versionObject



527
528
529
530
# File 'lib/rails_markup/cli.rb', line 527

def version
  require_relative "version"
  say "rails-markup #{RailsMarkup::VERSION}"
end

#watchObject



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
# File 'lib/rails_markup/cli.rb', line 212

def watch
  env = resolve_env(options[:production])
  return unless env

  env_label = options[:production] ? "Production" : "Development"
  interval = [options[:interval], 1].max

  $stdout.puts ""
  $stdout.puts "#{LABEL_STYLE.render(" Watching #{env_label} ")} #{HINT_STYLE.render(env[:base_url])}"
  $stdout.puts "#{HINT_STYLE.render("  Polling every #{interval}s. Press Ctrl+C to stop.")}"
  $stdout.puts ""

  seen_ids = Set.new
  loop do
    annotations = fetch_pending_from(env)
    break unless annotations

    new_annotations = annotations.reject { |ann| seen_ids.include?(ann["id"]) }
    if new_annotations.any?
      new_annotations.each do |ann|
        seen_ids.add(ann["id"])
        render_annotation(ann)
      end
      $stdout.puts "#{HINT_STYLE.render("  #{seen_ids.size} total seen, #{annotations.size} pending")}"
      $stdout.puts ""
    end

    sleep interval
  rescue Interrupt
    say ""
    say "Stopped watching.", :yellow
    break
  end
end