discharger 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3ccade0fc5dd3187492ca714a564007743c6b4b18633293fcd95de540e8b8cb
4
- data.tar.gz: 55833bfc91a090b58e1a853df4138699eab4dee9b5f60f5a5d01911d5adf580c
3
+ metadata.gz: 86a58f41a34e61bfc155a9563e55bbc1eb42d4bd6904c09fe4aafb9bc61f3411
4
+ data.tar.gz: 62d3b3ad5ffec0dcdee073f5ca0109416b383dd2fe180f8501af47f2028d9122
5
5
  SHA512:
6
- metadata.gz: e679dfc9501cc0d0739143785af0ef29de20295577ed0971c21fb66a4cb9506d67b3d644a19497e4a0353f925180413ae978d8366fb614b4a544150f3014b99d
7
- data.tar.gz: a92efb287ab888f7ff550797cf54bbdd6d8bad18d23cedfbc1f673309c4969ac86f1b9afc4fb7b2d8d9902a6d250854592d79056e5d2692635c1f9ef5879dfde
6
+ metadata.gz: d0f6c58d5fde62fb7dd63b5dbe56c867600eea46e5c8e74b9eee22025da5dd299bf9d0b1093a40a67411c5cc279b9ca826e0c6d0af8659635fb6692279fa8fab
7
+ data.tar.gz: 4ef09b9ca157755c99aa693d3c768e11df6f4fb470ed2d3c4736cf7c2fb894fb46352e941e550c2c200ec95bfd98f40a5daf22ca0928431231fdad04e07fe1ca
data/CHANGELOG.md CHANGED
@@ -5,14 +5,14 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
- ## [0.2.31] - 2026-04-09
8
+ ## [0.3.1] - 2026-05-13
9
9
 
10
10
  ### Added
11
11
 
12
- - Unified setup entry point with pre-steps support (1459633)
12
+ - database.prefer_docker setup.yml option for explicit Docker vs native PostgreSQL selection (2fca81e)
13
13
 
14
- ## [0.2.30] - 2026-03-11
14
+ ## [0.2.31] - 2026-04-09
15
15
 
16
- ### Changed
16
+ ### Added
17
17
 
18
- - Use git trailer fragments for changelog generation (70396ca)
18
+ - Unified setup entry point with pre-steps support (1459633)
data/README.md CHANGED
@@ -163,6 +163,12 @@ database:
163
163
  name: "db-your-app"
164
164
  version: "14"
165
165
  password: "postgres"
166
+ # Optional. Controls how the docker step handles a native PostgreSQL
167
+ # already listening on the configured port:
168
+ # omitted/nil/false - silently skip Docker and use the native instance (legacy default)
169
+ # true - always create the Docker container; fail if a native instance holds the port
170
+ # "prompt" - ask the developer (interactive shells only; non-interactive runs fall back to native)
171
+ prefer_docker: "prompt"
166
172
 
167
173
  redis:
168
174
  port: 6379
@@ -7,35 +7,8 @@ module Discharger
7
7
  module Commands
8
8
  class DockerCommand < BaseCommand
9
9
  def execute
10
- # Setup database container if configured
11
- if database_configured?
12
- puts " → Checking database configuration..." unless ENV["QUIET_SETUP"]
13
- if native_postgresql_available?
14
- puts " → Native PostgreSQL detected on port #{native_postgresql_port}, skipping Docker setup" unless ENV["QUIET_SETUP"]
15
- ENV["DB_PORT"] ||= native_postgresql_port.to_s
16
- else
17
- puts " → No native PostgreSQL found, setting up Docker container..." unless ENV["QUIET_SETUP"]
18
- ensure_docker_running
19
- setup_container(
20
- name: database_config.name || "db-app",
21
- port: database_config.port || 5432,
22
- image: "postgres:#{database_config.version || "14"}",
23
- env: {"POSTGRES_PASSWORD" => database_config.password || "postgres"},
24
- volume: "#{database_config.name || "db-app"}:/var/lib/postgresql/data",
25
- internal_port: 5432
26
- )
27
- end
28
- end
29
-
30
- # Setup Redis container if configured
31
- if redis_configured?
32
- setup_container(
33
- name: redis_config.name || "redis-app",
34
- port: redis_config.port || 6379,
35
- image: "redis:#{redis_config.version || "latest"}",
36
- internal_port: 6379
37
- )
38
- end
10
+ setup_database if database_configured?
11
+ setup_redis if redis_configured?
39
12
  end
40
13
 
41
14
  def can_execute?
@@ -49,6 +22,84 @@ module Discharger
49
22
 
