Sha256: a89dc40dacf170fd27f3ae25aba48c1e61563c32629a082aa9b935c6efbe5b26

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module GitlabQuality
  module TestTooling
    module Runtime
      class TokenFinder
        def self.find_token!(token, suffix: nil)
          new(token, suffix).find_token!
        end

        attr_reader :token, :suffix

        def initialize(token, suffix)
          @token = token
          @suffix = suffix
        end

        def find_token!
          find_token_from_attrs || find_token_from_env || find_token_from_file
        end

        def find_token_from_attrs
          token
        end

        def find_token_from_env
          Env.gitlab_ci_api_token
        end

        def find_token_from_file
          @token_from_file ||= File.read(token_file_path).strip
        rescue Errno::ENOENT
          fail "Please provide a valid access token with the `-t/--token` option, the `GITLAB_CI_API_TOKEN` environment variable, or in the `#{token_file_path}` file!"
        end

        private

        def token_file_path
          @token_file_path ||= File.expand_path("../api_token#{"_#{suffix}" if suffix}", __dir__)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gitlab_quality-test_tooling-0.2.2 lib/gitlab_quality/test_tooling/runtime/token_finder.rb
gitlab_quality-test_tooling-0.2.1 lib/gitlab_quality/test_tooling/runtime/token_finder.rb
gitlab_quality-test_tooling-0.2.0 lib/gitlab_quality/test_tooling/runtime/token_finder.rb