Class: SlackProfile::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/slack_profile/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure? ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/slack_profile/cli.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#batch_field ⇒ Object



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
# File 'lib/slack_profile/cli.rb', line 194

def batch_field
  client = get_client
  user_ids = options[:users].split(",").map(&:strip).reject(&:empty?)

  if user_ids.empty?
    puts "❌ Error: No valid user IDs provided"
    exit 1
  end

  puts "🚀 Updating #{options[:name]} for #{user_ids.length} users..."
  success_count = 0
  errors = 0

  user_ids.each do |user_id|
    begin
      client.set_single_field(user_id, options[:name], options[:value])
      puts "✅ Updated #{options[:name]} for user #{user_id}"
      success_count += 1
    rescue => e
      puts "❌ Failed to update #{options[:name]} for user #{user_id}: #{e.message}"
      errors += 1
    end
  end

  puts "\n📊 Summary: #{success_count}/#{user_ids.length} users updated successfully"
  exit 1 if errors > 0
end

#batch_profile ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/slack_profile/cli.rb', line 225

def batch_profile
  client = get_client
  user_ids = options[:users].split(",").map(&:strip).reject(&:empty?)

  if user_ids.empty?
    puts "❌ Error: No valid user IDs provided"
    exit 1
  end

  profile = JSON.parse(options[:profile])

  puts "🚀 Updating profiles for #{user_ids.length} users..."
  success_count = 0
  errors = 0

  user_ids.each do |user_id|
    begin
      client.set_profile(user_id, profile)
      puts "✅ Updated profile for user #{user_id}"
      success_count += 1
    rescue => e
      puts "❌ Failed to update profile for user #{user_id}: #{e.message}"
      errors += 1
    end
  end

  puts "\n📊 Summary: #{success_count}/#{user_ids.length} users updated successfully"
  exit 1 if errors > 0
rescue JSON::ParserError
  puts "❌ Error: Invalid JSON in profile data"
  exit 1
end

#interactive ⇒ Object



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
45
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
73
74
75
76
77
78
79
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
120
121
122
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
159
# File 'lib/slack_profile/cli.rb', line 13

