Class: HighCarb::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/highcarb/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



16
17
18
19
20
# File 'lib/highcarb/command.rb', line 16

def initialize
  @command_line = @args = []
  @options = {}
  @logger = Logger.new(STDERR).tap {|logger| logger.level = Logger::WARN }
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



14
15
16
# File 'lib/highcarb/command.rb', line 14

def args
  @args
end

#command_lineObject (readonly)

Returns the value of attribute command_line.



13
14
15
# File 'lib/highcarb/command.rb', line 13

def command_line
  @command_line
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/highcarb/command.rb', line 12

def options
  @options
end

Instance Method Details

#parse!(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/highcarb/command.rb', line 22

def parse!(args)
  @command_line = args.dup
  @args = args
  @options = Trollop.options(@args) do
    opt "generate", "Generate a new highcarb project"
    opt "server", "Start the servers (default action). See --http-port and --ws-port"

    opt "http-port", "HTTP server port", default: 9090

    opt "skip-libs", "Don't download vendor libraries, like Deck.js and jQuery"

    opt "verbose", "Be verbose"

    opt "auth", "Require auth to access", default: ""
  end

  if @options["verbose"]
    @logger.level = Logger::DEBUG
  end

  self
end

#run!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/highcarb/command.rb', line 45

def run!
  if args.size != 1
    STDERR.puts "Please indicate the project path as an extra argument of the command. For example:"
    STDERR.puts "$ \033[1m#$0 #{command_line * " "} project-path/\033[m"
    exit 1
  end

  if options["generate"]
    # Generate a new project
    HighCarb::Generator.new(self, args.first).run!
  else
    auth = nil
    if not options["auth"].empty?
      user, password = options["auth"].split(":", 2)
      if password.nil?
        print "Type the password for #{user}: "
        $stdout.flush
        password = $stdin.noecho { $stdin.readline.chomp }
      end

      auth = [ user, password ]
    end

    HighCarb::Services.start!(self, @logger, auth)
  end

rescue HighCarb::Error => error
  STDERR.puts "ERROR: " + error.message
end