cli

module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2025 License: MIT

README ΒΆ

cli

wakatime Build Status Go Report Card codecov

GitHub issues GitHub pull requests

code generation CLI tool for catalystgo projects

Installation πŸ—

Using Go 🐹
go install github.com/catalystgo/cli/cmd/catalystgo@latest
Using Docker 🐳
docker pull catalystgo/cli:latest

Usage πŸš€

Commands πŸ“œ
Command Short Description
init i Initialize the project files
implement impl Generate the gRPC code for the project
version ver Print the version of the tool
help Print the help message

Example

Prerequisites
Steps
  1. Initialize the project

    catalystgo init github.com/username/repo
    
  2. Create proto file

    mkdir -p api/user
    touch api/user/user.proto
    
  3. Insert proto file content

    syntax = "proto3";
    
    package user_pb;
    
    import "google/api/annotations.proto";
    import "google/api/http.proto";
    import "protoc-gen-openapiv2/options/annotations.proto";
    import "protoc-gen-openapiv2/options/openapiv2.proto";
    
    option go_package = "github.com/username/repo/pkg/example";
    
    service UserService {
      rpc Authenticate(AuthenticateRequest) returns (AuthenticateResponse) {
        option (google.api.http) = {
          post: "/user/authenticate"
          body: "*"
        };
        option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
          description: "Authenticate a user"
        };
      }
    }
    
    message AuthenticateRequest {
      string username = 1;
      string password = 2;
    }
    
    message AuthenticateResponse {
      string token = 1;
    }
    
  4. Generate the gRPC code

    task generate
    
  5. Modify the content of internal/api/user/authenticate.go to

    import (
       "context"
       desc "github.com/escalopa/awesome-app/pkg/example"
       "google.golang.org/grpc/codes"
       "google.golang.org/grpc/status"
    )
    
    func (i *Implementation) Authenticate(ctx context.Context, req *desc.AuthenticateRequest) (*desc.AuthenticateResponse, error) {
        if req.GetUsername() == "admin" && req.GetPassword() == "admin" {
            return &desc.AuthenticateResponse{Token: "admin-token"}, nil
        }
        return nil, status.Error(codes.Unauthenticated, "invalid credentials")
    }
    
  6. Modify the content of cmd/awesome-app/main.go to be

    func main() {
       app, err := catalystgo.New()
    
       ... 
    
       srv := example.NewUserService()
    
       ...
    
       err := app.Run(srv)
    }
    
  7. Run the app

    task run
    
  8. Test the app

    # using http
    curl -X POST -d '{"username": "admin", "password": "admin"}' http://localhost:8080/user/authenticate
    
    # using grpc
    grpcurl -plaintext -d '{"username": "admin", "password": "admin"}' localhost:8080 user_pb.UserService/Authenticate
    

Features 🎯

  • Write tests for service.Service methods in generated code

Directories ΒΆ

Path Synopsis
cmd
catalystgo command
internal
cli

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL