From a6aaed1fcdb16a5c3020843aecdf1e26f68e0430 Mon Sep 17 00:00:00 2001
From: "Glenn Y. Rolland" <glenux@glenux.net>
Date: Fri, 10 Apr 2020 23:41:50 +0200
Subject: [PATCH] Start changing structure

---
 src/config.cr                                 | 11 +++-
 src/config/config.cr                          | 14 -----
 src/config/deployment.cr                      | 16 ++---
 src/config/local.cr                           | 49 ++++++++++-----
 src/config/remote.cr                          | 12 +++-
 src/deployment/docker_image_to_dokku_app.cr   |  4 +-
 src/deployment/mysql_dump_to_dokku_mariadb.cr |  2 +-
 src/pushokku.cr                               | 62 +++++++++++--------
 8 files changed, 98 insertions(+), 72 deletions(-)
 delete mode 100644 src/config/config.cr

diff --git a/src/config.cr b/src/config.cr
index f11d5c9..63d2644 100644
--- a/src/config.cr
+++ b/src/config.cr
@@ -1,9 +1,14 @@
 
 require "yaml"
-require "./config/config"
 require "./config/local"
 require "./config/remote"
 require "./config/deployment"
 
-
-
+class Config
+  YAML.mapping(
+    version: String,
+    locals: Array(LocalConfig),
+    remotes: Array(RemoteConfig),
+    deployments: Array(DeploymentConfig)
+  )
+end
diff --git a/src/config/config.cr b/src/config/config.cr
deleted file mode 100644
index c6856a0..0000000
--- a/src/config/config.cr
+++ /dev/null
@@ -1,14 +0,0 @@
-
-require "yaml"
-require "./local"
-require "./remote"
-require "./deployment"
-
-class Config
-  YAML.mapping(
-    version: String,
-    locals: Array(LocalConfig),
-    remotes: Array(RemoteConfig),
-    deployments: Array(DeploymentConfig)
-  )
-end
diff --git a/src/config/deployment.cr b/src/config/deployment.cr
index 23f0168..d353ef9 100644
--- a/src/config/deployment.cr
+++ b/src/config/deployment.cr
@@ -1,37 +1,37 @@
 
 require "yaml"
 
-class DokkuMariadbConfig
+class DokkuMariadbDeploymentConfigSettings
   YAML.mapping(
     name: String,
     options: YAML::Any | Nil
   )
 end
 
-class DokkuAppConfig
+class DokkuAppDeploymentConfigSettings
   YAML.mapping(
     name: String,
     options: YAML::Any | Nil
   )
 end
 
-class DeploymentMariadbConfig
+class DokkuMariadbDeploymentConfig
   YAML.mapping(
     local: String,
     remote: String,
-    dokku_mariadb: DokkuMariadbConfig,
+    dokku_mariadb: DokkuMariadbDeploymentConfigSettings,
   )
 end
 
-class DeploymentAppConfig
+class DokkuAppDeploymentConfig
   YAML.mapping(
     local: String,
     remote: String,
-    dokku_app: DokkuAppConfig,
+    dokku_app: DokkuAppDeploymentConfigSettings,
   )
 end
 
 alias DeploymentConfig = 
-  DeploymentMariadbConfig | 
-  DeploymentAppConfig
+  DokkuMariadbDeploymentConfig | 
+  DokkuAppDeploymentConfig
 
diff --git a/src/config/local.cr b/src/config/local.cr
index a5f4856..2a26615 100644
--- a/src/config/local.cr
+++ b/src/config/local.cr
@@ -1,32 +1,47 @@
 
 require "yaml"
 
-enum LocalType
-  DOCKER_IMAGE = 1
-  MYSQL_DUMP = 2
-
-  def to_yaml(io)
-    to_s(io)
-  end
-end
-
-class LocalFileConfig
+class ScriptLocalConfigSettings
   YAML.mapping(
-    name: String,
-    type: LocalType, # enum ?
     path: String
   )
 end
 
-class LocalDockerConfig
+class MysqlDumpLocalConfigSettings
+  YAML.mapping(
+    path: String
+  )
+end
+
+class DockerImageLocalConfigSettings
+  YAML.mapping(
+    name: String
+  )
+end
+
+class ScriptLocalConfig
   YAML.mapping(
     name: String,
-    type: LocalType, # enum ?
-    docker_image: String
+    script: ScriptLocalConfigSettings
+  )
+end
+
+class MysqlDumpLocalConfig
+  YAML.mapping(
+    name: String,
+    mysql_dump: MysqlDumpLocalConfigSettings
+  )
+end
+
+class DockerImageLocalConfig
+  YAML.mapping(
+    name: String,
+    docker_image: DockerImageLocalConfigSettings
   )
 end
 
 alias LocalConfig =
