Module: Tainers::CLI

Defined in:
lib/tainers/cli.rb

Defined Under Namespace

Classes: Command

Class Method Summary collapse

Class Method Details

.from_file(file) ⇒ Object



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

def self.from_file file
  Proc.new do
    File.open(file, "r") do |f|
      JSON.parse(f.read)
    end
  end
end

.from_json(json) ⇒ Object



75
76
77
78
79
# File 'lib/tainers/cli.rb', line 75

def self.from_json json
  Proc.new do
    JSON.parse json
  end
end

.from_stdinObject



61
62
63
64
65
# File 'lib/tainers/cli.rb', line 61

def self.from_stdin
  Proc.new do
    JSON.parse(STDIN.read)
  end
end

.parse(parameters) ⇒ Object



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tainers/cli.rb', line 13

def self.parse parameters
  options = {}
  options[:spec_source] = from_stdin
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: tainers [opts] COMMAND"
    opts.separator ""
    opts.separator "Manipulate Tainer-managed containers, taking their specification in JSON (from STDIN by default)."
    opts.separator ""
    opts.separator "Specific options:"

    opts.on('-j JSON', '--json JSON', String, "Take container specification from the given JSON parameter.") do |j|
      options[:spec_source] = from_json(j)
    end

    opts.on('-f FILEPATH', '--file FILEPATH', String, "Take container specification from JSON in the given file.") do |f|
      options[:spec_source] = from_file(f)
    end

    opts.on('-p PREFIX', '--prefix PREFIX', String, "Use PREFIX as container name prefix (overriding whatever is in spec)") do |p|
      options['prefix'] = p
    end

    opts.on('-s SUFFIX', '--suffix SUFFIX', String, "Use SUFFIX as container name suffix (overriding whatever is in spec)") do |s|
      options['suffix'] = s
    end

    opts.on('-h', '--help') do
      print opts
      exit 0
    end

    opts.separator ""
    opts.separator "Commands:"
    Command.commands.each do |name, help|
      opts.separator ""
      opts.separator "    #{name}"
      opts.separator "          #{help}" if help.size > 0
    end
  end

  # Stop on first non-option param
  non_param = []
  args = opt_parser.order(parameters) {|p| non_param << p; opt_parser.terminate}
  spec = options.delete(:spec_source).call
  spec.update(options)
  [spec, non_param + args]
end

.run(parameters) ⇒ Object



6
7
8
9
10
11
# File 'lib/tainers/cli.rb', line 6

def self.run parameters
  spec, parameters = parse(parameters)
  cmd = Command.new(spec)
  cmd_name = parameters.shift
  cmd.send("#{cmd_name}_command".to_sym, *parameters)
end