Class: Dru::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dru/command.rb

Defined Under Namespace

Classes: MissingContainerError

Constant Summary collapse

DOCKER_COMPOSE_COMMAND =
'docker-compose'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/dru/command.rb', line 17

def options
  @options
end

Instance Method Details

#command(**options) ⇒ Object

The external commands runner



36
37
38
39
# File 'lib/dru/command.rb', line 36

def command(**options)
  require 'tty-command'
  TTY::Command.new({ printer: :quiet, uuid: false }.merge(options))
end

#container_name_to_id(container_name = 'app') ⇒ Object



165
166
167
# File 'lib/dru/command.rb', line 165

def container_name_to_id(container_name = 'app')
  run_docker_compose_command('ps', '-q', container_name, printer: :null).first
end

#cursorObject

The cursor movement



46
47
48
49
# File 'lib/dru/command.rb', line 46

def cursor
  require 'tty-cursor'
  TTY::Cursor
end

#default_docker_composeObject



143
144
145
# File 'lib/dru/command.rb', line 143

def default_docker_compose
  File.join(project_configuration_path, 'docker-compose.yml')
end

#docker_compose_pathsObject



153
154
155
# File 'lib/dru/command.rb', line 153

def docker_compose_paths
  docker_compose_default_path + docker_compose_environment_path
end

#editorObject

Open a file or text in the user’s preferred editor



56
57
58
59
# File 'lib/dru/command.rb', line 56

def editor
  require 'tty-editor'
  TTY::Editor
end

#environmentObject



135
136
137
# File 'lib/dru/command.rb', line 135

def environment
  options && options[:environment]
end

#environment_docker_composeObject



147
148
149
150
151
# File 'lib/dru/command.rb', line 147

def environment_docker_compose
  return unless environment

  File.join(project_configuration_path, "docker-compose.#{environment}.yml")
end

#exec_exist?(*args) ⇒ Boolean

Check if executable exists

Returns:

  • (Boolean)

See Also:



126
127
128
129
# File 'lib/dru/command.rb', line 126

def exec_exist?(*args)
  require 'tty-which'
  TTY::Which.exist?(*args)
end

#executeObject

Execute this command

Raises:

  • (NotImplementedError)


24
25
26
27
28
29
# File 'lib/dru/command.rb', line 24

def execute(*)
  raise(
    NotImplementedError,
    "#{self.class}##{__method__} must be implemented"
  )
end

#generatorObject

File manipulation utility methods



66
67
68
69
# File 'lib/dru/command.rb', line 66

def generator
  require 'tty-file'
  TTY::File
end

#pager(**options) ⇒ Object

Terminal output paging



76
77
78
79
# File 'lib/dru/command.rb', line 76

def pager(**options)
  require 'tty-pager'
  TTY::Pager.new(options)
end

#platformObject

Terminal platform and OS properties



86
87
88
89
# File 'lib/dru/command.rb', line 86

def platform
  require 'tty-platform'
  TTY::Platform.new
end

#project_configuration_pathObject



139
140
141
# File 'lib/dru/command.rb', line 139

def project_configuration_path
  File.expand_path(project_name, Dru.config.docker_projects_folder)
end

#project_nameObject



131
132
133
# File 'lib/dru/command.rb', line 131

def project_name
  File.basename(Dir.pwd)
end

#prompt(**options) ⇒ Object

The interactive prompt



96
97
98
99
# File 'lib/dru/command.rb', line 96

def prompt(**options)
  require 'tty-prompt'
  TTY::Prompt.new(options)
end

#run_docker_compose_command(*args, **options) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/dru/command.rb', line 157

def run_docker_compose_command(*args, **options)
  if options[:tty]
    system(DOCKER_COMPOSE_COMMAND, *docker_compose_paths, *args)
  else
    command(options).run(DOCKER_COMPOSE_COMMAND, *docker_compose_paths, *args)
  end
end

#screenObject

Get terminal screen properties



106
107
108
109
# File 'lib/dru/command.rb', line 106

def screen
  require 'tty-screen'
  TTY::Screen
end

#which(*args) ⇒ Object

The unix which utility



116
117
118
119
# File 'lib/dru/command.rb', line 116

def which(*args)
  require 'tty-which'
  TTY::Which.which(*args)
end