Module: DevContext::Commands::Find

Included in:
DevContext::CLI
Defined in:
lib/dev_context/commands/find.rb

Instance Method Summary collapse

Instance Method Details

#cmd_findObject



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
# File 'lib/dev_context/commands/find.rb', line 6

def cmd_find
  args = argv.dup
  opts = { stashes: false, branches: false, paths: false, all: false }
  parser = OptionParser.new
  parser.on("--stashes", "Search stash entry titles") { opts[:stashes] = true }
  parser.on("--branches", "Search current branch names") { opts[:branches] = true }
  parser.on("--paths", "Search repo paths") { opts[:paths] = true }
  parser.on("--all", "Show likely-forgotten work (stashes + dirty repos + detached HEAD)") { opts[:all] = true }
  parser.parse!(args)

  pattern = args.shift
  return usage_error("dx find [--stashes|--branches|--paths] <pattern>\n       dx find --all") unless args.empty?
  return usage_error("dx find [--stashes|--branches|--paths] <pattern>\n       dx find --all") if opts[:all] && pattern
  return usage_error("dx find [--stashes|--branches|--paths] <pattern>\n       dx find --all") if !opts[:all] && blank?(pattern)

  scopes = find_scopes(opts)
  rows = opts[:all] ? find_all_rows(scopes) : find_pattern_rows(pattern, scopes)
  if rows.empty?
    out.puts(opts[:all] ? "No likely-forgotten work found" : "No matches")
    return 0
  end

  render_find_table(rows)
  0
rescue OptionParser::InvalidOption => e
  err.puts("dx: #{e.message}")
  usage_error("dx find [--stashes|--branches|--paths] <pattern>\n       dx find --all")
end