The fun init command is a subcommand of Funcraft, which can be used to specify application templates. You can use common templates provided by Function Compute or custom templates to create applications with efficiency. This topic describes how to run the fun init command to experience and develop an application with efficiency.

Overview

Run the following command to query the features of the fun init command:
fun init --help
The following result is returned:
Usage: fun init [options] [template]

Initialize a new project based on a template. A template can be a folder containing template metadata and boilerplate files, a name of a pre-built template, or a url that resolves to a template. You can find more information about template at https://yq.aliyun.com/articles/674364.

Options:
  -o, --output-dir [path]  Where to output the initialized app into (default: ".")
  -n, --name [name]        The name of your project to be generated as a folder (default: "")
  -m, --merge [merge]      Merge into the template.[yml|yaml] file if it already exist (default: false)
  --no-input               Disable prompting and accept default values defined template config
  -V, --var [vars]         Template variable
  -h, --help               display help for command

  Examples:

    $ fun init
    $ fun init event-nodejs8
    $ fun init foo/bar
    $ fun init gh:foo/bar
    $ fun init gl:foo/bar
    $ fun init bb:foo/bar
    $ fun init github:foo/bar
    $ fun init gitlab:foo/bar
    $ fun init bitbucket:foo/bar
    $ fun init git+ssh://git@github.com/foo/bar.git
    $ fun init hg+ssh://hg@bitbucket.org/bar/foo
    $ fun init git@github.com:foo/bar.git
    $ fun init https://github.com/foo/bar.git
    $ fun init /path/foo/bar
    $ fun init -n fun-app -V foo=bar /path/foo/bar

Options that can be added to the fun init command

Option Default value Required Description Example
-o, --output-dir . No The directory where the initialized application is to be stored.
fun init -o /path/foo/bar
-n, --name fun-app No The name of the folder where the application resides.
fun init -n <projectname>
-m, --merge false No Specifies whether to merge the current content into the YAML file.
fun init -m
--no-input N/A No Disables prompts and uses default values for template variables.

If you do not want to receive the input prompts of template variables, you can use this option to disable prompts and use default values for template variables.

fun init --no-input       
-V, --var N/A No The variables in the template. The template may contain multiple template variables. When you initialize the template, if you need to specify values for some variables, you are prompted to set the variables. You can set the variables in one of the following ways:
  • Enter a value.
  • Press the Enter key to use the default value.
  • Use the -V, --var option to specify values for the variables. You are not prompted to specify values for the variables after you use this option to specify them.
Note You must enter a key-value pair for this option. The key can contain letters, digits, and underscores (_). The value can contain all characters. Separate a key and a value with an equal sign (=).
fun init -V foo=bar -V bar=baz
-h, --help N/A No Displays the help information of the fun init command.
fun init -h

Obtain application templates

You can use the following methods to obtain application templates:
  • Templates provided by Function Compute, including offline and online templates
    • Directly use the offline templates that are integrated into Funcraft.
    • Specify the name of an online template. Then, Funcraft converts the name to the GitHub address of the template.
  • Git or Mercurial repository templates: Obtain application templates from the corresponding repository.
  • Templates stored in the on-premises file system: Obtain application templates from the on-premises file system.
  • Custom templates: Obtain uploaded custom templates from GitHub.

Obtain offline templates provided by Function Compute

  1. Run the following command to specify the directory where the application resides:
    fun init -o /myapp
  2. Select an application template that is integrated into Funcraft.
    ? Select a template to init (Use arrow keys or type to search)
    > event-nodejs12
      event-nodejs10
      event-nodejs8
      event-nodejs6
      event-python3
      event-python2.7
      event-java8
      event-php7.2
      event-dotnetcore2.1
      http-trigger-nodejs12
      http-trigger-nodejs10
      http-trigger-nodejs8
      http-trigger-nodejs6
      http-trigger-python3
      http-trigger-python2.7
      http-trigger-java8 
    (Move up and down to reveal more choices)                 
    Note In the specified directory, you can view the created template application, and write your logic code in the code file.

Obtain online templates provided by Function Compute

Online templates provided by Function Compute are stored in GitHub.

Run the fun init <templates name> command to obtain the online application template.

Obtain Git or Mercurial repository templates

Git or Mercurial repository templates support multiple prefix abbreviation methods. Addresses of repository templates in GitHub can be written in the user/repo format. You can use the following templates:
  • fun init foo/bar
  • fun init fun init gh:foo/bar/bar
  • fun initfun init gl:foo/barfoo/bar
  • fun init fun init bb:foo/bar/bar
  • fun fun init github:foo/barfoo/bar
  • fun fun init gitlab:foo/barfoo/bar
  • fun fun init bitbucket:foo/barfoo/bar
  • fun ifun init git+ssh://git@github.com/foo/bar.gitnit foo/bar
  • fun init hg+ssh://hg@bitbucket.org/bar/foo
  • fun init git@github.com:foo/bar.git
  • fun init https://github.com/foo/bar.git
  • fun init /path/foo/bar
  • fun init -n fun-app -V foo=bar /path/foo/bar

Obtain on-premises templates

  1. Clone an online template to the on-premises file system.
  2. Run the following command to obtain the on-premises application template:
    fun init /path/foo/bar // Replace path with the path to the folder where your application resides. 

Obtain a custom template

If the existing templates cannot meet your requirements, you can customize a template and upload it to GitHub. When you or others need to use the template, you can obtain the template from GitHub.

  1. Customize your application template.
  2. Upload the template to GitHub.
  3. Run the following command to obtain the custom template:
    fun init <template URL>