Module: Wisco::Profiles
- Defined in:
- lib/wisco/profile.rb
Constant Summary collapse
- PROFILES_DIR =
File.join(Dir.home, '.wisco').freeze
- PROFILES_FILENAME =
'profiles.yaml'.freeze
- HOSTNAMES =
[ { region: 'US Data Center', hostname: 'www.workato.com' }, { region: 'EU Data Center', hostname: 'app.eu.workato.com' }, { region: 'JP Data Center', hostname: 'app.jp.workato.com' }, { region: 'SG Data Center', hostname: 'app.sg.workato.com' }, { region: 'AU Data Center', hostname: 'app.au.workato.com' }, { region: 'IL Data Center', hostname: 'app.il.workato.com' }, ].freeze
- DATACENTER_NAMES =
{ 'www.workato.com' => 'us', 'app.eu.workato.com' => 'eu', 'app.jp.workato.com' => 'jp', 'app.sg.workato.com' => 'sg', 'app.au.workato.com' => 'au', 'app.il.workato.com' => 'il', }.freeze
Class Method Summary collapse
- .all ⇒ Object
- .delete(name) ⇒ Object
- .exists?(name) ⇒ Boolean
- .find_duplicate(hostname, api_token) ⇒ Object
- .get(name) ⇒ Object
- .load_profiles ⇒ Object
- .mask_token(token) ⇒ Object
- .profiles_path ⇒ Object
- .prompt_hostname_selection(allow_skip: false) ⇒ Object
- .prompt_name(suggestion: nil) ⇒ Object
- .prompt_token(prompt_text = 'API token (input hidden): ') ⇒ Object
- .save_profiles(data) ⇒ Object
- .set(name, hostname, api_token) ⇒ Object
- .suggest_name(hostname) ⇒ Object
Class Method Details
.all ⇒ Object
51 52 53 |
# File 'lib/wisco/profile.rb', line 51 def all load_profiles.dig('workato_developer_api') || {} end |
.delete(name) ⇒ Object
66 67 68 69 70 |
# File 'lib/wisco/profile.rb', line 66 def delete(name) data = load_profiles data['workato_developer_api']&.delete(name) save_profiles(data) end |
.exists?(name) ⇒ Boolean
72 73 74 |
# File 'lib/wisco/profile.rb', line 72 def exists?(name) !get(name).nil? end |
.find_duplicate(hostname, api_token) ⇒ Object
76 77 78 |
# File 'lib/wisco/profile.rb', line 76 def find_duplicate(hostname, api_token) all.find { |_n, p| p['hostname'] == hostname && p['api_token'] == api_token }&.first end |
.get(name) ⇒ Object
55 56 57 |
# File 'lib/wisco/profile.rb', line 55 def get(name) all[name] end |
.load_profiles ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/wisco/profile.rb', line 34 def load_profiles return {} unless File.exist?(profiles_path) YAML.safe_load(File.read(profiles_path)) || {} rescue Psych::SyntaxError => e Wisco::TerminalOutput.emit_error("Error: Could not parse #{profiles_path}: #{e.}") exit 1 end |
.mask_token(token) ⇒ Object
84 85 86 87 88 |
# File 'lib/wisco/profile.rb', line 84 def mask_token(token) return '****' if token.nil? || token.length <= 4 "****#{token[-4..]}" end |
.profiles_path ⇒ Object
30 31 32 |
# File 'lib/wisco/profile.rb', line 30 def profiles_path File.join(PROFILES_DIR, PROFILES_FILENAME) end |
.prompt_hostname_selection(allow_skip: false) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/wisco/profile.rb', line 101 def prompt_hostname_selection(allow_skip: false) puts 'Select your Workato instance:' HOSTNAMES.each_with_index do |entry, i| puts " #{i + 1}. #{entry[:hostname]} (#{entry[:region]})" end skip_hint = allow_skip ? ', or Enter to keep current' : '' loop do print "Enter number (1-#{HOSTNAMES.size})#{skip_hint}: " input = $stdin.gets.strip return nil if allow_skip && input.empty? index = input.to_i if index >= 1 && index <= HOSTNAMES.size return HOSTNAMES[index - 1][:hostname] end warn "Invalid selection. Please enter a number between 1 and #{HOSTNAMES.size}." end end |
.prompt_name(suggestion: nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/wisco/profile.rb', line 121 def prompt_name(suggestion: nil) if suggestion print "Profile name [#{suggestion}]: " input = $stdin.gets.strip input.empty? ? suggestion : input else loop do print 'Profile name: ' input = $stdin.gets.strip return input unless input.empty? warn 'Name cannot be blank.' end end end |
.prompt_token(prompt_text = 'API token (input hidden): ') ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/wisco/profile.rb', line 90 def prompt_token(prompt_text = 'API token (input hidden): ') print prompt_text token = $stdin.noecho(&:gets).strip puts token rescue Errno::ENOTTY # stdin is not a tty (e.g. piped input) — read plainly print prompt_text $stdin.gets.strip end |
.save_profiles(data) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/wisco/profile.rb', line 43 def save_profiles(data) FileUtils.mkdir_p(PROFILES_DIR) File.write(profiles_path, YAML.dump(data)) rescue SystemCallError => e Wisco::TerminalOutput.emit_error("Error: Could not write #{profiles_path}: #{e.}") exit 1 end |
.set(name, hostname, api_token) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/wisco/profile.rb', line 59 def set(name, hostname, api_token) data = load_profiles data['workato_developer_api'] ||= {} data['workato_developer_api'][name] = { 'hostname' => hostname, 'api_token' => api_token } save_profiles(data) end |
.suggest_name(hostname) ⇒ Object
80 81 82 |
# File 'lib/wisco/profile.rb', line 80 def suggest_name(hostname) DATACENTER_NAMES[hostname] end |