Class: Wisco::Commands::Profile

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/wisco/commands/profile.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#add(name = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wisco/commands/profile.rb', line 17

def add(name = nil)
  hostname = Wisco::Profiles.prompt_hostname_selection
  puts "Hostname: #{hostname}"

  api_token = Wisco::Profiles.prompt_token('API token (input hidden): ')

  if name.nil?
    suggestion = Wisco::Profiles.suggest_name(hostname)
    name = Wisco::Profiles.prompt_name(suggestion: suggestion)
  end

  if Wisco::Profiles.exists?(name)
    Wisco::TerminalOutput.emit_error("Error: Profile \"#{name}\" already exists. Use 'wisco profile edit #{name}' to update it.")
    exit 1
  end

  Wisco::Profiles.set(name, hostname, api_token)
  puts "Profile \"#{name}\" saved to #{Wisco::Profiles.profiles_path}."
end

#currentObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/wisco/commands/profile.rb', line 201

def current
  config_path = Wisco.config_path(Dir.pwd)
  unless File.exist?(config_path)
    Wisco::TerminalOutput.emit_error("Error: No #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} found in #{Dir.pwd}.")
    Wisco::TerminalOutput.emit_error("       Run 'wisco init' first.")
    exit 1
  end

  config    = Wisco::Config.load_config(config_path)
  api_cfg   = config['workato_developer_api'] || {}
  prof_name = api_cfg['profile'].to_s.strip
  hostname  = api_cfg['hostname'].to_s.strip
  api_token = api_cfg['api_token'].to_s.strip

  if !prof_name.empty?
    profile = Wisco::Profiles.get(prof_name)
    if profile.nil?
      Wisco::TerminalOutput.emit_error("This project references profile \"#{prof_name}\" which does not exist.")
      Wisco::TerminalOutput.emit_error("Run 'wisco profile list' to see available profiles.")
      exit 1
    end
    puts "This project uses profile: #{prof_name}"
    puts "  hostname:  #{profile['hostname']}"
    puts "  api_token: #{Wisco::Profiles.mask_token(profile['api_token'])}"
  elsif !hostname.empty?
    puts 'This project uses inline credentials (no profile).'
    puts "  hostname:  #{hostname}"
    puts "  api_token: #{Wisco::Profiles.mask_token(api_token)}"
  else
    puts 'This project has no Workato API credentials configured.'
    puts "Run 'wisco init' or 'wisco profile use <name>' to configure."
  end
end

#edit(name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wisco/commands/profile.rb', line 66

def edit(name)
  profile = Wisco::Profiles.get(name)
  if profile.nil?
    Wisco::TerminalOutput.emit_error("Error: Profile \"#{name}\" not found.")
    Wisco::TerminalOutput.emit_error("       Run 'wisco profile list' to see available profiles.")
    exit 1
  end

  puts "Editing profile \"#{name}\":"
  puts "  Current hostname:  #{profile['hostname']}"
  puts "  Current api_token: #{Wisco::Profiles.mask_token(profile['api_token'])}"
  puts

  selected = Wisco::Profiles.prompt_hostname_selection(allow_skip: true)
  hostname = selected || profile['hostname']
  puts "Hostname: #{hostname}"

  raw = Wisco::Profiles.prompt_token('New API token (input hidden, Enter to keep current): ')
  api_token = raw.empty? ? profile['api_token'] : raw

  Wisco::Profiles.set(name, hostname, api_token)
  puts "Profile \"#{name}\" updated."
end

#extract(name = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/wisco/commands/profile.rb', line 147

