Sha256: ab17639bf40cee6bf5c1d70c2178c07926fdecd8d81e35b5219a3030f113606e

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

module ShopifyCli
  ##
  # ShopifyCli::Shopifolk contains the logic to determine if the user appears to be a Shopify staff
  #
  # The Shopifolk Feature flag will persist between runs so if the flag is enabled or disabled,
  # it will still be in that same state until the next class invocation.
  class Shopifolk
    GCLOUD_CONFIG_FILE = File.expand_path('~/.config/gcloud/configurations/config_default')
    DEV_PATH = '/opt/dev'
    SECTION = 'core'
    FEATURE_NAME = 'shopifolk'

    ##
    # will return if the user appears to be a Shopify employee, based on several heuristics
    #
    # #### Returns
    #
    # * `is_shopifolk` - returns true if the user is a Shopify Employee
    #
    # #### Example
    #
    #     ShopifyCli::Shopifolk.check
    #
    def self.check
      ShopifyCli::Shopifolk.new.shopifolk?
    end

    ##
    # will return if the user is a Shopify employee
    #
    # #### Returns
    #
    # * `is_shopifolk` - returns true if the user has `dev` installed and
    # a valid google cloud config file with email ending in "@shopify.com"
    #
    def shopifolk?
      return true if Feature.enabled?(FEATURE_NAME)
      if shopifolk_by_gcloud? && shopifolk_by_dev?
        ShopifyCli::Feature.enable(FEATURE_NAME)
        true
      else
        ShopifyCli::Feature.disable(FEATURE_NAME)
        false
      end
    end

    private

    def shopifolk_by_gcloud?
      ini&.dig("[#{SECTION}]", 'account')&.match?(/@shopify.com\z/)
    end

    def shopifolk_by_dev?
      File.exist?("#{DEV_PATH}/bin/dev") && File.exist?("#{DEV_PATH}/.shopify-build")
    end

    def ini
      @ini ||= begin
        if File.exist?(GCLOUD_CONFIG_FILE)
          CLI::Kit::Ini
            .new(GCLOUD_CONFIG_FILE, default_section: "[#{SECTION}]", convert_types: false)
            .tap(&:parse).ini
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify-cli-1.4.1 lib/shopify-cli/shopifolk.rb
shopify-cli-1.4.0 lib/shopify-cli/shopifolk.rb