Class: Wisco::Commands::Settings

Inherits:
Thor
  • Object
show all
Defined in:
lib/wisco/commands/settings.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/wisco/commands/settings.rb', line 10

def self.exit_on_failure?
  true
end

Instance Method Details

#add(connection) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wisco/commands/settings.rb', line 80

def add(connection)
  field_names = connection_field_names!
  if field_names.empty?
    Wisco::TerminalOutput.emit_error('Error: The connector defines no connection.fields to scaffold.')
    exit 1
  end
  new_set = field_names.each_with_object({}) { |n, h| h[n] = '' }

  store = settings_store
  data  = read_settings!(store)
  structure = Wisco::SettingsStore.detect_structure(data)

  if Wisco::SettingsStore.set_names(data).include?(connection)
    Wisco::TerminalOutput.emit_error("Error: Connection set \"#{connection}\" already exists in #{store.filename}.")
    Wisco::TerminalOutput.emit_error("       Use 'wisco settings show #{connection}' to view it, or edit it with 'workato edit'.")
    exit 1
  end

  target =
    case structure
    when :nested
      data.merge(connection => new_set)
    when :none
      { connection => new_set }
    when :flat
      migrate_flat(data, store).merge(connection => new_set)
    when :mixed
      Wisco::TerminalOutput.emit_error("Error: #{store.filename} mixes flat keys and named sets; resolve it with 'workato edit' before adding.")
      exit 1
    end

  # A brand-new file is written plaintext; existing files keep their form.
  encrypted = structure != :none && store.encrypted?
  written   = encrypted ? Wisco::SettingsStore::ENCRYPTED_FILENAME : Wisco::SettingsStore::PLAINTEXT_FILENAME
  store.write_all(target, encrypted: encrypted)

  puts "Added connection set \"#{connection}\" to #{written} with #{field_names.length} blank field(s):"
  puts "  #{field_names.join(', ')}"
  puts "Fill in the values with 'workato edit', then run 'wisco settings set #{connection}'."
end

#currentObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/wisco/commands/settings.rb', line 161

def current
  active = active_connection.to_s.strip
  store  = settings_store
  data   = begin
    store.read_all
  rescue Wisco::SettingsStore::MissingKeyError
    nil
  end
  names = data ? Wisco::SettingsStore.set_names(data) : []

  if active.empty?
    puts 'This project has no named connection selected (config.json has no "connection" key).'
    puts "The connector will use the single connection set in #{store.filename}."
    return
  end

  puts "This project uses connection set: #{active}"
  if data.nil?
    Wisco::TerminalOutput.emit_warning("  Could not read #{store.filename} to verify (no master key).")
  elsif names.include?(active)
    puts "  Defined in: #{store.filename}"
  else
    puts "  Not found in #{store.filename}. Run 'wisco settings add #{active}' to create it."
  end
end

#fieldsObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/wisco/commands/settings.rb', line 189

def fields
  raw = connection_fields!

  if options[:format] == 'json'
    puts JSON.pretty_generate(raw)
    return
  end

  if raw.empty?
    puts 'Connector defines no connection fields.'
    return
  end

  rows = raw.map do |f|
    [
      field_attr(f, :name).to_s,
      field_attr(f, :label).to_s,
      (field_attr(f, :control_type) || '(default)').to_s,
      field_attr(f, :optional) ? 'no' : 'yes'
    ]
  end

  headers = %w[Name Label Type Required]
  widths  = headers.each_index.map do |i|
    ([headers[i]] + rows.map { |r| r[i] }).map(&:length).max
  end

  puts "Connection fields (from #{connector_file_basename}):\n\n"
  puts "  #{headers.each_with_index.map { |h, i| h.ljust(widths[i]) }.join('  ')}"
  rows.each do |r|
    puts "  #{r.each_with_index.map { |c, i| c.ljust(widths[i]) }.join('  ')}"
  end
end

#listObject



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
45
46
# File 'lib/wisco/commands/settings.rb', line 16

def list
  store = settings_store
  data  = read_settings!(store)
  names = Wisco::SettingsStore.set_names(data)

  if options[:format] == 'json'
    puts JSON.generate(names)
    return
  end

  case Wisco::SettingsStore.detect_structure(data)
  when :nested, :mixed
    active = active_connection
    puts "Connection sets (#{store.filename}):\n\n"
    width = names.map(&:length).max || 0
    names.each do |n|
      marker = n == active ? '*' : ' '
      puts "  #{marker} #{n.ljust(width)}"
    end
    if active && !active.empty?
      puts
      puts "Active connection (from #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME}): #{active}"
    end
  when :flat
    puts "#{store.filename} contains a single, unnamed connection set."
    puts "No named connection sets are defined. Use 'wisco settings add <name>' to create named sets."
  when :none
    puts "No settings file found in #{store.connector_path}."
    puts "Run 'wisco settings add <name>' to create one, or 'workato edit' to create an encrypted file."
  end
end

#set(connection) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wisco/commands/settings.rb', line 49

def set(connection)
  store = settings_store
  names = begin
    Wisco::SettingsStore.set_names(store.read_all)
  rescue Wisco::SettingsStore::MissingKeyError
    nil
  end

  if names.nil?
    Wisco::TerminalOutput.emit_warning("Warning: could not read #{store.filename} to validate (no master key).")
    Wisco::TerminalOutput.emit_warning('         Config updated anyway.')
  elsif !names.include?(connection)
    Wisco::TerminalOutput.emit_warning("Warning: \"#{connection}\" is not currently defined in #{store.filename}.")
    Wisco::TerminalOutput.emit_warning("         Defined sets: #{names.empty? ? '(none)' : names.join(', ')}")
    Wisco::TerminalOutput.emit_warning("         Config updated anyway. Run 'wisco settings add #{connection}' to create it.")
  end

  cfg = config
  cfg['connection'] = connection
  Wisco::Config.save_config(config_path, cfg)
  puts "Active connection set to \"#{connection}\" in #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME}."
end

#show(connection = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/wisco/commands/settings.rb', line 123

def show(connection = nil)
  store = settings_store
  data  = read_settings!(store)

  name, set =
    case Wisco::SettingsStore.detect_structure(data)
    when :none
      Wisco::TerminalOutput.emit_error("Error: No settings file found in #{store.connector_path}. Nothing to show.")
      exit 1
    when :flat
      if connection
        Wisco::TerminalOutput.emit_error("Error: #{store.filename} has a single unnamed connection set; there is no named set \"#{connection}\".")
        exit 1
      end
      [nil, data]
    when :nested, :mixed
      names = Wisco::SettingsStore.set_names(data)
      if connection.nil?
        Wisco::TerminalOutput.emit_warning('Warning: This settings file has multiple connection sets; specify which one to show.')
        Wisco::TerminalOutput.emit_warning("         Defined sets: #{names.join(', ')}")
        return
      end
      unless names.include?(connection)
        Wisco::TerminalOutput.emit_error("Error: Connection set \"#{connection}\" not found in #{store.filename}.")
        Wisco::TerminalOutput.emit_error("       Defined sets: #{names.join(', ')}")
        exit 1
      end
      [connection, data[connection]]
    end

  if options[:format] == 'json'
    render_set_json(set)
  else
    render_set(name, set, store)
  end
end