#!/usr/bin/env ruby require "thor" module DokkuInstaller class Cli < Thor desc "config", "Display the app's environment variables" def config run_command "config #{app_name}" end desc "config:get KEY", "Display an environment variable value" def config_get(*args) run_command "config:get #{app_name} #{args.first}" end 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 desc "config:unset KEY1 [KEY2 ...]", "Unset one or more environment variables" def config_unset(*args) run_command "config:unset #{app_name} #{args.join(' ')}" end # 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 desc "domains:set DOMAIN1 [DOMAIN2 ...]", "Set one or more domains" def domains_set(*args) run_command "domains:set #{app_name} #{args.join(' ')}" end desc "logs [-t]", "Show the last logs for the application (-t follows)" def logs(*args) run_command "logs #{app_name} #{args.join(' ')}" end desc "run ", "Run a command in the environment of an application" def walk(*args) run_command "run #{app_name} #{args.join(' ')}" end desc "version", "Print dokku-installer-cli's version" def version puts "Dokku Installer CLI #{DokkuInstaller::VERSION}" end def help(method = nil) method = "walk" if method == "run" super end 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)