Class: RailsDocks::CLI
- Inherits:
-
Object
- Object
- RailsDocks::CLI
- Defined in:
- lib/railsdocks/cli.rb
Class Method Summary collapse
- .apps_destroy(arguments) ⇒ Object
- .apps_list ⇒ Object
- .create(arguments) ⇒ Object
- .database_command(operation) ⇒ Object
- .domains_add(arguments) ⇒ Object
- .domains_list ⇒ Object
- .domains_remove(arguments) ⇒ Object
- .login ⇒ Object
- .setup ⇒ Object
- .start(arguments) ⇒ Object
Class Method Details
.apps_destroy(arguments) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/railsdocks/cli.rb', line 281 def self.apps_destroy(arguments) slug = arguments.shift.to_s.strip if slug.empty? || !arguments.empty? warn( "Usage: railsdocks apps:destroy APP_NAME" ) return 1 end unless Config.logged_in? warn "You are not logged in." warn "Run `railsdocks login` first." return 1 end puts( "Destroy #{slug}? This cannot be undone." ) print "Type #{slug} to confirm: " confirmation = $stdin.gets&.chomp.to_s unless confirmation == slug puts "Cancelled." return 1 end response = Client.new.destroy_app( slug: slug, confirm: confirmation, token: Config.token ) app = response.fetch("app") destroyed_slug = app.fetch("slug") unless app.fetch("destroyed") == true raise KeyError, "destroyed" end puts "Destroyed #{destroyed_slug}." 0 rescue Client::Error => e warn "Destroy failed: #{e.}" 1 rescue KeyError warn( "Destroy failed: RailsDocks returned " "an unexpected response." ) 1 end |
.apps_list ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/railsdocks/cli.rb', line 210 def self.apps_list unless Config.logged_in? warn "You are not logged in." warn "Run `railsdocks login` first." return 1 end response = Client.new.apps( token: Config.token ) apps = response.fetch("apps") if apps.empty? puts "No applications." return 0 end name_width = [ "Name".length, *apps.map { |app| app.fetch("name").length } ].max hostname_width = [ "Hostname".length, *apps.map { |app| app.fetch("hostname").length } ].max printf( "%-#{name_width}s %-#{hostname_width}s %s\n", "Name", "Hostname", "Database" ) apps.each do |app| database = app.fetch("database_enabled") ? "PostgreSQL" : "None" printf( "%-#{name_width}s %-#{hostname_width}s %s\n", app.fetch("name"), app.fetch("hostname"), database ) end 0 rescue Client::Error => e warn "Could not list applications: #{e.}" 1 rescue KeyError warn( "Could not list applications: RailsDocks returned " \ "an unexpected response." ) 1 end |
.create(arguments) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/railsdocks/cli.rb', line 113 def self.create(arguments) name = nil no_db = false until arguments.empty? argument = arguments.shift case argument when "--no-db", "-no-db" no_db = true else if argument.start_with?("-") warn "Unknown option: #{argument}" return 1 end if name warn "Unexpected argument: #{argument}" warn( "Usage: railsdocks apps:create APP_NAME [--no-db]" ) return 1 end name = argument end end if name.to_s.strip.empty? warn( "Usage: railsdocks apps:create APP_NAME [--no-db]" ) return 1 end unless Config.logged_in? warn "You are not logged in." warn "Run `railsdocks login` first." return 1 end SSHConfig.configure response = Client.new.create_app( name: name, token: Config.token, no_db: no_db ) app = response.fetch("app") git_url = app.fetch("git_url") puts "Created #{app.fetch("slug")}." puts( "Hostname: https://" \ "#{app.fetch("hostname")}" ) if no_db puts "Database: disabled" else puts "Database: PostgreSQL" end if inside_git_repository? add_git_remote(git_url) else puts puts "Not currently inside a Git repository." puts "Add the RailsDocks remote when ready:" puts puts( " git remote add railsdocks #{git_url}" ) end 0 rescue Client::Error => e warn "Create failed: #{e.}" 1 rescue SSHConfig::Error => e warn "Create failed: #{e.}" 1 rescue KeyError warn( "Create failed: RailsDocks returned " \ "an unexpected response." ) 1 end |
.database_command(operation) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/railsdocks/cli.rb', line 345 def self.database_command(operation) unless Config.logged_in? warn "You are not logged in." warn "Run `railsdocks login` first." return 1 end slug = current_app_slug unless slug warn( "Could not determine the RailsDocks app." ) warn( "Run this command from a Git repository " \ "with a `railsdocks` remote." ) return 1 end display_name = operation == "migrate" ? "Migrating" : "Rolling back" puts "#{display_name} #{slug}..." response = Client.new.database_command( slug: slug, operation: operation, token: Config.token ) output = response["output"].to_s.strip puts output unless output.empty? puts( operation == "migrate" ? "Migration complete." : "Rollback complete." ) 0 rescue Client::Error => e warn( "Database command failed: #{e.}" ) 1 end |
.domains_add(arguments) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/railsdocks/cli.rb', line 468 def self.domains_add(arguments) unless Config.logged_in? warn "You are not logged in." warn "Run `railsdocks login` first." return 1 end hostname = arguments.shift.to_s.strip if hostname.empty? warn( "Usage: railsdocks domains:add DOMAIN " \ "[--app APP_NAME]" ) return 1 end app_slug = nil until arguments.empty? argument = arguments.shift case argument when "--app" app_slug = arguments.shift.to_s.strip if app_slug.empty? warn "--app requires an application name." return 1 end else warn "Unknown option: #{argument}" return 1 end end app_slug ||= current_app_slug unless app_slug warn( "Could not determine the RailsDocks app." ) warn( "Use --app APP_NAME or run this command " \ "from a RailsDocks Git repository." ) return 1 end response = Client.new.add_domain( hostname: hostname, app: app_slug, token: Config.token ) domain = response.fetch("domain") dns = response.fetch("dns") puts( "Added #{domain.fetch("hostname")} " \ "to #{domain.fetch("app")}." ) puts puts "DNS configuration:" puts( " #{dns.fetch("type")} " \ "#{dns.fetch("name")} " \ "#{dns.fetch("target")}" ) puts puts( "Status: #{domain.fetch("status")}" ) 0 rescue Client::Error => e warn "Could not add domain: #{e.}" 1 rescue KeyError warn( "Could not add domain: RailsDocks returned " \ "an unexpected response." ) 1 end |
.domains_list ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/railsdocks/cli.rb', line 402 def self.domains_list unless Config.logged_in? warn "You are not logged in." warn "Run `railsdocks login` first." return 1 end response = Client.new.domains( token: Config.token ) domains = response.fetch("domains") if domains.empty? puts "No custom domains." return 0 end hostname_width = [ "Domain".length, *domains.map { |domain| domain.fetch("hostname").length } ].max app_width = [ "App".length, *domains.map { |domain| domain.fetch("app").length } ].max printf( "%-#{hostname_width}s %-#{app_width}s %s\n", "Domain", "App", "Status" ) domains.each do |domain| printf( "%-#{hostname_width}s %-#{app_width}s %s\n", domain.fetch("hostname"), domain.fetch("app"), domain.fetch("status") ) end 0 rescue Client::Error => e warn "Could not list domains: #{e.}" 1 rescue KeyError warn( "Could not list domains: RailsDocks returned " \ "an unexpected response." ) 1 end |
.domains_remove(arguments) ⇒ Object
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'lib/railsdocks/cli.rb', line 569 def self.domains_remove(arguments) unless Config.logged_in? warn "You are not logged in." warn "Run `railsdocks login` first." return 1 end hostname = arguments.shift.to_s.strip if hostname.empty? || !arguments.empty? warn( "Usage: railsdocks domains:remove DOMAIN" ) return 1 end response = Client.new.remove_domain( hostname: hostname, token: Config.token ) domain = response.fetch("domain") puts( "Removed #{domain.fetch("hostname")} " \ "from #{domain.fetch("app")}." ) 0 rescue Client::Error => e warn "Could not remove domain: #{e.}" 1 rescue KeyError warn( "Could not remove domain: RailsDocks returned " \ "an unexpected response." ) 1 end |
.login ⇒ Object
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 97 |
# File 'lib/railsdocks/cli.rb', line 44 def self.login print "Email: " email = $stdin.gets&.chomp.to_s print "Password: " password = $stdin.noecho(&:gets)&.chomp.to_s puts response = Client.new.login( email: email, password: password ) Config.save_login( email: response .fetch("user") .fetch("email"), token: response.fetch("token") ) SSHConfig.configure puts( "Logged in as " \ "#{response.fetch("user").fetch("email")}." ) puts "RailsDocks SSH configured." 0 rescue Client::Error => e warn "Login failed: #{e.}" 1 rescue SSHConfig::Error => e warn( "Login succeeded, but SSH setup failed: " \ "#{e.}" ) 1 rescue KeyError warn( "Login failed: RailsDocks returned " \ "an unexpected response." ) 1 end |
.setup ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/railsdocks/cli.rb', line 99 def self.setup SSHConfig.configure puts "RailsDocks SSH configured." puts "You can now deploy with:" puts puts " git push railsdocks main" 0 rescue SSHConfig::Error => e warn "Setup failed: #{e.}" 1 end |
.start(arguments) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/railsdocks/cli.rb', line 8 def self.start(arguments) command = arguments.shift case command when "login" login when "setup" setup when "apps:create" create(arguments) when "apps:list" apps_list when "apps:destroy" apps_destroy(arguments) when "db:migrate" database_command("migrate") when "db:rollback" database_command("rollback") when "domains:list" domains_list when "domains:add" domains_add(arguments) when "domains:remove" domains_remove(arguments) when "version", "--version", "-v" puts "RailsDocks #{RailsDocks::VERSION}" 0 when nil, "help", "--help", "-h" help else warn "Unknown command: #{command}" warn "Run `railsdocks help` for available commands." 1 end end |