Class: Wip::CLI

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

Overview

Thor-based command-line interface for wip.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dispatch(meth, given_args, given_opts, config) ⇒ Object

Thor only falls back to the default task when no command name is given at all; an unrecognized first argument (e.g. a custom wip.yml command name) would otherwise raise "Could not find command". Route those to dispatch.



21
22
23
24
25
26
27
28
# File 'lib/wip/cli.rb', line 21

def self.dispatch(meth, given_args, given_opts, config)
  name = given_args.first
  if meth.nil? && name && !name.to_s.start_with?('-') && find_command_possibilities(name).empty?
    return super('dispatch', given_args, given_opts, config)
  end

  super
end

.exit_on_failure?Boolean

Returns:

  • (Boolean)


16
# File 'lib/wip/cli.rb', line 16

def self.exit_on_failure? = true

Instance Method Details

#build(*extra) ⇒ Object



53
54
55
56
# File 'lib/wip/cli.rb', line 53

def build(*extra)
  extra.shift if extra.first == '--'
  execute(builder.build(settings: load_config.command('build') || {}, extra: extra))
end

#configObject



48
49
50
# File 'lib/wip/cli.rb', line 48

def config
  puts YAML.dump(load_config.to_h)
end

#dispatch(name = nil, *arguments) ⇒ Object

Raises:



109
110
111
112
113
114
# File 'lib/wip/cli.rb', line 109

def dispatch(name = nil, *arguments)
  raise ConfigError, 'A command is required' unless name

  values = load_config.command(name) || raise(ConfigError, "Unknown command: #{name}")
  execute(builder.custom(name, arguments), interactive: builder.tty?(values.fetch('interactive', false)))
end

#doctorObject



41
42
43
44
45
# File 'lib/wip/cli.rb', line 41

def doctor
  results = Doctor.new(loader: loader).call
  results.each { |item| puts "[#{item.level.to_s.upcase}] #{item.message}" }
  exit(1) if results.any? { |item| item.level == :fail }
end

#downObject



67
68
69
70
71
72
73
74
# File 'lib/wip/cli.rb', line 67

def down
  execute(builder.down, exit_on_failure: false)
  execute(builder.remove, exit_on_failure: false)
  load_config.dependencies.each_key do |name|
    execute(builder.dependency_down(name), exit_on_failure: false)
    execute(builder.dependency_remove(name), exit_on_failure: false)
  end
end

#exec(*command) ⇒ Object



78
79
80
81
# File 'lib/wip/cli.rb', line 78

def exec(*command)
  tty = builder.tty?(options[:interactive])
  execute(builder.exec(command, interactive: options[:interactive]), interactive: tty)
end

#run_command(*command) ⇒ Object



85
86
87
88
# File 'lib/wip/cli.rb', line 85

def run_command(*command)
  tty = builder.tty?(options[:interactive])
  execute(builder.run(command, interactive: options[:interactive]), interactive: tty)
end

#shell_commandObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wip/cli.rb', line 92

def shell_command
  configured = load_config.command('shell')
  if configured
    tty = builder.tty?(configured.fetch('interactive', false))
    return execute(builder.custom('shell', []), interactive: tty)
  end

  tty = builder.tty?(true)
  code = execute(builder.exec(['bash'], settings: { 'interactive' => true }, interactive: true),
                 interactive: tty, exit_on_failure: false)
  return if code.zero?

  execute(builder.exec(['sh'], settings: { 'interactive' => true }, interactive: true), interactive: tty)
end

#upObject



60
61
62
63
64
# File 'lib/wip/cli.rb', line 60

def up
  ensure_network
  load_config.dependencies.each_key { |name| ensure_dependency(name) }
  ensure_container
end

#versionObject



31
32
33
34
35
36
37
38
# File 'lib/wip/cli.rb', line 31

def version
  puts "wip #{VERSION}"
  config = load_config
  command = resolver.resolve(config.wslc_command)
  system(command, 'version')
rescue Error
  nil
end