Skip to content

Commit

Permalink
Merge pull request #6 from storyicon/v0.4.0
Browse files Browse the repository at this point in the history
feat(*): support perComandTimeout & variables in options
  • Loading branch information
storyicon authored Aug 28, 2021
2 parents 7b2c076 + 1504771 commit 8bdd192
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 118 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ plugins:
protoc-gen-go-json: github.com/mitchellh/protoc-gen-go-json@v1.0.0
protoc-gen-grpc-gateway: github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.5.0
# required. defines the parameters of protoc when compiling proto files
# In options, you can still use variables like $GOPATH, $SOURCE_RELATIVE, $GOGO_PROTOBUF as in importPaths
options:
- --go_out=paths=source_relative:.
- --go-json_out=.
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ plugins:
protoc-gen-go-json: github.com/mitchellh/protoc-gen-go-json@v1.0.0
protoc-gen-grpc-gateway: github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.5.0
# 必填,定义了编译proto文件时 protoc 的参数
# 在options里,你仍然可以像在 importPaths 中一样使用像 $GOPATH、$SOURCE_RELATIVE、$GOGO_PROTOBUF这样的变量
options:
- --go_out=paths=source_relative:.
- --go-json_out=.
Expand Down
4 changes: 4 additions & 0 deletions cmd/powerproto/subcommands/build/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -51,6 +52,7 @@ func CommandBuild(log logger.Logger) *cobra.Command {
var dryRun bool
var debugMode bool
var postScriptEnabled bool
perCommandTimeout := time.Second * 300
cmd := &cobra.Command{
Use: "build [dir|proto file]",
Short: "compile proto files",
Expand All @@ -59,6 +61,7 @@ func CommandBuild(log logger.Logger) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
log.SetLogLevel(logger.LevelInfo)
ctx := cmd.Context()
ctx = consts.WithPerCommandTimeout(ctx, perCommandTimeout)
if debugMode {
ctx = consts.WithDebugMode(ctx)
log.LogWarn(nil, "running in debug mode")
Expand Down Expand Up @@ -122,5 +125,6 @@ func CommandBuild(log logger.Logger) *cobra.Command {
flags.BoolVarP(&postScriptEnabled, "postScriptEnabled", "p", postScriptEnabled, "when this flag is attached, it will allow the execution of postActions and postShell")
flags.BoolVarP(&debugMode, "debug", "d", debugMode, "debug mode")
flags.BoolVarP(&dryRun, "dryRun", "y", dryRun, "dryRun mode")
flags.DurationVarP(&perCommandTimeout, "timeout", "t", perCommandTimeout, "execution timeout for per command")
return cmd
}
4 changes: 4 additions & 0 deletions cmd/powerproto/subcommands/tidy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"os"
"path/filepath"
"time"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -62,11 +63,13 @@ func tidy(ctx context.Context,
// You can also explicitly specify the configuration file to clean up
func CommandTidy(log logger.Logger) *cobra.Command {
var debugMode bool
perCommandTimeout := time.Second * 300
cmd := &cobra.Command{
Use: "tidy [config file]",
Short: "tidy the config file. It will replace the version number and install the protoc and proto plugins that declared in the config file",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
ctx = consts.WithPerCommandTimeout(ctx, perCommandTimeout)
log.SetLogLevel(logger.LevelInfo)
if debugMode {
log.SetLogLevel(logger.LevelDebug)
Expand Down Expand Up @@ -121,5 +124,6 @@ func CommandTidy(log logger.Logger) *cobra.Command {
}
flags := cmd.PersistentFlags()
flags.BoolVarP(&debugMode, "debug", "d", debugMode, "debug mode")
flags.DurationVarP(&perCommandTimeout, "timeout", "t", perCommandTimeout, "execution timeout for per command")
return cmd
}
37 changes: 37 additions & 0 deletions examples/multi-config-item/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Using Multi Config Items

This example uses two different configurations for "using-gogo" and "using-googleapis" through the "scope" field.

It uses the following public libraries:
* [googleapis](https://github.com/googleapis/googleapis)

The following plug-ins are used:
* [protoc-gen-go](https://google.golang.org/protobuf/cmd/protoc-gen-go)
* [protoc-gen-go-grpc](https://google.golang.org/grpc/cmd/protoc-gen-go-grpc)
* [protoc-gen-grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway)
* [protoc-gen-gogo](https://github.com/gogo/protobuf/protoc-gen-gogo)


You can compile the proto file in this directory by executing the following command:
```
powerproto build -r .
```

Not surprisingly, you will get the following output:
```
➜ tree
.
├── README.md
├── powerproto.yaml
├── using-gogo
│   ├── service.pb.go
│   ├── service.pb.gw.go
│   └── service.proto
└── using-googleapis
├── service.pb.go
├── service.pb.gw.go
├── service.proto
└── service_grpc.pb.go
2 directories, 9 files
```
52 changes: 52 additions & 0 deletions examples/multi-config-item/powerproto.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
scopes:
- ./using-gogo
protoc: v3.17.0
protocWorkDir: ""
plugins:
protoc-gen-gogo: github.com/gogo/protobuf/protoc-gen-gogo@v1.3.2
protoc-gen-grpc-gateway: github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.5.0
repositories:
GOGO_PROTOBUF: https://github.com/gogo/protobuf@226206f39bd7276e88ec684ea0028c18ec2c91ae
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --grpc-gateway_out=.
- --grpc-gateway_opt=paths=source_relative
- --gogo_out=plugins=grpc:.
- --gogo_opt=paths=source_relative
importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $SOURCE_RELATIVE
- $GOOGLE_APIS/github.com/googleapis/googleapis
- $GOGO_PROTOBUF
postActions: []
postShell: ""

---

scopes:
- ./using-googleapis
protoc: v3.17.3
protocWorkDir: ""
plugins:
protoc-gen-go: google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1
protoc-gen-go-grpc: google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
protoc-gen-grpc-gateway: github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.5.0
repositories:
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --go_out=.
- --go_opt=paths=source_relative
- --go-grpc_out=.
- --go-grpc_opt=paths=source_relative
- --grpc-gateway_out=.
- --grpc-gateway_opt=paths=source_relative
importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $SOURCE_RELATIVE
- $GOOGLE_APIS/github.com/googleapis/googleapis
postActions: []
postShell: ""
40 changes: 40 additions & 0 deletions examples/multi-config-item/using-gogo/service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";

import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/api/annotations.proto";

package powerproto.examples.using_gogo;
option go_package = "github.com/storyicon/powerproto/examples/using_gogo/apis;apis";

option java_multiple_files = true;
option java_package = "com.powerproto.examples.using_gogo";
option objc_class_prefix = "BAPIMetadata";

option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_registration) = true;

service Mocker {
rpc Echo (EchoRequest) returns (EchoResponse) {
option (google.api.http) = {
post: "/echo"
body: "*"
};
};
}

message EchoRequest {
string message = 1;
int64 timestamp = 2;
uint32 code = 3;
uint32 delay = 4;
}

message EchoResponse {
string message = 1;
int64 timestamp = 2;
string server = 3;
}
28 changes: 28 additions & 0 deletions examples/multi-config-item/using-googleapis/service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";

package powerproto.examples.using_googleapis;
option go_package = "github.com/storyicon/powerproto/examples/using_googleapis/apis;apis";

import "google/api/annotations.proto";

service Mocker {
rpc Echo (EchoRequest) returns (EchoResponse) {
option (google.api.http) = {
post: "/echo"
body: "*"
};
};
}

message EchoRequest {
string message = 1;
int64 timestamp = 2;
uint32 code = 3;
uint32 delay = 4;
}

message EchoResponse {
string message = 1;
int64 timestamp = 2;
string server = 3;
}
Loading

0 comments on commit 8bdd192

Please sign in to comment.