class Gzr::Commands::User::Ls

Public Class Methods

new(options) click to toggle source
Calls superclass method Gzr::Command::new
# File lib/gzr/commands/user/ls.rb, line 33
def initialize(options)
  super()
  @options = options
end

Public Instance Methods

execute(input: $stdin, output: $stdout) click to toggle source
# File lib/gzr/commands/user/ls.rb, line 38
def execute(input: $stdin, output: $stdout)
  say_warning(@options) if @options[:debug]
  with_session do
    f = @options[:fields]
    f += ',credentials_email,credentials_totp,credentials_google,credentials_saml,credentials_oidc' if @options[:"last-login"]
    data = query_all_users(f, "id")
    begin
      say_ok "No users found"
      return nil
    end unless data && data.length > 0

    table_hash = Hash.new
    fields = field_names(@options[:fields])
    fields.unshift 'last_login' if @options[:"last-login"]
    table_hash[:header] = fields unless @options[:plain]
    expressions = fields.collect { |fn| field_expression(fn) }
    table_hash[:rows] = data.map do |row|
      expressions.collect do |e|
        next(eval "row.#{e}") unless (e == 'last_login')
        [
          row.credentials_email()&.logged_in_at(),
          (row.credentials_totp()&.logged_in_at()),
          (row.credentials_google()&.logged_in_at()),
         (row.credentials_saml()&.logged_in_at()),
          (row.credentials_oidc()&.logged_in_at())
        ].compact.max
      end
    end
    table = TTY::Table.new(table_hash)
    alignments = fields.collect do |k|
      (k =~ /id$/) ? :right : :left
    end
    begin
      if @options[:csv] then
        output.puts render_csv(table)
      else
        output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments)
      end
    end if table
  end
end