def interactive
  client = get_client
  prompt = TTY::Prompt.new

  # Fetch custom fields on startup
  custom_fields = {}
  begin
    puts "🔄 Fetching available fields...\n\n"
    custom_fields = client.get_team_profile
  rescue => e
    puts "⚠️  Could not fetch custom fields: #{e.message}\n\n"
  end

  loop do
    puts "🚀 Slack Profile CLI - Interactive Mode\n\n"

    action = prompt.select("What would you like to do?", filter: true, per_page: 10) do |menu|
      menu.choice "Get user profile", :get
      menu.choice "Set a single field for one user", :single
      menu.choice "Set a single field for multiple users", :batch_single
      menu.choice "Set multiple fields for one user", :multiple
      menu.choice "Set multiple fields for multiple users", :batch_multiple
      menu.choice "List available fields", :list
      menu.choice "Exit", :exit
    end

    break if action == :exit

    case action
    when :get
      user_id = prompt.ask("Enter the Slack user ID:") do |q|
        q.validate(/^U\w+/, "User ID should start with 'U'")
        q.required true
      end

      profile = client.get_profile(user_id)
      puts "\n📋 Profile for user #{user_id}:"
      puts JSON.pretty_generate(profile)
      puts ""

    when :list
      puts "\n📋 Available profile fields:"
      puts "\nStandard fields:"
      standard_fields.each { |field| puts "  • #{field}" }

      if custom_fields.any?
        puts "\nCustom fields:"
        custom_fields.each do |id, field|
          puts "  • #{id} - #{field['label']} (#{field['type']})"
        end
      else
        puts "\n📝 No custom fields configured."
      end
      puts ""

    when :single, :batch_single
      user_ids = if action == :batch_single
        prompt.ask("Enter Slack user IDs (comma-separated):") do |q|
          q.required true
        end.split(",").map(&:strip)
      else
        [prompt.ask("Enter the Slack user ID:") do |q|
          q.validate(/^U\w+/, "User ID should start with 'U'")
          q.required true
        end]
      end

      # Build field choices
      field_choices = standard_fields.map { |f| { name: f, value: f } }
      custom_fields.each do |id, field|
        field_choices << { name: "#{field['label']} (#{id})", value: id }
      end
      field_choices << { name: "Enter field ID manually", value: :custom }

      field_name = prompt.select("Which field would you like to update?", field_choices, filter: true, per_page: 15)

      if field_name == :custom
        field_name = prompt.ask("Enter the field name or ID:")
      end

      value = prompt.ask("Enter the value for #{field_name} (leave empty to clear):", default: "")

      if action == :single
        client.set_single_field(user_ids[0], field_name, value)
        puts "✅ Successfully updated #{field_name} for user #{user_ids[0]}\n\n"
      else
        puts "🚀 Updating #{field_name} for #{user_ids.length} users..."
        success_count = 0
        user_ids.each do |user_id|
          begin
            client.set_single_field(user_id, field_name, value)
            puts "✅ Updated #{field_name} for user #{user_id}"
            success_count += 1
          rescue => e
            puts "❌ Failed to update #{field_name} for user #{user_id}: #{e.message}"
          end
        end
        puts "\n📊 Summary: #{success_count}/#{user_ids.length} users updated successfully\n\n"
      end

    when :multiple, :batch_multiple
      user_ids = if action == :batch_multiple
        prompt.ask("Enter Slack user IDs (comma-separated):") do |q|
          q.required true
        end.split(",").map(&:strip)
      else
        [prompt.ask("Enter the Slack user ID:") do |q|
          q.validate(/^U\w+/, "User ID should start with 'U'")
          q.required true
        end]
      end

      puts "\n📝 You can set multiple fields using JSON format."
      puts 'Example: {"first_name":"John","last_name":"Doe","title":"Developer"}'

      profile_json = prompt.ask("Enter profile data as JSON:") do |q|
        q.required true
        q.validate ->(input) { JSON.parse(input); true rescue false }, "Invalid JSON format"
      end

      profile = JSON.parse(profile_json)

      if action == :multiple
        client.set_profile(user_ids[0], profile)
        puts "✅ Successfully updated profile for user #{user_ids[0]}\n\n"
      else
        puts "🚀 Updating profiles for #{user_ids.length} users..."
        success_count = 0
        user_ids.each do |user_id|
          begin
            client.set_profile(user_id, profile)
            puts "✅ Updated profile for user #{user_id}"
            success_count += 1
          rescue => e
            puts "❌ Failed to update profile for user #{user_id}: #{e.message}"
          end
        end
        puts "\n📊 Summary: #{success_count}/#{user_ids.length} users updated successfully\n\n"
      end
    end
  end

  puts "👋 Goodbye!"
rescue TTY::Reader::InputInterrupt
  puts "\n\n👋 Goodbye!"
  exit 0
end

#list_fields ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/slack_profile/cli.rb', line 259

def list_fields
  client = get_client
  fields = client.get_team_profile

  puts "Available profile fields:"
  puts "\nStandard fields:"
  standard_fields.each { |field| puts "  #{field}" }

  if fields.any?
    puts "\nCustom fields:"
    fields.each do |id, field|
      puts "  #{id} - #{field['label']} (#{field['type']})"
    end
  else
    puts "\nNo custom fields configured."
  end
rescue Error => e
  puts "❌ Error: #{e.message}"
  exit 1
end

#set_field ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/slack_profile/cli.rb', line 165

def set_field
  client = get_client
  client.set_single_field(options[:user], options[:name], options[:value])
  puts "✅ Successfully updated #{options[:name]} for user #{options[:user]}"
rescue Error => e
  puts "❌ Error: #{e.message}"
  exit 1
end

#set_profile ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/slack_profile/cli.rb', line 177

def set_profile
  client = get_client
  profile = JSON.parse(options[:profile])
  client.set_profile(options[:user], profile)
  puts "✅ Successfully updated profile for user #{options[:user]}"
rescue JSON::ParserError
  puts "❌ Error: Invalid JSON in profile data"
  exit 1
rescue Error => e
  puts "❌ Error: #{e.message}"
  exit 1
end

#version ⇒ Object



281
282
283
# File 'lib/slack_profile/cli.rb', line 281

def version
  puts "slack-profile version #{SlackProfile::VERSION}"
end