Class: Nugrant::Vagrant::V2::Command::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/nugrant/vagrant/v2/command/env.rb

Instance Method Summary collapse

Constructor Details

#initialize(arguments, environment) ⇒ Env

Returns a new instance of Env.



10
11
12
13
14
15
16
17
# File 'lib/nugrant/vagrant/v2/command/env.rb', line 10

def initialize(arguments, environment)
  super(arguments, environment)

  @unset = false
  @script = false
  @format = :terminal
  @show_help = false
end

Instance Method Details

#create_parserObject



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
60
# File 'lib/nugrant/vagrant/v2/command/env.rb', line 19

def create_parser()
  return OptionParser.new do |parser|
    parser.banner = "Usage: vagrant user env [<options>]"
    parser.separator ""

    parser.separator "Outputs the commands that should be executed to export\n" +
                     "the various parameter as environment variables. By default,\n" +
                     "existing ones are overridden. The --format argument can be used\n" +
                     "to choose in which format the variables should be displayed.\n" +
                     "Changing the format will also change where they are displayed.\n"
    parser.separator ""
    parser.separator "The `-s, --script` option is deprecated and will be removed in\n" +
                     "version 2.0. Use `--format script` instead."
    parser.separator ""

    parser.separator "Available formats:"
    parser.separator "  autoenv  => Write commands to a file named `.env` in the current directory.\n" +
                     "               See https://github.com/kennethreitz/autoenv for more info."
    parser.separator "  terminal => Display commands to terminal so they can be sourced."
    parser.separator "  script   => Write commands to a bash script named `nugrant2env.sh` so it can be sourced."
    parser.separator ""

    parser.separator "Available options:"
    parser.separator ""

    parser.on("-u", "--[no-]unset", "Generates commands needed to unset environment variables, default false") do |unset|
      @unset = unset
    end

    parser.on("-s", "--[no-]script", "Generates a bash script instead of simply showing command, default false") do |script|
       @script = script
    end

    parser.on("-f", "--format FORMAT", "Determines in what format variables are outputted, default to terminal") do |format|
       @format = format.to_sym()
    end

    parser.on("-h", "--help", "Print this help") do
      @show_help = true
    end
  end
end

#error(message, parser) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/nugrant/vagrant/v2/command/env.rb', line 62

def error(message, parser)
  @env.ui.info("ERROR: #{message}", :prefix => false)
  @env.ui.info("", :prefix => false)

  help(parser)

  return 1
end

#executeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nugrant/vagrant/v2/command/env.rb', line 75

def execute
  parser = create_parser()
  arguments = parse_options(parser)

  return error("Invalid format value '#{@format}'", parser) if not Helper::Env::Exporter.valid?(@format)
  return help(parser) if @show_help

  @logger.debug("Nugrant 'Env'")
  with_target_vms(arguments) do |vm|
    config = vm.config.user
    parameters = config ? config.parameters : Nugrant::Parameters.new()
    bag = parameters.__all

    options = {:type => @unset ? :unset : :export}

    case
    when @script || @format == :script
      Helper::Env::Exporter.script_exporter(bag, options)
    when @format == :autoenv
      Helper::Env::Exporter.autoenv_exporter(bag, options)
    else
      Helper::Env::Exporter.terminal_exporter(bag, options)
    end

    # No need to execute for the other VMs
    return 0
  end
end

#help(parser) ⇒ Object



71
72
73
# File 'lib/nugrant/vagrant/v2/command/env.rb', line 71

def help(parser)
  @env.ui.info(parser.help, :prefix => false)
end