def extract(name = nil)
  config_path = Wisco.config_path(Dir.pwd)
  unless File.exist?(config_path)
    Wisco::TerminalOutput.emit_error("Error: No #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} found in #{Dir.pwd}.")
    Wisco::TerminalOutput.emit_error("       Run 'wisco init' first.")
    exit 1
  end

  config    = Wisco::Config.load_config(config_path)
  api_cfg   = config['workato_developer_api'] || {}
  hostname  = api_cfg['hostname'].to_s.strip
  api_token = api_cfg['api_token'].to_s.strip

  if hostname.empty? || api_token.empty?
    Wisco::TerminalOutput.emit_error('Error: This project does not have inline credentials to extract.')
    Wisco::TerminalOutput.emit_error("       Use 'wisco profile use <name>' to attach an existing profile.")
    exit 1
  end

  duplicate = Wisco::Profiles.find_duplicate(hostname, api_token)
  if duplicate
    puts "A profile with these credentials already exists: #{duplicate} (#{hostname})"
    print 'Reference existing profile instead of creating a new one? (y/n): '
    if $stdin.gets.strip.downcase == 'y'
      api_cfg.delete('hostname')
      api_cfg.delete('api_token')
      api_cfg['profile'] = duplicate
      Wisco::Config.save_config(config_path, config)
      puts "Project config updated to reference profile \"#{duplicate}\"."
      return
    end
  end

  if name.nil?
    suggestion = Wisco::Profiles.suggest_name(hostname)
    name = Wisco::Profiles.prompt_name(suggestion: suggestion)
  end

  if Wisco::Profiles.exists?(name)
    Wisco::TerminalOutput.emit_error("Error: Profile \"#{name}\" already exists. Choose a different name or use 'wisco profile edit #{name}'.")
    exit 1
  end

  Wisco::Profiles.set(name, hostname, api_token)
  puts "Profile \"#{name}\" created in #{Wisco::Profiles.profiles_path}."

  api_cfg.delete('hostname')
  api_cfg.delete('api_token')
  api_cfg['profile'] = name
  Wisco::Config.save_config(config_path, config)
  puts "Project config updated to reference profile \"#{name}\"."
end

#listObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wisco/commands/profile.rb', line 38

def list
  profiles = Wisco::Profiles.all
  if profiles.empty?
    puts "No profiles found. Run 'wisco profile add' to create one."
    return
  end
  puts "Profiles (from #{Wisco::Profiles.profiles_path}):\n\n"
  name_width = profiles.keys.map(&:length).max
  profiles.each do |prof_name, p|
    puts "  #{prof_name.ljust(name_width)}  #{p['hostname']}"
  end
end

#remove(name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wisco/commands/profile.rb', line 91

def remove(name)
  unless Wisco::Profiles.exists?(name)
    Wisco::TerminalOutput.emit_error("Error: Profile \"#{name}\" not found.")
    exit 1
  end
  print "Remove profile \"#{name}\"? (y/n): "
  unless $stdin.gets.strip.downcase == 'y'
    puts 'Aborted.'
    return
  end
  Wisco::Profiles.delete(name)
  puts "Profile \"#{name}\" removed."
end

#show(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wisco/commands/profile.rb', line 52

def show(name)
  profile = Wisco::Profiles.get(name)
  if profile.nil?
    Wisco::TerminalOutput.emit_error("Error: Profile \"#{name}\" not found.")
    Wisco::TerminalOutput.emit_error("       Run 'wisco profile list' to see available profiles.")
    exit 1
  end
  puts "Profile: #{name}"
  puts "  hostname:  #{profile['hostname']}"
  puts "  api_token: #{Wisco::Profiles.mask_token(profile['api_token'])}"
end

#use(name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/wisco/commands/profile.rb', line 106

def use(name)
  unless Wisco::Profiles.exists?(name)
    Wisco::TerminalOutput.emit_error("Error: Profile \"#{name}\" not found in #{Wisco::Profiles.profiles_path}.")
    Wisco::TerminalOutput.emit_error("       Run 'wisco profile list' to see available profiles.")
    exit 1
  end

  config_path = Wisco.config_path(Dir.pwd)
  unless File.exist?(config_path)
    Wisco::TerminalOutput.emit_error("Error: No #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} found in #{Dir.pwd}.")
    Wisco::TerminalOutput.emit_error("       Run 'wisco init' first.")
    exit 1
  end

  config  = Wisco::Config.load_config(config_path)
  api_cfg = config['workato_developer_api'] ||= {}
  has_inline = !api_cfg['hostname'].to_s.strip.empty? || !api_cfg['api_token'].to_s.strip.empty?

  if has_inline
    print "This project has inline credentials configured.\nReplace with a reference to profile \"#{name}\"? (y/n): "
    unless $stdin.gets.strip.downcase == 'y'
      puts 'Aborted.'
      return
    end
    api_cfg.delete('hostname')
    api_cfg.delete('api_token')
  end

  api_cfg['profile'] = name
  Wisco::Config.save_config(config_path, config)
  puts "Project config updated to use profile \"#{name}\"."
end