Commit f2e1463

mo khan <mo@mokhan.ca>
2021-02-07 19:51:48
refactor: extract docker class
1 parent f2efd41
Changed files (2)
lib/jive/cli.rb
@@ -15,39 +15,17 @@ module Jive
       subcommand "docker", (Class.new(Thor) do
         desc "build", "build the Dockerfile in the current directory"
         def build
-          Jive.shell.execute([
-            "docker",
-            "build",
-            "--network=host",
-            "-t", image_tag_for(Pathname.pwd),
-            "."
-          ], env: { "DOCKER_BUILDKIT" => "1" })
+          Docker.new.build(Pathname.pwd)
         end
 
         desc "launch", "launch a shell into a container"
         def launch
-          Jive.shell.execute([
-            "docker",
-            "run",
-            "--network=host",
-            '--entrypoint=""',
-            "-it", image_tag_for(Pathname.pwd),
-            "/bin/bash -l"
-          ])
+          Docker.new.launch(Pathname.pwd)
         end
 
         desc "size", "print the size of each image"
         def size
-          Jive.shell.execute([
-            :docker, "image", "inspect", '--format="{{.Size}}"',
-            image_tag_for(Pathname.pwd)
-          ])
-        end
-
-        private
-
-        def image_tag_for(path)
-          "#{path.basename.to_s.downcase}:latest"
+          Docker.new.size(Pathname.pwd)
         end
       end)
 
lib/jive/docker.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+module Jive
+  class Docker
+    attr_reader :shell
+
+    def initialize(shell = ::Jive.shell)
+      @shell = shell
+    end
+
+    def build(path)
+      shell.execute([
+        "docker",
+        "build",
+        "--network=host",
+        "-t", image_tag_for(path),
+        "."
+      ], env: { "DOCKER_BUILDKIT" => "1" })
+    end
+
+    def launch(path)
+      shell.execute([
+        "docker",
+        "run",
+        "--network=host",
+        '--entrypoint=""',
+        "-it", image_tag_for(path),
+        "/bin/bash -l"
+      ])
+    end
+
+    def size(path)
+      shell.execute([
+        :docker, "image", "inspect", '--format="{{.Size}}"',
+        image_tag_for(path)
+      ])
+    end
+
+    private
+
+    def image_tag_for(path)
+      "#{path.basename.to_s.downcase}:latest"
+    end
+  end
+end