<commit_before>require 'colorize'

module Cyclid
  module Cli
    # 'organization' sub-command
    class Organization < Thor
      desc 'show', 'Show details of the organization'
      def show
        client = Cyclid::Client::Tilapia.new(options[:config], debug?)

        begin
          org = client.org_get(client.config.organization)

          # Convert the public key to PEM
          der_key = Base64.decode64(org['public_key'])
          public_key = OpenSSL::PKey::RSA.new(der_key)

          # Pretty print the organization details
          puts 'Name: '.colorize(:cyan) + org['name']
          puts 'Owner Email: '.colorize(:cyan) + org['owner_email']
          puts 'Public Key: '.colorize(:cyan) + public_key.to_pem
          puts 'Members:'.colorize(:cyan)
          if org['users'].any?
            org['users'].each do |user|
              puts "\t#{user}"
            end
          else
            puts "\tNone"
          end
        rescue StandardError => ex
          abort "Failed to get organization: #{ex}"
        end
      end

      desc 'modify', 'Modify the organization'
      long_desc <<-LONGDESC
        Modify the organization.

        The --email option sets the owners email address.
      LONGDESC
      option :email, aliases: '-e'
      def modify
        client = Cyclid::Client::Tilapia.new(options[:config], debug?)

        begin
          client.org_modify(client.config.organization,
                            owner_email: options[:email])
        rescue StandardError => ex
          abort "Failed to modify organization: #{ex}"
        end
      end

      # Commands for managing organization members
      class Member < Thor
        desc 'add USERS', 'Add users to the organization'
        def add(*users)
          client = Cyclid::Client::Tilapia.new(options[:config], debug?)

          begin
            org = client.org_get(client.config.organization)

            # Concat the new list with the existing list and remove any
            # duplicates.
            user_list = org['users']
            user_list.concat users
            user_list.uniq!

            client.org_modify(client.config.organization,
                              members: user_list)
          rescue StandardError => ex
            abort "Failed to add users to the organization: #{ex}"
          end
        end

        desc 'permission USER PERMISSION', 'Modify a members organization permissions'
        long_desc <<-LONGDESC
          Modify the organization permissions for USER.

          PERMISSION must be one of:
            * admin - Give the user organization administrator permissions.
            * write - Give the user organization create/modify permissions.
            * read  - Give the user organization read permissions.
            * none  - Remove all organization permissions.

          With 'none' the user remains an organization member but can not
          interact with it. See the 'member remove' command if you want to
          actually remove a user from your organization.
        LONGDESC
        def permission(user, permission)
          client = Cyclid::Client::Tilapia.new(options[:config], debug?)

          begin
            perms = case permission.downcase
                    when 'admin'
                      { 'admin' => true, 'write' => true, 'read' => true }
                    when 'write'
                      { 'admin' => false, 'write' => true, 'read' => true }
                    when 'read'
                      { 'admin' => false, 'write' => false, 'read' => true }
                    when 'none'
                      { 'admin' => false, 'write' => false, 'read' => false }
                    else
                      raise "invalid permission #{permission}"
                    end

            client.org_user_permissions(client.config.organization, user, perms)
          rescue StandardError => ex
            abort "Failed to modify user permissions: #{ex}"
          end
        end

        desc 'list', 'List organization members'
        def list
          client = Cyclid::Client::Tilapia.new(options[:config], debug?)

          begin<commit_msg>Add an alias for 'org user permissions'<commit_after>require 'colorize'

module Cyclid
  module Cli
    # 'organization' sub-command
    class Organization < Thor
      desc 'show', 'Show details of the organization'
      def show
        client = Cyclid::Client::Tilapia.new(options[:config], debug?)

        begin
          org = client.org_get(client.config.organization)

          # Convert the public key to PEM
          der_key = Base64.decode64(org['public_key'])
          public_key = OpenSSL::PKey::RSA.new(der_key)

          # Pretty print the organization details
          puts 'Name: '.colorize(:cyan) + org['name']
          puts 'Owner Email: '.colorize(:cyan) + org['owner_email']
          puts 'Public Key: '.colorize(:cyan) + public_key.to_pem
          puts 'Members:'.colorize(:cyan)
          if org['users'].any?
            org['users'].each do |user|
              puts "\t#{user}"
            end
          else
            puts "\tNone"
          end
        rescue StandardError => ex
          abort "Failed to get organization: #{ex}"
        end
      end

      desc 'modify', 'Modify the organization'
      long_desc <<-LONGDESC
        Modify the organization.

        The --email option sets the owners email address.
      LONGDESC
      option :email, aliases: '-e'
      def modify
        client = Cyclid::Client::Tilapia.new(options[:config], debug?)

        begin
          client.org_modify(client.config.organization,
                            owner_email: options[:email])
        rescue StandardError => ex
          abort "Failed to modify organization: #{ex}"
        end
      end

      # Commands for managing organization members
      class Member < Thor
        desc 'add USERS', 'Add users to the organization'
        def add(*users)
          client = Cyclid::Client::Tilapia.new(options[:config], debug?)

          begin
            org = client.org_get(client.config.organization)

            # Concat the new list with the existing list and remove any
            # duplicates.
            user_list = org['users']
            user_list.concat users
            user_list.uniq!

            client.org_modify(client.config.organization,
                              members: user_list)
          rescue StandardError => ex
            abort "Failed to add users to the organization: #{ex}"
          end
        end

        desc 'permission USER PERMISSION', 'Modify a members organization permissions'
        long_desc <<-LONGDESC
          Modify the organization permissions for USER.

          PERMISSION must be one of:
            * admin - Give the user organization administrator permissions.
            * write - Give the user organization create/modify permissions.
            * read  - Give the user organization read permissions.
            * none  - Remove all organization permissions.

          With 'none' the user remains an organization member but can not
          interact with it. See the 'member remove' command if you want to
          actually remove a user from your organization.
        LONGDESC
        def permission(user, permission)
          client = Cyclid::Client::Tilapia.new(options[:config], debug?)

          begin
            perms = case permission.downcase
                    when 'admin'
                      { 'admin' => true, 'write' => true, 'read' => true }
                    when 'write'
                      { 'admin' => false, 'write' => true, 'read' => true }
                    when 'read'
                      { 'admin' => false, 'write' => false, 'read' => true }
                    when 'none'
                      { 'admin' => false, 'write' => false, 'read' => false }
                    else
                      raise "invalid permission #{permission}"
                    end

            client.org_user_permissions(client.config.organization, user, perms)
          rescue StandardError => ex
            abort "Failed to modify user permissions: #{ex}"
          end
        end
        map 'perms' => :permission

        desc 'list', 'List organization members'
        def list
          client = Cyclid::Client::Tilapia.new(options[:config], debug?)

          begin
{'fraction_non_alphanumeric': 0.09216644807440086, 'fraction_numerical': 0.0009538571598903064, 'mean_word_length': 1.7492625368731562, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6176503', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2070, 'n_words': 676}