Class: Envo::Cli::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/envo/cli/runner.rb

Defined Under Namespace

Modules: Opts

Constant Summary collapse

VERSION_TEXT =
"envo v#{Envo::VERSION} #{Envo::VERSION_TYPE}"
USAGE =
"usage: envo [--version] [--help] <command> [<args>]\n"
Commands =
[
  CmdShow,
  CmdSet,
  CmdReset,
  CmdUnset,
  CmdList,
  CmdListAdd,
  CmdListDel,
  CmdClean,
  CmdCopy,
  CmdSwap,
  CmdPath,
  CmdRun,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, payload) ⇒ Runner



4
5
6
7
# File 'lib/envo/cli/runner.rb', line 4

def initialize(host, payload)
  @host = host
  @payload = payload
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/envo/cli/runner.rb', line 8

def host
  @host
end

#payloadObject (readonly)

Returns the value of attribute payload.



8
9
10
# File 'lib/envo/cli/runner.rb', line 8

def payload
  @payload
end

Instance Method Details

#check_help_ver(argv) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/envo/cli/runner.rb', line 75

def check_help_ver(argv)
  raise Error.new USAGE if argv.empty?
  case argv[0]
  when '--help', '-h', '-?'
    print_help
    true
  when '--version'
    print_version
    true
  else
    false
  end
end

#do_run(argv) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/envo/cli/runner.rb', line 89

def do_run(argv)
  return if check_help_ver(argv)

  parser = CliParser.new(Opts)
  Commands.each { |cmd| cmd.register_cli_parser(parser) }
  parsed = parser.parse(argv)

  ctx = Context.new(@host, @log, Opts::Defaults)
  ctx.execute(parsed)

  patch = ctx.state.diff

  return if patch.empty?

  patch.removed.each { |name|
    @payload.puts @host.shell.cmd_unset_env_var(name)
    puts @host.shell.cmd_unset_env_var(name)
  }
  patch.changed.each { |name, val|
    @payload.puts @host.shell.cmd_set_env_var(name, val)
    puts @host.shell.cmd_set_env_var(name, val)
  }
  patch.added.each { |name, val|
    @payload.puts @host.shell.cmd_set_env_var(name, val)
    puts @host.shell.cmd_set_env_var(name, val)
  }
end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/envo/cli/runner.rb', line 60

def print_help
  print_version
  @log.puts USAGE
  @log.puts ''
  @log.puts 'Commands:'
  help = Help.new
  Commands.each do |cmd|
    cmd.register_help(help)
  end
  help.print(@log)
  @log.puts ''
  @log.puts 'Common options:'
  Opts.print_help(@log)
end


56
57
58
# File 'lib/envo/cli/runner.rb', line 56

def print_version
  @log.puts VERSION_TEXT
end

#run(argv) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/envo/cli/runner.rb', line 117

def run(argv)
  @log = Logger.new
  begin
    do_run(argv)
    return 0
  rescue Error => e
    @log.error e.message
    return 1
  end
end