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'
SOURCE_NATIVE =
'native'
SOURCE_OBSIDIAN =
'obsidian'
VAULT_SOURCE_EXPLICIT =
'--vault'
VAULT_SOURCE_SAVED_DEFAULT =
'saved default'
VAULT_SOURCE_SESSION =
'session'
VAULT_SOURCE_OBSIDIAN_DEFAULT =
'obsidian default'
ROOT_SOURCE_EXPLICIT =
'--root'
ROOT_SOURCE_CWD =
'current folder'
ROOT_SOURCE_SESSION =
'session'

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CLI.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/architext/cli.rb', line 30

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,
    source: SOURCE_NATIVE,
    root: Dir.pwd,
    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
  @root_source = ROOT_SOURCE_CWD
end

Instance Method Details

#runObject



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
81
82
83
84
85
86
87
88
89
90
# File 'lib/architext/cli.rb', line 54

def run
  parse_options
  return 0 if handled_default_vault_options?

  apply_default_vault
  normalize_source_options
  return run_diagnostics if @options[:diagnose]

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

  source = build_source
  if @options[:dry_run]
    print_dry_run(selected_paths, source)
    return 0
  end

  files = read_selected_files(source, 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, SourceError => e
  ui.show_error "#{@app_name}: #{e.message}"
  1
rescue Interrupt
  @stderr.puts "\n#{@app_name}: cancelled"
  130
end