Last active
November 29, 2018 14:58
-
-
Save mqu/7a918ed76a4af5e698fdb5da71553187 to your computer and use it in GitHub Desktop.
Mattermost cli using API V4 in ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# Mattermost simple cli command based on API4 from maruTA code | |
# version : 0.3 | |
# usage : | |
# you need to create a new token in Mattermost to use this CLI | |
# and export this token as MM_TOKEN and MM_SERVER (or as an option) | |
# export MM_TOKEN='...' | |
# export MM_SERVER='https://mattermost.localhost/api/v4' | |
# ruby ./mattermost-cli.rb test | |
# mattermost-clib.rb <cmd> <opt> | |
# Available cmds : | |
# - teams-list [-l] [-d] : list teams, optionaly associated channels. | |
# - user-list [-l] : list users | |
# - user-teams [-l] <username> : list all teams for a given user and optionaly public and private channels | |
# | |
# global options | |
# -l : long display | |
# -L : very long display | |
# -d : debug | |
# -t : token | |
# -s : server URL : https://mattermost.your.domain/api/v4/ | |
# | |
# links : | |
# - https://github.com/joshmn/mattermost-ruby (API V3) | |
# - https://github.com/maruTA-bis5/mattermost-api4-ruby (API V4) | |
# - https://github.com/mattermost/mattermost-api-reference/tree/master/v4/source - formal API specifications | |
# - https://api.mattermost.com/ - online API documentation | |
require 'optparse' | |
require "pp" | |
require "mattermost" # gem install mattermost-api4-ruby | |
module Mattermost | |
module Request | |
# get request with auto paging mode. | |
def get(path, options = {}, &block) | |
if path.include? '?per_page=' | |
# start paging with page=0 | |
page=0 | |
result=nil | |
# loop until page size is 0 | |
loop do | |
_path="#{path}&page=#{page}" | |
# intermediate request result | |
_res=get_simple(_path, options = {}, &block) | |
# raise an exception if request result is not OK | |
raise "Mattermost request error #{_res.body['message']}" unless _res.success? | |
# break when no more result is available in result (body) | |
break if _res.body.size==0 | |
# concatenate result body from intermediate body. | |
if result==nil | |
result=_res | |
else | |
result.body.concat _res.body | |
end | |
# incremental paging mode | |
page=page+1 | |
end | |
return result | |
else | |
get_simple(path, options = {}, &block) | |
end | |
end | |
def get_simple(path, options = {}, &block) | |
connection.send(:get) do |request| | |
request.url api(path), options | |
end | |
end | |
end | |
end | |
def usage | |
help = <<USAGE | |
Usage: | |
#{__FILE__} <cmd> opts | |
Available cmds : | |
- teams-list [-l] [-d] : list teams, optionaly associated channels. | |
- user-list [-l] : list users | |
- user-teams [-l] <username> : list all teams for a given user and optionaly public and private channels | |
USAGE | |
puts help | |
exit 0 | |
end | |
options = {} | |
# this options with environment variables à usefull with gitlab-ci jobs. | |
options[:server]=ENV['MM_SERVER'] or options[:server]="https://mattermost.your.domain.local/api/v4" | |
options[:token]=ENV['MM_TOKEN'] | |
OptionParser.new do |opts| | |
opts.banner = "Usage: {__FILE__} [options]" | |
opts.on("-t", "--token", "token for mattermost authentificatiin") do |_v| | |
options[:token] = _v | |
end | |
opts.on("-s", "--server", "mattermost server URL") do |_v| | |
options[:server] = _v | |
end | |
opts.on("-v", "--[no-]verbose", "Run verbosely") do |_v| | |
options[:verbose] = _v | |
end | |
opts.on("-l", "--long", "long display") do |_v| | |
options[:long] = _v | |
end | |
opts.on("-L", "--long-long", "extra long display") do |_v| | |
options[:extra_long] = _v | |
end | |
opts.on("-d", "--debug", "debug mode") do |_v| | |
options[:debug] = _v | |
end | |
end.parse! | |
mm=Mattermost.new_client(options[:server]) | |
mm.use_access_token(options[:token]) | |
case ARGV[0] | |
when "test" | |
pp mm.get_users.body.size | |
# list teams, and associated channels with -l opt and all options and analytics with -d opt | |
when 'teams-list' | |
teams=mm.get_teams(per_page=nil).body | |
teams.each do |t| | |
puts "team:#{t['name']}/#{t['email']}" | |
if options[:long] | |
puts " - description:#{t['description']}" | |
puts " - display-name:#{t['display_name']}" | |
puts " - purpose:#{t['purpose']}" | |
puts " - invite_id:#{t['invite_id']}" | |
# display some statistics about team | |
if options[:extra_long] | |
i=mm.get_analytics(t['id']).body | |
pp i | |
end | |
end | |
pp t if options[:debug] | |
end | |
# list users | |
when 'user-list' | |
opts={ | |
# :paging => true | |
:max => 100 | |
} | |
mm.get_users(opts).body.each do |u| | |
puts "user:#{u['username']}/#{u['email']} " | |
if options[:long] | |
puts " - id:#{u['id']}" | |
puts " - roles:#{u['roles']}" | |
end | |
pp u if options[:debug] | |
end | |
when 'user-teams' | |
user=mm.get_user_by_username(ARGV[1]).body | |
puts "user : #{user['email']}/#{user['id']} " | |
mm.get_teams_for_user(user['id']).body.each do |t| | |
puts "team:#{t['name']}/#{t['display_name']}/#{t['purpose']}/#{t['email']}" | |
if options[:long] | |
mm.get_channels_for_user(user['id'], t['id']).body.each do |c| | |
puts " - channel:#{c['name']}/#{c['display_name']}" | |
pp c if options[:debug] | |
end | |
end | |
end | |
when 'system-ping' | |
pp mm.ping.body | |
when 'system-config' | |
pp mm.get_configuration.body | |
when 'system-client-config' | |
pp mm.get_client_configuration.body | |
# FIXME : mm.get_logs to long or never return. | |
when 'system-logs' | |
pp mm.get('/logs').body # empty :-) | |
when 'system-analytics' | |
pp mm.get('/analytics/old').body | |
else | |
usage | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment