Class: Renuo::Cli::Commands::Debug

Inherits:
Object
  • Object
show all
Defined in:
lib/renuo/cli/commands/debug.rb

Constant Summary collapse

Cache =
Renuo::Cli::Services::Cache
Deploio =
Renuo::Cli::Services::Deploio
Heroku =
Renuo::Cli::Services::Heroku

Instance Method Summary collapse

Instance Method Details

#run(args, options) ⇒ Object

rubocop:todo Metrics/PerceivedComplexity rubocop:todo Metrics/MethodLength rubocop:todo Metrics/AbcSize



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/renuo/cli/commands/debug.rb', line 32

def run(args, options) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
  query = args[0]
  abort(">> Please provide an app name or domain.") if query.blank?

  cloud_unspecified = !options.heroku && !options.deploio
  should_scan_heroku = options.heroku || cloud_unspecified
  should_scan_deploio = options.deploio || cloud_unspecified

  choose do |menu| # rubocop:todo Metrics/BlockLength
    deploio_cache_info = "Deploio: #{Cache.stored_at("deploio_apps") || "never"}"
    heroku_cache_info = "Heroku: #{Cache.stored_at("heroku_apps") || "never"}"

    menu.choice "Update caches (#{deploio_cache_info}, #{heroku_cache_info})" do
      if should_scan_deploio
        say "Updating Deploio cache"
        Cache.store("deploio_apps", Deploio.fetch_apps)
      end
      if should_scan_heroku
        say "Updating Heroku cache..."
        Cache.store("heroku_apps", Heroku.fetch_apps)
      end
      say "Caches updated"
    end

    open_cmds = []

    if should_scan_deploio && Cache.stored_at("deploio_apps")
      select_deploio_targets(query).each do |app|
        bash_cmd = "nctl exec app #{app[:name]} bash --project #{app[:namespace]}"
        menu.choice(bash_cmd) { exec bash_cmd.squish }

        rails_console_cmd = "nctl exec app #{app[:name]} bundle exec rails console --project #{app[:namespace]}"
        menu.choice(rails_console_cmd) { exec rails_console_cmd.squish }

        log_cmd = "nctl logs app #{app[:name]} -f   --project #{app[:namespace]}"
        menu.choice(log_cmd) { exec log_cmd.squish }

        app[:hosts].each do |host|
          open_cmds << "open https://#{host}"
        end
      end
    end

    if should_scan_heroku && Cache.stored_at("heroku_apps")
      select_heroku_targets(query).each do |app|
        exec_cmd = "heroku run bash --app #{app[:name]}"
        menu.choice(exec_cmd) { exec exec_cmd.squish }

        rails_console_cmd = "heroku run bundle exec rails console --app #{app[:name]}"
        menu.choice(rails_console_cmd) { exec rails_console_cmd.squish }

        log_cmd = "heroku logs -t --app #{app[:name]}"
        menu.choice(log_cmd)  { exec log_cmd.squish }

        app[:domains].each do |host|
          open_cmds << "open https://#{host}"
        end
      end
    end

    open_cmds.uniq.each do |cmd|
      menu.choice(cmd) { exec cmd.squish }
    end
  end
end