Class: JadeSystemsToolbox::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/jade_systems_toolbox/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


9
# File 'lib/jade_systems_toolbox/cli.rb', line 9

def exit_on_failure? = true

Instance Method Details

#downObject



18
19
20
# File 'lib/jade_systems_toolbox/cli.rb', line 18

def down
  system("docker compose down")
end

#editObject



23
24
25
# File 'lib/jade_systems_toolbox/cli.rb', line 23

def edit
  `devcontainer open`
end

#initObject



32
33
34
35
# File 'lib/jade_systems_toolbox/cli.rb', line 32

def init
  invoke :initialize_docker
  invoke :initialize_vscode, [], {}
end

#initialize_dockerObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jade_systems_toolbox/cli.rb', line 42

def initialize_docker
  get_and_save_file(
    "https://github.com/lcreid/docker/raw/refs/heads/main/rails-app-sqlite/compose.yml",
  ) do |file_contents|
    file_contents.gsub!(
      /jade:rails-app-[0-9]+\.[0-9]+-\w+-\w+$/,
      "jade:rails-app-#{options[:ruby_version] || "3.4"}" \
        "-#{options[:database] || "sqlite"}" \
        "-#{options[:distro_version] || bookworm}",
    )
  end

  case Gem::Platform.local.os
  when "linux"
    get_and_save_file("https://github.com/lcreid/docker/raw/refs/heads/main/Linux/compose.override.yml")
  else
    raise Error.new("Not implemented for #{Gem::Platform.local.os}", options[:verbose])
  end
end

#initialize_vscodeObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jade_systems_toolbox/cli.rb', line 63

def initialize_vscode
  get_and_save_file("https://github.com/lcreid/docker/raw/refs/heads/main/.devcontainer.json") do |file_contents|
    file_contents.gsub!(/, "compose.override.yml"/, "") unless Gem::Platform.local.os == "linux"
  end

  Dir.mkdir(".vscode") unless Dir.exist?(".vscode")
  [
    "https://github.com/lcreid/jade_systems_toolbox/raw/refs/heads/main/templates/extensions.json",
    "https://github.com/lcreid/jade_systems_toolbox/raw/refs/heads/main/templates/settings.json",
  ].each { get_and_save_file(_1, target_directory: File.join(".", ".vscode")) }
end

#openObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jade_systems_toolbox/cli.rb', line 80

def open
  service = options[:service]
  path = options[:path]
  container_port = options[:container_port]
  protocol = options[:protocol]

  container_ports = compose_yaml.dig("services", "service", "ports")
  container_port = container_ports&.[](0) || container_port
  path = "/#{path}" if path[0] != "/"
  `open "#{protocol}://localhost:#{host_port_from_container_port(service:, container_port:)}#{path}"`
end

#port(container_port = 3000) ⇒ Object



94
95
96
97
# File 'lib/jade_systems_toolbox/cli.rb', line 94

def port(container_port = 3000)
  service = options[:service]
  puts host_port_from_container_port(service:, container_port:)
end

#portsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/jade_systems_toolbox/cli.rb', line 101

def ports
  services = compose_yaml["services"]
  service_ports = services.transform_values { |attributes| attributes&.[]("ports") }

  service_ports.each do |service, ports|
    ports&.each do |container_port|
      puts "#{service}: #{host_port_from_container_port(service:, container_port:)}"
    end
  end
end

#server(command = "bin/dev") ⇒ Object



115
116
117
118
119
# File 'lib/jade_systems_toolbox/cli.rb', line 115

def server(command = "bin/dev")
  service = options[:service]
  workdir = "-w #{options[:work_dir]} " unless options[:work_dir].nil?
  system("docker compose exec #{workdir}#{service} #{command}")
end

#terminalObject



123
124
125
126
# File 'lib/jade_systems_toolbox/cli.rb', line 123

def terminal
  service = options[:service]
  system("docker compose exec -it #{service} '/bin/bash'")
end

#upObject



129
130
131
# File 'lib/jade_systems_toolbox/cli.rb', line 129

def up
  system("docker compose up -d")
end

#versionObject



134
135
136
# File 'lib/jade_systems_toolbox/cli.rb', line 134

def version
  puts JadeSystemsToolbox::VERSION
end