-
Notifications
You must be signed in to change notification settings - Fork 15
/
ow-cli
executable file
·67 lines (57 loc) · 1.87 KB
/
ow-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'clamp'
require 'English'
module OpenwareCli
class Base < ::Clamp::Command
def shell(command, fail_on_error = true)
warn "Executing: #{command}"
system(command)
raise 'Command failed' if fail_on_error && $CHILD_STATUS.exitstatus != 0
end
end
class Build < Base
def version(image)
path = File.join(image, 'VERSION')
raise "#{path} not found" unless File.exist?(path)
File.read(path).strip
end
def build(image)
shell("docker build -t quay.io/openware/#{image}:#{version(image)} --build-arg=VERSION=#{version(image)} #{image}")
shell("docker push quay.io/openware/#{image}:#{version(image)}") if push?
end
option '--push', :flag, 'Push the image after build', default: false
option '--all-images', :flag, 'Build all images', default: false
option '--image', 'IMAGE', 'Build a specific image', default: nil
def execute
if all_images?
warn 'TODO: call build for every image'
elsif image
build(image)
else
warn 'Nothing to do'
end
end
end
class BuildIncremental < Base
option '--push', :flag, 'Push the image after build', default: false
def execute
images.each do |image|
params = ['--image', image]
params << '--push' if push?
Build.run(File.basename($PROGRAM_NAME), params)
end
end
def images
IO.popen('git log -1 --stat').read
.split("\n").map { |s| (m = s.match(%r{^ ([^/]+)/})) && m[1] }
.compact.map { |s| (m = s.match(/{\w+ => (\w+)}/)) && m[1] || s }
.compact.uniq
end
end
class Root < ::Clamp::Command
subcommand 'build', 'build a specific image', Build
subcommand 'incremental-build', 'build images which were changed in the last patch only', BuildIncremental
end
end
OpenwareCli::Root.run