Module: Strata::CLI::Helpers::CommandContext

Includes:
DatasourceHelper
Included in:
SubCommands::Create, SubCommands::Table
Defined in:
lib/strata/cli/helpers/command_context.rb

Constant Summary

Constants included from DatasourceHelper

DatasourceHelper::ADAPTER_DRIVER_GEMS

Instance Method Summary collapse

Methods included from DatasourceHelper

#apply_readonly_mode, #create_adapter, #ds_config, #ensure_adapter_driver_gems!, #load_adapter_driver_gems!, #resolve_datasource, #resolve_datasource_value

Instance Method Details

#adapterObject



15
16
17
# File 'lib/strata/cli/helpers/command_context.rb', line 15

def adapter
  @adapter ||= create_adapter(datasource_key)
end

#all_tablesObject



54
55
56
# File 'lib/strata/cli/helpers/command_context.rb', line 54

def all_tables
  table_fetch_result[:tables]
end

#datasource_keyObject



23
24
25
# File 'lib/strata/cli/helpers/command_context.rb', line 23

def datasource_key
  @datasource_key ||= resolve_datasource(prompt: prompt)
end

#promptObject



19
20
21
# File 'lib/strata/cli/helpers/command_context.rb', line 19

def prompt
  @prompt ||= TTY::Prompt.new
end

#table_fetch_resultObject



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
# File 'lib/strata/cli/helpers/command_context.rb', line 27

def table_fetch_result
  @table_fetch_result ||= begin
    tables = with_spinner("Fetching tables from #{datasource_key}...") { adapter.tables }
    if tables.empty?
      # Assuming Prompts and ColorHelper are available in the class or via module
      say Prompts::MSG_NO_TABLES_FOUND % datasource_key, ColorHelper.warning
      {tables: [], failed: false}
    else
      {tables: tables, failed: false}
    end
  rescue => e
    ErrorReporter.log_error(e, context: "create table: failed fetching tables for '#{datasource_key}'")

    if ErrorReporter.connection_error?(e)
      say "Could not connect to datasource '#{datasource_key}'.", ColorHelper.warning
    else
      say "Could not fetch tables from datasource '#{datasource_key}'.", ColorHelper.warning
      say "Reason: #{ErrorReporter.user_message_for(e)}", ColorHelper.warning
    end

    say "Hint: verify credentials/settings and run 'strata datasource test #{datasource_key}'.",
      ColorHelper.info
    say "Details logged to '#{ErrorReporter.log_relative_path}'.", ColorHelper.info
    {tables: [], failed: true}
  end
end