Class: Harbor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/config.rb

Constant Summary collapse

SCHEMA_VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/harbor/config.rb', line 17

def initialize(path)
  @path = path
  @data = if File.exist?(path)
            YAML.safe_load_file(path, permitted_classes: [Symbol]) || {}
          else
            {}
          end
  @data["version"] ||= SCHEMA_VERSION
  @data["projects"] ||= {}
  @settings = @data.fetch("settings", {})
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/harbor/config.rb', line 10

def path
  @path
end

#settingsObject (readonly)

Returns the value of attribute settings.



10
11
12
# File 'lib/harbor/config.rb', line 10

def settings
  @settings
end

Class Method Details

.load(path = nil) ⇒ Object



12
13
14
15
# File 'lib/harbor/config.rb', line 12

def self.load(path = nil)
  path ||= DEFAULT_CONFIG_PATH
  new(path)
end

Instance Method Details

#add_project(name, path, destinations: nil, default_destination: nil, description: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/harbor/config.rb', line 42

def add_project(name, path, destinations: nil, default_destination: nil, description: nil)
  full_path = File.expand_path(path)
  config_file = "config/deploy.yml"

  unless File.directory?(full_path)
    raise ConfigError, "Path does not exist: #{full_path}"
  end

  unless File.exist?(File.join(full_path, config_file))
    raise ConfigError, "No deploy.yml found at #{File.join(full_path, config_file)}"
  end

  destinations ||= detect_destinations(full_path)

  @data["projects"][name] = Project.new(
    name: name,
    path: full_path,
    destinations: destinations,
    default_destination: default_destination || destinations.first,
    description: description
  ).to_h

  save
end

#audit_db_pathObject



94
95
96
# File 'lib/harbor/config.rb', line 94

def audit_db_path
  setting("audit_db", DEFAULT_AUDIT_DB)
end

#deploy_timeoutObject



82
83
84
# File 'lib/harbor/config.rb', line 82

def deploy_timeout
  setting("deploy_timeout", 600) # 10 minutes
end

#exec_timeoutObject



86
87
88
# File 'lib/harbor/config.rb', line 86

def exec_timeout
  setting("exec_timeout", 120) # 2 minutes
end

#lock_timeoutObject



78
79
80
# File 'lib/harbor/config.rb', line 78

def lock_timeout
  setting("lock_timeout", 1800) # 30 minutes
end

#logs_timeoutObject



90
91
92
# File 'lib/harbor/config.rb', line 90

def logs_timeout
  setting("logs_timeout", 30)
end

#project(name) ⇒ Object

Raises:



35
36
37
38
39
40
# File 'lib/harbor/config.rb', line 35

def project(name)
  attrs = @data["projects"][name]
  raise ProjectNotFound, "Project '#{name}' not found. Run 'harbor list' to see registered projects." unless attrs

  build_project(name, attrs)
end

#projectsObject



29
30
31
32
33
# File 'lib/harbor/config.rb', line 29

def projects
  @data["projects"].map do |name, attrs|
    build_project(name, attrs)
  end
end

#remove_project(name) ⇒ Object

Raises:



67
68
69
70
71
72
# File 'lib/harbor/config.rb', line 67

def remove_project(name)
  raise ProjectNotFound, "Project '#{name}' not found." unless @data["projects"].key?(name)

  @data["projects"].delete(name)
  save
end

#setting(key, default = nil) ⇒ Object



74
75
76
# File 'lib/harbor/config.rb', line 74

def setting(key, default = nil)
  @settings.fetch(key.to_s, default)
end