Module: Apidepth::CLI::Setup

Defined in:
lib/apidepth/cli/setup.rb

Constant Summary collapse

DASHBOARD_KEYS_URL =
"https://apidepth.io/dashboard/api-keys".freeze

Class Method Summary collapse

Class Method Details

._open_browser(url) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/apidepth/cli/setup.rb', line 101

def self._open_browser(url)
  if RUBY_PLATFORM.include?("darwin")
    system("open", url)
  elsif RUBY_PLATFORM.include?("linux")
    system("xdg-open", url)
  else
    $stdout.puts "Visit: #{url}"
  end
end

._print_result(result, no_prompt:) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/apidepth/cli/setup.rb', line 111

def self._print_result(result, no_prompt:)
  framework_label = result.name.to_s.capitalize
  $stdout.puts "\nDetected: #{framework_label}" unless no_prompt

  if result.initializer_path && !no_prompt
    $stdout.puts "\nAdd the following to #{result.initializer_path}:"
    $stdout.puts
    $stdout.puts result.initializer_snippet
    $stdout.print "Write to #{result.initializer_path}? [y/N] "
    answer = $stdin.gets&.strip&.downcase
    if answer == "y"
      full_path = File.join(Dir.pwd, result.initializer_path)
      FileUtils.mkdir_p(File.dirname(full_path))
      File.write(full_path, result.initializer_snippet)
      $stdout.puts "Written to #{result.initializer_path}"
    else
      $stdout.puts "(Not written — copy the snippet above into your codebase)"
    end
  else
    $stdout.puts result.initializer_snippet
  end
  $stdout.puts "\nRun `bundle exec apidepth test` to confirm events are reaching the collector." unless no_prompt
end

.parse_options(argv) ⇒ Object



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
# File 'lib/apidepth/cli/setup.rb', line 74

def self.parse_options(argv)
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: bundle exec apidepth setup [options]"
    opts.on("--api-key KEY", "API key (skips browser OAuth)") { |v| options[:api_key] = v }
    opts.on("--collector-url URL", "Override collector URL") { |v| options[:collector_url] = v }
    opts.on("--ignored-hosts HOSTS", "Comma-separated ignored host patterns") do |v|
      options[:ignored_hosts] = v.split(",").map(&:strip)
    end
    opts.on("--no-prompt", "Non-interactive mode; output to stdout only") { options[:no_prompt] = true }
    opts.on("--framework NAME", "Override framework detection (rails|sinatra|generic)") do |v|
      options[:framework] = v
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end
  begin
    parser.parse!(argv.dup)
  rescue OptionParser::InvalidOption => e
    warn e.message
    exit 1
  end
  options
end

.run(argv = ARGV) ⇒ Object



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
# File 'lib/apidepth/cli/setup.rb', line 21

def self.run(argv = ARGV)
  options = parse_options(argv)
  api_key = options[:api_key]
  collector_url = options[:collector_url]
  ignored_hosts = options[:ignored_hosts] || []
  no_prompt = options[:no_prompt]
  framework_override = options[:framework]

  # Interactive: open dashboard and prompt for key
  unless api_key || no_prompt
    $stdout.puts "\nApidepth SDK Setup"
    $stdout.puts "" * 40
    $stdout.puts "\nOpening your API keys page..."
    _open_browser(DASHBOARD_KEYS_URL)
    $stdout.print "\nPaste your API key: "
    api_key = $stdin.gets&.strip
    if api_key.nil? || api_key.empty?
      warn "No API key provided. Aborting."
      exit 1
    end
  end

  # Interactive: prompt for ignored hosts
  unless no_prompt
    $stdout.puts "\nDefault ignored hosts (always skipped):"
    %w[localhost 127.0.0.1 0.0.0.0 ::1].each { |h| $stdout.puts "#{h}" }
    $stdout.puts "#{collector_url || 'collector.apidepth.io'}"
    $stdout.puts "\nAny internal API patterns to ignore? (comma-separated, wildcards ok)"
    $stdout.puts "  Examples: *.internal, *.local, *.svc.cluster.local, *.railway.internal"
    $stdout.print "> "
    input = $stdin.gets&.strip || ""
    ignored_hosts += input.split(",").map(&:strip).reject(&:empty?) unless input.empty?
  end

  result = FrameworkDetector.detect(
    dir: Dir.pwd,
    api_key: api_key,
    ignored_hosts: ignored_hosts,
    collector_url: collector_url
  )
  # Override detected framework if flag provided
  if framework_override
    result = FrameworkDetector.detect(
      dir: Dir.pwd,
      api_key: api_key,
      ignored_hosts: ignored_hosts,
      collector_url: collector_url
    )
  end

  _print_result(result, no_prompt: no_prompt)
end