github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/command/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"context"
     8  	"github.com/micro/go-micro/v2"
     9  
    10  	proto "github.com/micro/go-micro/v2/agent/proto"
    11  )
    12  
    13  type Command struct{}
    14  
    15  // Help returns the command usage
    16  func (c *Command) Help(ctx context.Context, req *proto.HelpRequest, rsp *proto.HelpResponse) error {
    17  	rsp.Usage = "command"
    18  	rsp.Description = "This is an example bot command as a micro service"
    19  	return nil
    20  }
    21  
    22  // Exec executes the command
    23  func (c *Command) Exec(ctx context.Context, req *proto.ExecRequest, rsp *proto.ExecResponse) error {
    24  	rsp.Result = []byte(strings.Join(req.Args, " "))
    25  	// rsp.Error could be set to return an error instead
    26  	// the function error would only be used for service level issues
    27  	return nil
    28  }
    29  
    30  func main() {
    31  	service := micro.NewService(
    32  		micro.Name("go.micro.bot.command"),
    33  	)
    34  
    35  	service.Init()
    36  
    37  	proto.RegisterCommandHandler(service.Server(), new(Command))
    38  
    39  	if err := service.Run(); err != nil {
    40  		fmt.Println(err)
    41  	}
    42  }