Class: Architext::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/architext/cli.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: SearchAttempt

Constant Summary collapse

DEFAULT_QUERY =
'tag:#project/active'
VAULT_SOURCE_EXPLICIT =
'--vault'
VAULT_SOURCE_SAVED_DEFAULT =
'saved default'
VAULT_SOURCE_SESSION =
'session'
VAULT_SOURCE_OBSIDIAN_DEFAULT =
'obsidian default'

Instance Method Summary collapse

Constructor Details

#initialize(argv, io: {}, app_name: 'architext', dependencies: {}) ⇒ CLI

Returns a new instance of CLI.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/architext/cli.rb', line 25

def initialize(argv, io: {}, app_name: 'architext', dependencies: {})
  @stdin = io.fetch(:stdin, $stdin)
  @stdout = io.fetch(:stdout, $stdout)
  @stderr = io.fetch(:stderr, $stderr)
  @clipboard = dependencies.fetch(:clipboard, Clipboard.new)
  @settings = dependencies.fetch(:settings, Settings.new)
  @argv = argv
  @app_name = app_name
  @options = {
    query: nil,
    vault: nil,
    set_default_vault: nil,
    clear_default_vault: false,
    diagnose: false,
    stdout: false,
    dry_run: false,
    all: false
  }
  @vault_source = VAULT_SOURCE_OBSIDIAN_DEFAULT
end

Instance Method Details

#runObject



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

def run
  parse_options
  return 0 if handled_default_vault_options?

  apply_default_vault
  return run_diagnostics if @options[:diagnose]

  selected_paths = gather_selection
  return 1 unless selected_paths
  return no_selection if selected_paths.empty?

  if @options[:dry_run]
    print_dry_run(selected_paths, Obsidian.new(vault: @options[:vault]))
    return 0
  end

  files = read_selected_files(Obsidian.new(vault: @options[:vault]), selected_paths)
  bundle = Bundle.new(files).to_markdown
  write_output(bundle)
  0
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
  ui.show_error "#{@app_name}: #{e.message}"
  @stderr.puts "Run `bin/#{@app_name} --help` for usage."
  2
rescue Obsidian::CommandNotFound => e
  ui.show_error "#{@app_name}: #{e.message}"
  @stderr.puts 'Install/enable Obsidian CLI, or set ARCHITEXT_OBSIDIAN to its path.'
  127
rescue Obsidian::CommandFailed => e
  ui.show_error "#{@app_name}: #{e.message}"
  1
rescue Interrupt
  @stderr.puts "\n#{@app_name}: cancelled"
  130
end