Sha256: 1edcd589221bf913c6dac656d52671611711f75baa311b66e619c0a7dc36cdc9

Contents?: true

Size: 1.76 KB

Versions: 18

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require 'gitlab/cli_helpers'
module Gitlab
  # Defines constants and methods related to configuration.
  module Configuration
    # An array of valid keys in the options hash when configuring a Gitlab::API.
    VALID_OPTIONS_KEYS = %i[endpoint private_token user_agent sudo httparty].freeze

    # The user agent that will be sent to the API endpoint if none is set.
    DEFAULT_USER_AGENT = "Gitlab Ruby Gem #{Gitlab::VERSION}"

    # @private
    attr_accessor(*VALID_OPTIONS_KEYS)
    # @private
    alias auth_token= private_token=

    # Sets all configuration options to their default values
    # when this module is extended.
    def self.extended(base)
      base.reset
    end

    # Convenience method to allow configuration options to be set in a block.
    def configure
      yield self
    end

    # Creates a hash of options and their values.
    def options
      VALID_OPTIONS_KEYS.inject({}) do |option, key|
        option.merge!(key => send(key))
      end
    end

    # Resets all configuration options to the defaults.
    def reset
      self.endpoint       = ENV['GITLAB_API_ENDPOINT'] || ENV['CI_API_V4_URL']
      self.private_token  = ENV['GITLAB_API_PRIVATE_TOKEN'] || ENV['GITLAB_API_AUTH_TOKEN']
      self.httparty       = get_httparty_config(ENV['GITLAB_API_HTTPARTY_OPTIONS'])
      self.sudo           = nil
      self.user_agent     = DEFAULT_USER_AGENT
    end

    private

    # Allows HTTParty config to be specified in ENV using YAML hash.
    def get_httparty_config(options)
      return if options.nil?

      httparty = Gitlab::CLI::Helpers.yaml_load(options)
      raise ArgumentError, 'HTTParty config should be a Hash.' unless httparty.is_a? Hash

      Gitlab::CLI::Helpers.symbolize_keys httparty
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
gitlab-5.0.0 lib/gitlab/configuration.rb
gitlab-4.20.1 lib/gitlab/configuration.rb
gitlab-4.20.0 lib/gitlab/configuration.rb
fs-gitlab-4.19.3 lib/gitlab/configuration.rb
fs-gitlab-4.19.2 lib/gitlab/configuration.rb
fs-gitlab-4.19.1 lib/gitlab/configuration.rb
gitlab-4.19.0 lib/gitlab/configuration.rb
fs-gitlab-4.18.2 lib/gitlab/configuration.rb
fs-gitlab-4.18.1 lib/gitlab/configuration.rb
gitlab-4.18.0 lib/gitlab/configuration.rb
gitlab-4.17.0 lib/gitlab/configuration.rb
gitlab-4.16.1 lib/gitlab/configuration.rb
gitlab-4.16.0 lib/gitlab/configuration.rb
gitlab-4.15.0 lib/gitlab/configuration.rb
gitlab-4.14.1 lib/gitlab/configuration.rb
gitlab-4.14.0 lib/gitlab/configuration.rb
gitlab-4.13.1 lib/gitlab/configuration.rb
gitlab-4.13.0 lib/gitlab/configuration.rb