Sha256: 8f34c02d133fa68bae25f9fb66f281e63fa85462dbad9047b83920d1d8492439

Contents?: true

Size: 1.48 KB

Versions: 8

Compression:

Stored size: 1.48 KB

Contents

require "yaml"
require "skylight/errors"

module Skylight
  class UserConfig
    attr_accessor :disable_dev_warning, :disable_env_warning

    def initialize(config)
      @config = config
      @file_path = nil
      reload
    end

    def file_path
      return @file_path if @file_path

      config_path = @config[:user_config_path] || begin
        require "etc"
        home_dir = ENV["HOME"] || Etc.getpwuid.dir || (ENV["USER"] && File.expand_path("~#{ENV['USER']}"))
        if home_dir
          File.join(home_dir, ".skylight")
        else
          raise ConfigError,
                "The Skylight `user_config_path` must be defined since the home directory cannot be inferred"
        end
      end

      @file_path = File.expand_path(config_path)
    end

    def disable_dev_warning?
      disable_dev_warning || ENV["SKYLIGHT_DISABLE_DEV_WARNING"] =~ /^true$/i
    end

    def disable_env_warning?
      disable_env_warning
    end

    def reload
      config = File.exist?(file_path) ? YAML.load_file(file_path) : false
      return unless config

      self.disable_dev_warning = !!config["disable_dev_warning"]
      self.disable_env_warning = !!config["disable_env_warning"]
    end

    def save
      FileUtils.mkdir_p(File.dirname(file_path))
      File.open(file_path, "w") do |f|
        f.puts YAML.dump(to_hash)
      end
    end

    def to_hash
      {
        "disable_dev_warning" => disable_dev_warning,
        "disable_env_warning" => disable_env_warning
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
skylight-5.1.0.beta lib/skylight/user_config.rb
skylight-5.0.1 lib/skylight/user_config.rb
skylight-5.0.0 lib/skylight/user_config.rb
skylight-5.0.0.beta5 lib/skylight/user_config.rb
skylight-5.0.0.beta4 lib/skylight/user_config.rb
skylight-5.0.0.beta3 lib/skylight/user_config.rb
skylight-5.0.0.beta2 lib/skylight/user_config.rb
skylight-5.0.0.beta lib/skylight/user_config.rb