Class: D2::Commands::Cd

Inherits:
D2::Command
  • Object
show all
Defined in:
lib/d2/commands/cd.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Formatter

#logger

Class Method Details

.helpObject



26
27
28
29
30
31
# File 'lib/d2/commands/cd.rb', line 26

def self.help
  <<~EOF
    Change Directory to Repository. Uses Fuzzy Matching.
    Usage: {{command:#{D2::TOOL_NAME} cd d2}}
  EOF
end

Instance Method Details

#call(args, _name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/d2/commands/cd.rb', line 7

def call(args, _name)
  # This find command will enumerate the src_path directory for all repos
  # Assumes src_path/PROVIDER.com/OWNER/REPO
  # Will return all `REPO` entries relative to the src path
  base_path = File.expand_path(D2::Config.get('src_path', 'default'))
  options = D2::Helpers::Fzy.fuzzy_match(
    "find #{base_path}/*/* -type d -maxdepth 1 -mindepth 1 | sed -n 's|^#{base_path}||p'",
    query: args.shift,
    num_matches: 1
  )

  if options.empty?
    logger.info "Nothing found for #{args.first}"
  else
    path = File.join(base_path, options.first)
    D2::FILE_DESCRIPTOR.write("cd #{path}")
  end
end