50
23
  private
51
24
 
25
+ def setup_database
26
+ puts " → Checking database configuration..." unless ENV["QUIET_SETUP"]
27
+
28
+ case database_config.prefer_docker
29
+ when true
30
+ create_database_container_or_fail
31
+ when "prompt"
32
+ if native_postgresql_available? && wants_native_postgresql?
33
+ use_native_postgresql
34
+ else
35
+ create_database_container_or_fail
36
+ end
37
+ else
38
+ # Legacy default: prefer the native instance when one is available.
39
+ if native_postgresql_available?
40
+ use_native_postgresql
41
+ else
42
+ create_database_container
43
+ end
44
+ end
45
+ end
46
+
47
+ def setup_redis
48
+ setup_container(
49
+ name: redis_config.name || "redis-app",
50
+ port: redis_config.port || 6379,
51
+ image: "redis:#{redis_config.version || "latest"}",
52
+ internal_port: 6379
53
+ )
54
+ end
55
+
56
+ def use_native_postgresql
57
+ puts " → Native PostgreSQL detected on port #{native_postgresql_port}, skipping Docker setup" unless ENV["QUIET_SETUP"]
58
+ ENV["DB_PORT"] ||= native_postgresql_port.to_s
59
+ end
60
+
61
+ def create_database_container_or_fail
62
+ if native_postgresql_available?
63
+ raise "prefer_docker is set in the database config but native PostgreSQL is " \
64
+ "listening on port #{native_postgresql_port}. Stop the native instance " \
65
+ "(e.g., `brew services stop postgresql@15`) or remove prefer_docker."
66
+ end
67
+ create_database_container
68
+ end
69
+
70
+ def create_database_container
71
+ puts " → No native PostgreSQL found, setting up Docker container..." unless ENV["QUIET_SETUP"]
72
+ ensure_docker_running
73
+ setup_container(
74
+ name: database_config.name || "db-app",
75
+ port: database_config.port || 5432,
76
+ image: "postgres:#{database_config.version || "14"}",
77
+ env: {"POSTGRES_PASSWORD" => database_config.password || "postgres"},
78
+ volume: "#{database_config.name || "db-app"}:/var/lib/postgresql/data",
79
+ internal_port: 5432
80
+ )
81
+ end
82
+
83
+ # Ask the developer whether to use the detected native PostgreSQL or set up
84
+ # the configured Docker container instead. Falls back to native (the legacy
85
+ # behavior) when stdin is not a TTY, in CI, or when QUIET_SETUP is set,
86
+ # so non-interactive runs do not hang waiting for input.
87
+ def wants_native_postgresql?
88
+ unless interactive_prompt_available?
89
+ puts " → Non-interactive shell; defaulting to native PostgreSQL on port #{native_postgresql_port}" unless ENV["QUIET_SETUP"]
90
+ return true
91
+ end
92
+
93
+ puts "Native PostgreSQL detected on port #{native_postgresql_port}. Use the Docker container anyway?\n ===> Type Y to set up the Docker container\nOtherwise hit any key to use the native PostgreSQL."
94
+ $stdin.gets&.chomp != "Y"
95
+ end
96
+
97
+ def interactive_prompt_available?
98
+ return true if ENV["DISCHARGER_FORCE_INTERACTIVE"]
99
+ return false if ENV["CI"] || ENV["QUIET_SETUP"]
100
+ $stdin.tty?
101
+ end
102
+
52
103
  def setup_container(name:, port:, image:, internal_port:, env: {}, volume: nil)
53
104
  log "Checking #{name} container"
54
105
 
@@ -37,13 +37,14 @@ module Discharger
37
37
  end
38
38
 
39
39
  class DatabaseConfig
40
- attr_accessor :port, :name, :version, :password
40
+ attr_accessor :port, :name, :version, :password, :prefer_docker
41
41
 
42
42
  def initialize
43
43
  @port = 5432
44
44
  @name = "db-app"
45
45
  @version = "14"
46
46
  @password = "postgres"
47
+ @prefer_docker = nil
47
48
  end
48
49
 
49
50
  def from_hash(hash)
@@ -51,6 +52,7 @@ module Discharger
51
52
  @name = hash["name"] if hash["name"]
52
53
  @version = hash["version"] if hash["version"]
53
54
  @password = hash["password"] if hash["password"]
55
+ @prefer_docker = hash["prefer_docker"] if hash.key?("prefer_docker")
54
56
  end
55
57
  end
56
58
 
@@ -1,3 +1,3 @@
1
1
  module Discharger
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discharger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay