Sha256: c383ab3ab772b170fa6c85170b31c416203e377edc3a1648795306ccbf74768d

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

#
#   Loads the Pivotal-Git configuration file
#
# - config_path is the path to the pivotal-git configuration file
#   This loads the file. Throws an error if a key (such as api_token, path, id)
#   is missing. Check #general_error_message for more info
#

module PGit
  class Configuration
    def self.default_options
      {
        'projects' => [
          {
            'api_token' => 'somepivotalatoken124',
            'id' => '12345',
            "path" => "~/some/path/to/a/pivotal-git/project"
          },
          {
            'api_token' => 'somepivotalatoken124',
            'id' => '23429070',
            "path" => "~/some/other/pivotal-git/project"
          }
        ]
      }
    end

    def initialize(config_path = '~/.pgit.rc.yml')
      @expanded_path = File.expand_path(config_path)
      if File.exists? @expanded_path
        config_file = File.open(@expanded_path, 'r')
        @yaml = YAML.load(config_file)

        validate_existence_of_at_least_one_project
        validate_presence_of_items_in_each_project
      else
        raise missing_config_default
      end
    end

    def to_yaml
      @yaml
    end

    private

    def missing_config_default
      "Default configuration file does not exist. Please run `pgit install`"
    end

    def general_error_message
      "Please have the following layout:\n" + YAML.dump(PGit::Configuration.default_options)
    end

    def validate_presence_of_items_in_each_project
      projects = @yaml["projects"]
      all_present = projects.all? do |project|
        project["api_token"] &&
          project["path"] &&
          project["id"]
      end

      unless all_present
        raise "Error: Must have a path, id, and api_token for each project.\n" +
          general_error_message
      end
    end

    def validate_existence_of_at_least_one_project
      unless @yaml["projects"]
        raise "Error: #{@expanded_path} needs at least one project.\n" +
          general_error_message

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pgit-0.0.3 lib/pgit/configuration.rb
pgit-0.0.2 lib/pgit/configuration.rb