dce 1.2.1 → 2.0.0
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 +4 -4
- data/bin/dce +6 -3
- data/lib/dce.rb +50 -56
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfec824823df9097b378b60414248b0f15425f8b36b3b7451976deda98483521
|
4
|
+
data.tar.gz: e4f323a4856ddb13104fb6a685f6a7b592d6ac4132ca27dcf407683a68390020
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e081f242132ddf4bc5f492b70c9cb6ef3c68a6389595719ff7cd9ef057ce4b83f696d90562a62de9de74dc73d67c02c3c2f97f8aee637ccce6533d7d615b291c
|
7
|
+
data.tar.gz: aa71ec9c94d55ad927337ac8f67d342964305116af3ef3d15f93731a6e3488d774e9166dba54863be4e1fa4099ad2f01013055bb86955911e2b34622463ed878
|
data/bin/dce
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
3
5
|
require 'yaml'
|
4
6
|
if ENV['COVERAGE']
|
5
7
|
require 'simplecov'
|
6
8
|
|
7
9
|
# Redefine exec to save coverage first.
|
8
10
|
module Kernel
|
9
|
-
|
11
|
+
alias exec_real exec
|
10
12
|
|
11
13
|
def exec(command)
|
12
14
|
SimpleCov.result
|
@@ -15,12 +17,13 @@ if ENV['COVERAGE']
|
|
15
17
|
end
|
16
18
|
|
17
19
|
SimpleCov.at_exit do
|
20
|
+
# Merge results with coverage from other processes, but suppresses
|
21
|
+
# formatting and output.
|
18
22
|
SimpleCov.result
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
22
|
-
|
23
|
-
require_relative '../lib/dce.rb'
|
26
|
+
require_relative '../lib/dce'
|
24
27
|
|
25
28
|
app = DCE.new
|
26
29
|
app.run
|
data/lib/dce.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Wishlist:
|
2
4
|
# Option to delete .dce_container.
|
3
5
|
# Option for using run instead of exec?
|
4
6
|
|
7
|
+
# The dce commmand class.
|
5
8
|
class DCE
|
6
9
|
# Run the command
|
7
10
|
def run
|
@@ -9,45 +12,38 @@ class DCE
|
|
9
12
|
|
10
13
|
@conf_file = File.join(File.dirname(docker_compose_file), '.dce_container')
|
11
14
|
config_container = nil
|
12
|
-
if File.
|
13
|
-
config_container = File.read @conf_file
|
14
|
-
end
|
15
|
+
config_container = File.read(@conf_file) if File.exist? @conf_file
|
15
16
|
|
16
17
|
if @list_containers
|
17
|
-
|
18
|
+
$stdout.puts(containers.join(', '))
|
18
19
|
exit
|
19
20
|
end
|
20
21
|
|
21
22
|
if @query
|
22
23
|
if config_container
|
23
|
-
|
24
|
+
$stdout.puts(config_container)
|
24
25
|
exit
|
25
26
|
else
|
26
|
-
abort
|
27
|
+
abort 'No container saved.'
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
@container = config_container ? config_container : query_container
|
32
|
-
end
|
31
|
+
@container ||= config_container || query_container
|
33
32
|
|
34
|
-
if @container != config_container
|
35
|
-
File.write(@conf_file, @container)
|
36
|
-
end
|
33
|
+
File.write(@conf_file, @container) if @container != config_container
|
37
34
|
|
38
35
|
# If no command given, open a shell.
|
39
|
-
if
|
40
|
-
@command =
|
36
|
+
if @command.strip.empty?
|
37
|
+
@command = 'if [ -e /usr/bin/fish ]; then /usr/bin/fish; elif [ -e /bin/bash ]; then /bin/bash; else /bin/sh; fi'
|
41
38
|
end
|
42
39
|
|
43
|
-
|
44
|
-
args += 't' if STDIN.tty?
|
45
|
-
container_id = %x{docker-compose ps -q #{@container}}.chomp
|
40
|
+
container_id = `docker compose ps -q #{@container}`.chomp
|
46
41
|
|
47
42
|
abort("Container #{@container} not created.") if container_id.empty?
|
48
43
|
|
49
|
-
|
50
|
-
|
44
|
+
tty = $stdin.tty? ? 't' : ''
|
45
|
+
command = "docker exec -i#{tty} #{container_id} sh -c '#{@command}'"
|
46
|
+
warn "Exec'ing: #{command}" if @verbose
|
51
47
|
exec command unless @dry_run
|
52
48
|
end
|
53
49
|
|
@@ -55,8 +51,8 @@ class DCE
|
|
55
51
|
# Will exit with an error if not found
|
56
52
|
def docker_compose_file
|
57
53
|
unless @compose_file
|
58
|
-
dir = Dir.pwd
|
59
|
-
while dir !=
|
54
|
+
dir = Dir.pwd
|
55
|
+
while dir != '/'
|
60
56
|
file = Dir.glob('docker-compose.{yml,yaml}', base: dir).first
|
61
57
|
if file
|
62
58
|
@compose_file = File.join(dir, file)
|
@@ -65,12 +61,10 @@ class DCE
|
|
65
61
|
dir = File.dirname(dir)
|
66
62
|
end
|
67
63
|
|
68
|
-
|
69
|
-
abort "No docker-compose.yml file found."
|
70
|
-
end
|
64
|
+
abort 'No docker-compose.yml file found.' unless @compose_file
|
71
65
|
end
|
72
66
|
|
73
|
-
|
67
|
+
@compose_file
|
74
68
|
end
|
75
69
|
|
76
70
|
# Parse command line arguments
|
@@ -82,9 +76,7 @@ class DCE
|
|
82
76
|
case option
|
83
77
|
when '-c', '--container'
|
84
78
|
@container = ARGV.shift
|
85
|
-
|
86
|
-
abort "Unknown container #{@container}"
|
87
|
-
end
|
79
|
+
abort "Unknown container #{@container}" unless containers.include? @container
|
88
80
|
when '-v', '--verbose'
|
89
81
|
@verbose = true
|
90
82
|
when '-n', '--dry-run'
|
@@ -95,23 +87,23 @@ class DCE
|
|
95
87
|
when '-l', '--list-containers'
|
96
88
|
@list_containers = true
|
97
89
|
when '-h', '--help'
|
98
|
-
|
99
|
-
Usage: #{File.basename($
|
100
|
-
Runs COMMAND in docker
|
90
|
+
warn <<~HEREDOC
|
91
|
+
Usage: #{File.basename($PROGRAM_NAME)} [OPTIONS]... COMMAND
|
92
|
+
Runs COMMAND in docker compose container.
|
101
93
|
|
102
|
-
On first run, asks for the service container to use and saves it to .dce_container next
|
103
|
-
to the docker-compose.yml file.
|
94
|
+
On first run, asks for the service container to use and saves it to .dce_container next
|
95
|
+
to the docker-compose.yml file.
|
104
96
|
|
105
|
-
If no command given, opens a shell.
|
97
|
+
If no command given, opens a shell.
|
106
98
|
|
107
|
-
Options:
|
108
|
-
|
99
|
+
Options:
|
100
|
+
-c, --container SERVICE use the container of the specified service
|
109
101
|
replaces the selected container in the .dce_container
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
102
|
+
-v, --verbose print exec'ed command
|
103
|
+
-n, --dry-run only print exec'ed command, don't run
|
104
|
+
-?, --print-service print the service saved
|
105
|
+
-l, --list-containers print the containers available
|
106
|
+
-h, --help print this help and exit
|
115
107
|
|
116
108
|
HEREDOC
|
117
109
|
exit
|
@@ -126,26 +118,28 @@ Options:
|
|
126
118
|
# Ask the user to select a container
|
127
119
|
# The options are taken from the docker-compose.yml file
|
128
120
|
def query_container
|
129
|
-
containers
|
130
|
-
|
131
|
-
choice = STDIN.gets.strip
|
121
|
+
warn "Please select container [#{containers.join(', ')}]"
|
122
|
+
choice = $stdin.gets.strip
|
132
123
|
exit if choice.empty?
|
133
|
-
|
134
|
-
|
135
|
-
end
|
124
|
+
|
125
|
+
abort 'Illegal choice.' unless containers.include?(choice)
|
136
126
|
choice
|
137
127
|
end
|
138
128
|
|
139
129
|
# Read containers from docker-compose.yml
|
140
|
-
def
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
130
|
+
def containers
|
131
|
+
unless @containers
|
132
|
+
# Older Psychs took whether to allow YAML aliases as a fourth
|
133
|
+
# argument, while newer has a keyword argument. Try both to be
|
134
|
+
# compatible with as many versions as possible.
|
135
|
+
begin
|
136
|
+
content = YAML.safe_load(File.read(docker_compose_file), aliases: true)
|
137
|
+
rescue ArgumentError
|
138
|
+
content = YAML.safe_load(File.read(docker_compose_file), [], [], true)
|
139
|
+
end
|
140
|
+
@containers = content.key?('services') ? content['services'].keys : content.keys
|
148
141
|
end
|
149
|
-
|
142
|
+
|
143
|
+
@containers
|
150
144
|
end
|
151
145
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dce
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Fini Hansen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Run shell commands in docker.
|
14
14
|
email: xen@xen.dk
|
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
|
-
rubygems_version: 3.
|
41
|
+
rubygems_version: 3.1.6
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: DCE
|