Skip to content

Commit

Permalink
fix: barry 2024-07-27 13:04:58
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Jul 27, 2024
1 parent 522002d commit f132b86
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 42 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ LDFLAGS=-ldflags " \
build:
go build ${LDFLAGS} -mod vendor -v -o main *.go

.PHONY: install
install:
go install ${LDFLAGS} -mod vendor -v .

vet:
@go vet ./...
gofumpt -l -w -extra .
Expand Down
60 changes: 38 additions & 22 deletions cmd/protobuild/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package protobuild

import (
"bytes"
"context"
"fmt"
"io"
"io/fs"
Expand All @@ -20,7 +21,7 @@ import (
"github.com/pubgo/funk/pathutil"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/strutil"
cli "github.com/urfave/cli/v2"
cli "github.com/urfave/cli/v3"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
Expand Down Expand Up @@ -84,27 +85,38 @@ func parseConfig() (gErr error) {
return nil
}

func Main() *cli.App {
func Main() *cli.Command {
var force bool
app := &cli.App{
Name: "protobuf build",
Usage: "protobuf generation, configuration and management",
Version: version.Version,
app := &cli.Command{
Name: "protobuf",
Usage: "protobuf generation, configuration and management",
Version: version.Version,
ShellComplete: cli.DefaultAppComplete,
EnableShellCompletion: true,
Suggest: true,
Flags: typex.Flags{
&cli.StringFlag{
Name: "conf",
Aliases: typex.Strs{"c", "f"},
Usage: "protobuf config path",
Value: protoCfg,
Hidden: false,
Persistent: true,
Destination: &protoCfg,
},
},
Action: func(context *cli.Context) error {
Action: func(ctx context.Context, c *cli.Command) error {
if shutil.IsHelp() {
return nil
}

in := assert.Must1(io.ReadAll(os.Stdin))
file := os.Stdin
fi := assert.Exit1(file.Stat())
if fi.Size() == 0 {
return cli.ShowAppHelp(c)
}

in := assert.Must1(io.ReadAll(file))
req := &pluginpb.CodeGeneratorRequest{}
assert.Must(proto.Unmarshal(in, req))

Expand Down Expand Up @@ -157,14 +169,14 @@ func Main() *cli.App {

return nil
},
Commands: cli.Commands{
Commands: typex.Commands{
&cli.Command{
Name: "gen",
Usage: "编译 protobuf 文件",
Before: func(context *cli.Context) error {
Before: func(ctx context.Context, c *cli.Command) error {
return parseConfig()
},
Action: func(ctx *cli.Context) error {
Action: func(ctx context.Context, c *cli.Command) error {
defer recovery.Exit()

var protoList sync.Map
Expand Down Expand Up @@ -242,7 +254,7 @@ func Main() *cli.App {
return "."
}()

_ = pathutil.IsNotExistMkDir(out)
assert.Exit(pathutil.IsNotExistMkDir(out))

opts := plg.Opt
hasPath := func() bool {
Expand Down Expand Up @@ -299,9 +311,10 @@ func Main() *cli.App {
assert.Must(shutil.Shell(data).Run(), data)
if retagOut != "" && retagOpt != "" {
data = base + retagOut + retagOpt + " " + filepath.Join(in, "*.proto")
logger.Info().Bool("retag", true).Msg(data)
assert.Must(shutil.Shell(data).Run(), data)
}
logger.Info().Msg(data)
assert.Must(shutil.Shell(data).Run(), data)

return true
})
return nil
Expand All @@ -310,7 +323,7 @@ func Main() *cli.App {
&cli.Command{
Name: "vendor",
Usage: "同步项目 protobuf 依赖到 .proto 目录中",
Before: func(context *cli.Context) error {
Before: func(ctx context.Context, c *cli.Command) error {
return parseConfig()
},
Flags: typex.Flags{
Expand All @@ -322,7 +335,7 @@ func Main() *cli.App {
Destination: &force,
},
},
Action: func(ctx *cli.Context) error {
Action: func(ctx context.Context, c *cli.Command) error {
defer recovery.Exit()

var changed bool
Expand Down Expand Up @@ -390,12 +403,15 @@ func Main() *cli.App {
cfg.Depends[i].Version = generic.Ptr(v)
}

var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
defer enc.Close()
assert.Must(enc.Encode(cfg))
assert.Must(os.WriteFile(protoCfg, buf.Bytes(), 0o666))
// TODO 强制更新配置文件, 可以考虑参数化
{
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
defer enc.Close()
assert.Must(enc.Encode(cfg))
assert.Must(os.WriteFile(protoCfg, buf.Bytes(), 0o666))
}

if !changed && !cfg.changed && !force {
fmt.Println("No changes")
Expand Down
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/pubgo/funk v0.5.49
github.com/searKing/golang/go v1.2.115
github.com/spf13/cast v1.5.0
github.com/urfave/cli/v2 v2.8.0
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f
github.com/yuin/goldmark v1.4.12
go.uber.org/multierr v1.11.0
golang.org/x/mod v0.14.0
Expand All @@ -30,10 +30,7 @@ require (
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/alecthomas/repr v0.4.0 // indirect
github.com/antzucaro/matchr v0.0.0-20210222213004-b04723ef80f0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand All @@ -42,9 +39,7 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/phuslu/goid v1.0.0 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/a8m/envsubst v1.3.0 h1:GmXKmVssap0YtlU3E230W98RWtWCyIZzjtf1apWWyAg=
github.com/a8m/envsubst v1.3.0/go.mod h1:MVUTQNGQ3tsjOOtKCNd+fl8RzhsXcDvvAEzkhGtlsbY=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/antzucaro/matchr v0.0.0-20210222213004-b04723ef80f0 h1:R/qAiUxFT3mNgQaNqJe0IVznjKRNm23ohAIh9lgtlzc=
github.com/antzucaro/matchr v0.0.0-20210222213004-b04723ef80f0/go.mod h1:v3ZDlfVAL1OrkKHbGSFFK60k0/7hruHPDq2XMs9Gu6U=
github.com/bufbuild/protocompile v0.5.1 h1:mixz5lJX4Hiz4FpqFREJHIXLfaLBntfaJv1h+/jS+Qg=
github.com/bufbuild/protocompile v0.5.1/go.mod h1:G5iLmavmF4NsYtpZFvE3B/zFch2GIY8+wjsYLR/lc40=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand All @@ -59,8 +55,6 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 h1:ox2F0PSMlrAAiAdknSRMDrAr8mfxPCfSZolH+/qQnyQ=
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08/go.mod h1:pCxVEbcm3AMg7ejXyorUXi6HQCzOIBf7zEDVPtw0/U4=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/dave/jennifer v1.6.0 h1:MQ/6emI2xM7wt0tJzJzyUik2Q3Tcn2eE0vtYgh4GPVI=
github.com/dave/jennifer v1.6.0/go.mod h1:AxTG893FiZKqxy3FP1kL80VMshSMuz2G+EgvszgGRnk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -197,8 +191,6 @@ github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/searKing/golang/go v1.2.115 h1:JqnZZXanD/TphR1aT0NOOpJnt78PL1cJA5/ZzYIpjNM=
github.com/searKing/golang/go v1.2.115/go.mod h1:VoL3JmaQd7yt+o9jZ9t9HnfABv5/RYBGzMXlS5415S0=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
Expand All @@ -217,8 +209,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/urfave/cli/v2 v2.8.0 h1:EZsAB20oRW4nHcB99TTL6PrXpBGIEujMEKdjwruY9KQ=
github.com/urfave/cli/v2 v2.8.0/go.mod h1:TYFbtzt/azQoJOrGH5mDfZtS0jIkl/OeFwlRWPR9KRM=
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f h1:yCJ90PBe7+45EQSF3qJXyAGW5rkE65lE8huv5pM0HY8=
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f/go.mod h1:Z1ItyMma7t6I7zHG9OpbExhHQOSkFf/96n+mAZ9MtVI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
Expand Down
4 changes: 2 additions & 2 deletions internal/typex/cli.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package typex

import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

type (
Strs = []string
Flags = []cli.Flag
Command = cli.Command
Commands = []cli.Command
Commands = []*cli.Command
)
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"os"

"github.com/pubgo/funk/assert"
Expand All @@ -9,6 +10,6 @@ import (

func main() {
assert.ExitFn(func() error {
return protobuild.Main().Run(os.Args)
return protobuild.Main().Run(context.Background(), os.Args)
})
}
2 changes: 1 addition & 1 deletion protobuf.test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
checksum: 85da3b3146c7021097b973c086b0a12dae70c023
checksum: ae1e52134aaa1770db37e83bf3cea605874942cb
vendor: .proto
root:
- proto
Expand Down

0 comments on commit f132b86

Please sign in to comment.