Module: Wisco::Config

Defined in:
lib/wisco/config.rb

Class Method Summary collapse

Class Method Details

.ensure_api_config(config, config_path) ⇒ Object

Ensures workato_developer_api hostname and api_token are available in config. Resolves profile references, handles conflicts, and prompts for missing inline values. When a profile is used, credentials are injected into the in-memory config so all callers can continue to use config.dig(‘workato_developer_api’, ‘hostname’) etc.



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
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wisco/config.rb', line 9

def ensure_api_config(config, config_path)
  api_cfg     = config['workato_developer_api'] ||= {}
  has_profile = !api_cfg['profile'].to_s.strip.empty?
  has_inline  = !api_cfg['hostname'].to_s.strip.empty? && !api_cfg['api_token'].to_s.strip.empty?

  if has_profile && has_inline
    resolve_profile_conflict(api_cfg, config, config_path)
    has_profile = !api_cfg['profile'].to_s.strip.empty?
  end

  if has_profile
    profile_name = api_cfg['profile']
    profile      = Wisco::Profiles.get(profile_name)
    if profile.nil?
      Wisco::TerminalOutput.emit_error("Error: Profile \"#{profile_name}\" not found in #{Wisco::Profiles.profiles_path}.")
      Wisco::TerminalOutput.emit_error("       Run 'wisco profile list' to see available profiles.")
      exit 1
    end
    api_cfg['hostname']  = profile['hostname']
    api_cfg['api_token'] = profile['api_token']
    return config
  end

  if api_cfg['hostname'].nil? || api_cfg['hostname'].strip.empty?
    print 'Workato API hostname not configured. Enter hostname (e.g. app.au.workato.com): '
    api_cfg['hostname'] = $stdin.gets.strip
  end

  if api_cfg['api_token'].nil? || api_cfg['api_token'].strip.empty?
    print 'Workato API token not configured. Enter your API token: '
    api_cfg['api_token'] = $stdin.gets.strip
  end

  save_config(config_path, config)
  config
end

.load_config(path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wisco/config.rb', line 74

def load_config(path)
  return {} unless File.exist?(path)

  begin
    JSON.parse(File.read(path))
  rescue JSON::ParserError => e
    warn "Warning: Existing #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} contains invalid JSON (#{e.message})."
    warn '         Connector section will be overwritten; other content may be lost.'
    {}
  end
end

.resolve_profile_conflict(api_cfg, config, config_path) ⇒ Object



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

def resolve_profile_conflict(api_cfg, config, config_path)
  Wisco::TerminalOutput.emit_warning('Warning: This project has both a profile reference and inline credentials.')
  puts "  Profile:  #{api_cfg['profile']}"
  puts "  Hostname: #{api_cfg['hostname']}"
  puts
  puts 'Which should be kept?'
  puts "  1. Profile reference (\"#{api_cfg['profile']}\")"
  puts '  2. Inline credentials'
  loop do
    print 'Enter 1 or 2: '
    case $stdin.gets.strip
    when '1'
      api_cfg.delete('hostname')
      api_cfg.delete('api_token')
      save_config(config_path, config)
      puts "Inline credentials removed. Using profile \"#{api_cfg['profile']}\"."
      return
    when '2'
      api_cfg.delete('profile')
      save_config(config_path, config)
      puts 'Profile reference removed. Using inline credentials.'
      return
    else
      warn 'Please enter 1 or 2.'
    end
  end
end

.save_config(path, config) ⇒ Object



86
87
88
89
90
91
# File 'lib/wisco/config.rb', line 86

def save_config(path, config)
  File.write(path, JSON.pretty_generate(config) + "\n")
rescue SystemCallError => e
  warn "Error: Could not write #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME}: #{e.message}"
  exit 1
end