Class: Jarbs::CLI

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/jarbs.rb

Constant Summary collapse

GLOBAL_DEFAULTS =
{ env: 'dev' }

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



19
20
21
22
23
24
25
26
27
28
# File 'lib/jarbs.rb', line 19

def initialize
  @config = Config.new

  if @config.get('crashes.report')
    CrashReporter.configure do |c|
      c.engines = [CrashReporter::GithubIssues.new('articulate/jarbs', @config.get('github.token'))]
      c.version = Jarbs::VERSION
    end
  end
end

Instance Method Details

#runObject



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
103
104
# File 'lib/jarbs.rb', line 30

def run
  program :version, Jarbs::VERSION
  program :description, 'Lambda Tooling'

  global_option('-e', '--env ENV', String, 'Set deployment environment [Default to dev]')
  global_option('-d', '--debug', 'Enable debug mode') { $debug = true }

  command :new do |c|
    c.syntax = 'jarbs new [options] name'
    c.summary = "Generate a new lambda function or project skeleton"
    c.option "-f", "--force", "Force overwrite of existing function definition"
    c.action do |args, options|
      name = args.shift || abort("Must provide a lambda name")
      options.default GLOBAL_DEFAULTS

      lambda = Lambda.new(name, options)

      project_exists?(name, remove: options.force)
      lambda_exists?(lambda, remove: options.force)

      ProjectGenerator.new(name).generate unless jarbs_project?
      lambda.generate
    end
  end

  command :deploy do |c|
    c.syntax = 'jarbs deploy [options] directory'
    c.summary = 'Deploy a lambda function to AWS'
    c.option "--role [STRING]", String, "IAM role for Lambda execution"
    c.action do |args, options|
      name = args.shift || abort('Name argument required')
      options.default GLOBAL_DEFAULTS

      lambda = Lambda.new(name, options)

      abort("Lambda '#{name}' does not exist.") unless lambda.exists?

      lambda.deploy
    end
  end

  command :rm do |c|
    c.syntax = 'jarbs rm NAME [NAME...]'
    c.summary = "Delete a lambda function"
    c.action do |args, options|
      abort('Name argument required') if args.empty?
      options.default GLOBAL_DEFAULTS

      begin
        args.each do |fn|
          begin
            Lambda.new(fn, options).delete
          rescue Aws::Lambda::Errors::ResourceNotFoundException => e
            say_error "Function \"#{fn}\" does not exists. Ignoring."
            next
          end
        end
      end
    end
  end

  command :ls do |c|
    c.syntax = 'jarbs ls'
    c.summary = "List lambda functions in this project"
    c.action do |args, options|
      lambdas = Dir.glob("lambdas/*").map {|x| Lambda.new(File.basename(x), options) }

      lambdas.each do |l|
        say "#{l.function.name}: #{l.function.description}"
      end
    end
  end

  run!
end