-  LocalFileConfig |
-  LocalDockerConfig
+  DockerImageLocalConfig |
+  MysqlDumpLocalConfig |
+  ScriptLocalConfig
 
diff --git a/src/config/remote.cr b/src/config/remote.cr
index 8963a7e..477ed4f 100644
--- a/src/config/remote.cr
+++ b/src/config/remote.cr
@@ -1,11 +1,19 @@
 
 require "yaml"
 
-class RemoteConfig
+class DokkuRemoteConfigSettings
   YAML.mapping(
-    name: String,
     user: String,
     host: String
   )
 end
 
+class DokkuRemoteConfig
+  YAML.mapping(
+    name: String,
+    dokku: DokkuRemoteConfigSettings
+  )
+end
+
+alias RemoteConfig =
+  DokkuRemoteConfig
diff --git a/src/deployment/docker_image_to_dokku_app.cr b/src/deployment/docker_image_to_dokku_app.cr
index 808d15c..e684437 100644
--- a/src/deployment/docker_image_to_dokku_app.cr
+++ b/src/deployment/docker_image_to_dokku_app.cr
@@ -1,8 +1,8 @@
 
 require "colorize"
 
-class DeploymentApp
-  def initialize(@local : LocalDockerConfig, @remote : RemoteConfig, @deployment : DeploymentAppConfig)
+class DockerImageToDokkuAppDeployment
+  def initialize(@local : DockerImageLocalConfig, @remote : RemoteConfig, @deployment : DokkuAppDeploymentConfig)
   end
 
   def run
diff --git a/src/deployment/mysql_dump_to_dokku_mariadb.cr b/src/deployment/mysql_dump_to_dokku_mariadb.cr
index 4f6c775..60005f1 100644
--- a/src/deployment/mysql_dump_to_dokku_mariadb.cr
+++ b/src/deployment/mysql_dump_to_dokku_mariadb.cr
@@ -1,5 +1,5 @@
 
-class DeploymentMariadb
+class MysqlDumpToDokkuMariadbDeployment
   def initialize(@local : LocalFileConfig, @remote : RemoteConfig, @deployment : DeploymentMariadbConfig)
   end
 
diff --git a/src/pushokku.cr b/src/pushokku.cr
index fd5eed4..18374cd 100644
--- a/src/pushokku.cr
+++ b/src/pushokku.cr
@@ -75,6 +75,42 @@ module Pushokku
     end
 
 
+    def self.handle_deployment(config : Config, deployment_config : DeploymentConfig) 
+        local = config.locals.select { |l| l.name == deployment_config.local }.first
+        remote = config.remotes.select { |r| r.name == deployment_config.remote }.first
+
+        if local.nil?
+          puts "Unknown local #{deployment_config.local}. Exiting."
+          exit 2
+        end
+
+        if remote.nil?
+          puts "Unknown remote #{deployment_config.remote}. Exiting."
+          exit 2
+        end
+
+        deployment = 
+          case deployment_config
+          when DokkuAppDeploymentConfig then
+            DockerImageToDokkuAppDeployment.new(
+              local.as(DockerImageLocalConfig), 
+              remote, 
+              deployment_config.as(DokkuAppDeploymentConfig)
+            )
+          when MysqlDumpToDokkuMariadbDeployment then
+            DeploymentMariadb.new(
+              local.as(MysqlDumpLocalConfig), 
+              remote, 
+              deployment_config.as(DokkuMariadbDeploymentConfig)
+            )
+          when Nil
+            nil
+          end
+
+        next if deployment.nil?
+        deployment.run
+    end
+
     def self.run(args) 
       app = Cli.new
       opts = app.parse_options(args)
@@ -82,31 +118,7 @@ module Pushokku
       # env_config = App.get_config(config, opts["environment"])
 
       config.deployments.each do |deployment_config|
-        local = config.locals.select { |l| l.name == deployment_config.local }.first
-        remote = config.remotes.select { |r| r.name == deployment_config.remote }.first
-        if local.nil?
-          puts "Unknown local #{deployment_config.local}. Exiting."
-          exit 2
-        end
-        if remote.nil?
-          puts "Unknown remote #{deployment_config.remote}. Exiting."
-          exit 2
-        end
-
-
-        deployment = 
-          case deployment_config
-          when DeploymentAppConfig then
-
-            DeploymentApp.new(local.as(LocalDockerConfig), remote, deployment_config.as(DeploymentAppConfig))
-          when DeploymentMariadbConfig then
-            DeploymentMariadb.new(local.as(LocalFileConfig), remote, deployment_config.as(DeploymentMariadbConfig))
-          when Nil
-            nil
-          end
-
-        next if deployment.nil?
-        deployment.run
+        handle_deployment(config, deployment_config)
       end
 
       exit 2