Skip to content
/ cli Public

πŸ— Catalystgo CLI tool for code generation πŸ—

License

Notifications You must be signed in to change notification settings

catalystgo/cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

cli

wakatime Build Status Go Report Card codecov

GitHub issues GitHub pull requests License: MIT

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/catalystgo/awesome-app
  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/catalystgo/awesome-app/pkg/user";
    
    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

    package user
    
    import (
       "context"
       desc "github.com/catalystgo/awesome-app/pkg/user"
       "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

    package main
    
    import (
     "context"
    
     "github.com/catalystgo/catalystgo"
     "github.com/catalystgo/logger/logger"
     "github.com/catalystgo/awesome-app/internal/api/user"
    )
    
    func main() {
     ctx := context.Background()
    
     app, err := catalystgo.New()
     if err != nil {
       logger.Fatalf(ctx, "create app: %v", err)
     }
     
     srv := user.NewUserService()
    
     if err := app.Run(srv); err != nil {
       logger.Fatalf(ctx, "run app: %v", err)
     }
    }
  7. Run the app

    task run
  8. Test the app

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

Congratz!!! You have built a golang app in seconds πŸ₯³

Contributing 🀝

Alt