Class: FaaStRuby::Command::Function::New

Inherits:
FunctionBaseCommand show all
Defined in:
lib/faastruby/cli/commands/function/new.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FunctionBaseCommand

#load_yaml

Methods inherited from BaseCommand

#has_user_logged_in?, #help, #load_credentials, #load_yaml, #say, spin, #spin, #write_file

Constructor Details

#initialize(args) ⇒ New

Returns a new instance of New.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/faastruby/cli/commands/function/new.rb', line 10

def initialize(args)
  @args = args
  help
  @missing_args = []
  FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
  @function_name = @args.shift
  FaaStRuby::CLI.error("The function name must have at least one character and can only contain letters, numbers, -, _, . and /. Names with just a period are not allowed. Invalid name: #{@function_name}") unless name_valid?
  parse_options
  @base_dir ||= @function_name
  @options['runtime_name'] ||= 'ruby'
  @options['runtime_version'] ||= CURRENT_MINOR_RUBY
  if @options['blank_template']
    @options['template'] = FaaStRuby::Template.new(type: 'local', source: Template.gem_template_path_for('example-blank', runtime: @options['runtime_name'] || 'ruby'))
  else
    @options['template'] ||= FaaStRuby::Template.new(type: 'local', source: Template.gem_template_path_for('example', runtime: @options['runtime_name']))
  end
end

Class Method Details

.helpObject



45
46
47
# File 'lib/faastruby/cli/commands/function/new.rb', line 45

def self.help
  "new FUNCTION_NAME [ARGS]"
end

Instance Method Details

#run(print_base_dir: false, blank_template: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/faastruby/cli/commands/function/new.rb', line 28

def run(print_base_dir: false, blank_template: false)
  @options['blank_template'] ||= blank_template
  @options['template'].install(to: @base_dir, force: @options['force'], print_base_dir: print_base_dir)
  faastruby_yaml = "#{@base_dir}/faastruby.yml"
  if File.file?(faastruby_yaml)
    @yaml_content = YAML.load(File.read(faastruby_yaml))
    @yaml_content['name'] = @function_name
    @options['runtime_name'], @options['runtime_version'] = @yaml_content['runtime']&.split(':')
    @options['runtime_name'] ||= 'ruby'
    @options['runtime_version'] ||= CURRENT_MINOR_RUBY
  else
    @yaml_content = yaml_for(@options['runtime_name'])
  end
  write_yaml(print_base_dir: print_base_dir)
  post_tasks(@options['runtime_name'])
end

#usageObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/faastruby/cli/commands/function/new.rb', line 49

def usage
  puts "\nUsage: faastruby #{self.class.help}"
  puts %(
--blank          # Create a blank function
--force          # Continue if directory already exists and overwrite files
-g, --git        # Initialize a Git repository.
--runtime        # Set the language runtime.
         # Options are: #{SUPPORTED_RUNTIMES.join(', ')}
--template TYPE(local|git|github):SOURCE   # Initialize the function using a template
                                   # Examples:
                                   # --template local:/path/to/folder
                                   # --template git:[email protected]:user/repo.git
                                   # --template github:user/repo
)
end