Template Operation

Template is the basis of data-driven generation, all code (rest api, rpc, model, docker, kube) generation will rely on template. By default, the template generator selects the in-memory template for generation, while for developers who need to modify the template, they need to drop the template and make template changes in the next code generation. For developers who need to modify the templates, they need to modify the templates, and then the next time the code is generated, it will load the templates under the specified path to generate.

Help

  1. NAME:
  2. goctl template - template operation
  3. USAGE:
  4. goctl template command [command options] [arguments...]
  5. COMMANDS:
  6. init initialize the all templates(force update)
  7. clean clean the all cache templates
  8. update update template of the target category to the latest
  9. revert revert the target template to the latest
  10. OPTIONS:
  11. --help, -h show help

Init

  1. NAME:
  2. goctl template init - initialize the all templates(force update)
  3. USAGE:
  4. goctl template init [command options] [arguments...]
  5. OPTIONS:
  6. --home value the goctl home path of the template

Clean

  1. NAME:
  2. goctl template clean - clean the all cache templates
  3. USAGE:
  4. goctl template clean [command options] [arguments...]
  5. OPTIONS:
  6. --home value the goctl home path of the template

Update

  1. NAME:
  2. goctl template update - update template of the target category to the latest
  3. USAGE:
  4. goctl template update [command options] [arguments...]
  5. OPTIONS:
  6. --category value, -c value the category of template, enum [api,rpc,model,docker,kube]
  7. --home value the goctl home path of the template

Revert

  1. NAME:
  2. goctl template revert - revert the target template to the latest
  3. USAGE:
  4. goctl template revert [command options] [arguments...]
  5. OPTIONS:
  6. --category value, -c value the category of template, enum [api,rpc,model,docker,kube]
  7. --name value, -n value the target file name of template
  8. --home value the goctl home path of the template

[!TIP]

--home Specify the template storage path

Template loading

You can specify the folder where the template is located by --home during code generation, and the commands that have been supported to specify the template directory are

  • goctl api go Details can be found in goctl api go --help for help
  • goctl docker Details can be viewed with goctl docker --help
  • goctl kube Details can be viewed with goctl kube --help
  • goctl rpc new Details can be viewed with goctl rpc new --help
  • goctl rpc proto Details can be viewed with goctl rpc proto --help
  • goctl model mysql ddl Details can be viewed with goctl model mysql ddl --help
  • goctl model mysql datasource Details can be viewed with goctl model mysql datasource --help
  • goctl model postgresql datasource Details can be viewed with goctl model mysql datasource --help
  • goctl model mongo Details can be viewed with goctl model mongo --help

The default (when --home is not specified) is to read from the $HOME/.goctl directory.

Example

  • Initialize the template to the specified $HOME/template directory
    1. $ goctl template init --home $HOME/template
  1. Templates are generated in /Users/anqiansong/template, edit on your risk!
  • Greet rpc generation using $HOME/template template
    1. $ goctl rpc new greet --home $HOME/template
  1. Done