Plugin Commands

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

Goctl supports custom plugins for api, so how do I customize a plugin? Let’s take a look at an example of how to finally use it below.

  1. $ goctl api plugin -p goctl-android="android -package com.tal" -api user.api -dir .

The above command can be broken down into the following steps:

  • goctl parsing api file
  • goctl passes the parsed structure ApiSpec and parameters to the goctl-android executable file
  • goctl-android customizes the generation logic according to the ApiSpec structure.

The first part of this command goctl api plugin -p is a fixed parameter, goctl-android=”android -package com.tal” is a plugin parameter, where goctl-android is the plugin binary file, and android -package com.tal is a custom parameter of the plugin , -Api user.api -dir. Is a common custom parameter for goctl.

How to write a custom plug-in?

A very simple custom plug-in demo is included in the go-zero framework. The code is as follows:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/zeromicro/go-zero/tools/goctl/plugin"
  5. )
  6. func main() {
  7. plugin, err := plugin.NewPlugin()
  8. if err != nil {
  9. panic(err)
  10. }
  11. if plugin.Api != nil {
  12. fmt.Printf("api: %+v \n", plugin.Api)
  13. }
  14. fmt.Printf("dir: %s \n", plugin.Dir)
  15. fmt.Println("Enjoy anything you want.")
  16. }

plugin, err := plugin.NewPlugin() The function of this line of code is to parse the data passed from goctl, which contains the following parts:

  1. type Plugin struct {
  2. Api *spec.ApiSpec
  3. Style string
  4. Dir string
  5. }

[!TIP] Api: defines the structure data of the api file

Style: optional, it is used to control file naming conventions

Dir: workDir

Complete android plugin demo project based on plugin https://github.com/zeromicro/goctl-android

Guess you wants