bin/dokku in dokku-installer-cli-0.0.2 vs bin/dokku in dokku-installer-cli-0.0.3

- old
+ new

@@ -1,76 +1,105 @@ #!/usr/bin/env ruby -require "optparse" -require "methadone" -require "dokku_installer_cli.rb" +require "thor" -class App - include Methadone::Main - include Methadone::CLILogging +module DokkuInstaller + class Cli < Thor - main do |command, *args| - git_config = File.join(Dir.pwd, ".git", "config") - exit unless File.exist?(git_config) + desc "config", "Display the app's environment variables" + def config + run_command "config #{app_name}" + end - git_config = File.read(git_config) - match = git_config.match(/url \= dokku@(.*):(.*)\n/).to_a - exit unless match + desc "config:get KEY", "Display an environment variable value" + def config_get(*args) + run_command "config:get #{app_name} #{args.first}" + end - domain = match[1] - app_name = match[2] + desc "config:set KEY1=VALUE1 [KEY2=VALUE2 ...]", "Set one or more environment variables" + def config_set(*args) + run_command "config:set #{app_name} #{args.join(' ')}" + end - dokku_command = "ssh -t dokku@#{domain} " + desc "config:unset KEY1 [KEY2 ...]", "Unset one or more environment variables" + def config_unset(*args) + run_command "config:unset #{app_name} #{args.join(' ')}" + end - case command - when "config" - dokku_command += "config #{app_name}" - when "config:get" - dokku_command += "config:get #{app_name} #{args.first}" - when "config:set" - dokku_command += "config:set #{app_name} #{args.join(' ')}" - when "config:unset" - dokku_command += "config:unset #{app_name} #{args.join(' ')}" - when "create" - dokku_command += "create #{args.first}" - when "domains" - dokku_command += "domains #{app_name}" - when "domains:set" - dokku_command += "domains:set #{app_name} #{args.join(' ')}" - when "logs" - dokku_command += "logs #{app_name} #{args.join(' ')}" - when "run" - command = args.join(" ") - dokku_command += "run #{app_name} #{command}" - else - exit + # desc "create", "Create an app" + # def create(*args) + # run_command "create #{args.first}" + # end + + desc "domains", "Display the app's domains" + def domains(*args) + run_command "domains #{app_name}" end - puts "Running #{dokku_command}..." - exec(dokku_command) - end + desc "domains:set DOMAIN1 [DOMAIN2 ...]", "Set one or more domains" + def domains_set(*args) + run_command "domains:set #{app_name} #{args.join(' ')}" + end - description "Command line tool for Dokku Installer" + desc "logs [-t]", "Show the last logs for the application (-t follows)" + def logs(*args) + run_command "logs #{app_name} #{args.join(' ')}" + end - arg :command - # - # Accept flags via: - # on("--flag VAL","Some flag") - # options[flag] will contain VAL - # - # Specify switches via: - # on("--[no-]switch","Some switch") - # - # Or, just call OptionParser methods on opts - # - # Require an argument - # arg :some_arg - # - # # Make an argument optional - # arg :optional_arg, :optional + desc "run <cmd>", "Run a command in the environment of an application" + def walk(*args) + run_command "run #{app_name} #{args.join(' ')}" + end - version DokkuInstallerCli::VERSION + desc "version", "Print dokku-installer-cli's version" + def version + puts "Dokku Installer CLI #{DokkuInstaller::VERSION}" + end - use_log_level_option :toggle_debug_on_signal => "USR1" + def help(method = nil) + method = "walk" if method == "run" + super + end - go! + def method_missing(method, *args, &block) + if method.to_s.split(":").length == 2 + self.send(method.to_s.gsub(":", "_"), *args) + elsif method == :run + self.walk(*args) + else + super + end + end + + private + + def app_name + @app_name ||= git_config_match[2] + end + + def domain + @domain ||= git_config_match[1] + end + + def git_config_match + @git_config_match ||= begin + git_config = File.join(Dir.pwd, ".git", "config") + exit unless File.exist?(git_config) + + git_config = File.read(git_config) + match = git_config.match(/url \= dokku@(.*):(.*)\n/).to_a + exit unless match + + match + end + end + + def run_command(command) + dokku_command = "ssh -t dokku@#{domain} #{command}" + + puts "Running #{dokku_command}..." + exec(dokku_command) + end + end end + +DokkuInstaller::Cli.start(ARGV)