Sha256: b48e34744e675ee6b169d267b7eb2cc930c37ea97a8f63711a42a6a1d5791a19

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

require "singleton"

module Jets::Api
  class Config
    include Singleton
    extend Memoist

    def initialize(options = {})
      @options = options
      @config_path = "#{ENV["HOME"]}/.jets/config.yml"
    end

    def api_key
      ENV["JETS_API_KEY"] || data["api_key"] || data["key"] # keep key for backwards compatibility
    end

    def api_key?
      !!api_key
    end

    def clear_api_key
      FileUtils.rm_f(@config_path)
      puts "Removed #{@config_path.sub(ENV["HOME"], "~")}"
    end

    def data
      @data ||= load
    end

    # Ensure a Hash is returned
    def load
      return {} unless File.exist?(@config_path)

      data = YAML.load_file(@config_path)
      if data.is_a?(Hash)
        data
      else
        puts "WARN: #{@config_path} is not in the correct format. Loading an empty hash.".color(:yellow)
        {}
      end
    end

    def prompt
      puts <<~EOL
        You are about to configure your #{pretty_path(@config_path)}
        You can get an api key from www.rubyonjets.com
      EOL
      print "Please provide your api key: "
      $stdin.gets.strip
    end

    # interface method: do not remove
    def update_api_key(api_key = nil)
      api_key ||= prompt
      write(
        key: api_key,     # legacy jets 5 and below
        api_key: api_key  # legacy jets 6 and above
      ) # specify keys to allow
    end

    def write(values = {})
      data = load
      data.merge!(values.deep_stringify_keys)
      FileUtils.mkdir_p(File.dirname(@config_path))
      IO.write(@config_path, YAML.dump(data))
      puts "Updated #{pretty_path(@config_path)}"
    end

    def pretty_path(path)
      path.sub(ENV["HOME"], "~")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jets-6.0.4 lib/jets/api/config.rb
jets-6.0.3 lib/jets/api/config.rb
jets-6.0.2 lib/jets/api/config.rb