From 15047711a94fe117ff6b2e51f33d205cf4ae2abe Mon Sep 17 00:00:00 2001 From: storyicon Date: Sat, 28 Aug 2021 14:36:47 +0800 Subject: [PATCH] feat(*): support perComandTimeout & variables in options Signed-off-by: storyicon --- README.md | 1 + README_CN.md | 1 + cmd/powerproto/subcommands/build/command.go | 4 + cmd/powerproto/subcommands/tidy/command.go | 4 + examples/multi-config-item/README.md | 37 ++++ examples/multi-config-item/powerproto.yaml | 52 ++++++ .../using-gogo/service.proto | 40 +++++ .../using-googleapis/service.proto | 28 +++ pkg/component/compilermanager/compiler.go | 163 +++++++----------- pkg/component/pluginmanager/manager.go | 49 ++++-- pkg/component/pluginmanager/paths.go | 5 + pkg/component/pluginmanager/protoc.go | 3 + pkg/consts/consts.go | 3 +- pkg/consts/context.go | 19 ++ pkg/util/util.go | 15 +- 15 files changed, 302 insertions(+), 122 deletions(-) create mode 100644 examples/multi-config-item/README.md create mode 100644 examples/multi-config-item/powerproto.yaml create mode 100644 examples/multi-config-item/using-gogo/service.proto create mode 100644 examples/multi-config-item/using-googleapis/service.proto diff --git a/README.md b/README.md index 2628268..57ee30a 100644 --- a/README.md +++ b/README.md @@ -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=. diff --git a/README_CN.md b/README_CN.md index 402972a..75abccf 100644 --- a/README_CN.md +++ b/README_CN.md @@ -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=. diff --git a/cmd/powerproto/subcommands/build/command.go b/cmd/powerproto/subcommands/build/command.go index 025444d..664915e 100644 --- a/cmd/powerproto/subcommands/build/command.go +++ b/cmd/powerproto/subcommands/build/command.go @@ -18,6 +18,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/spf13/cobra" @@ -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", @@ -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") @@ -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 } diff --git a/cmd/powerproto/subcommands/tidy/command.go b/cmd/powerproto/subcommands/tidy/command.go index d0a38e4..6a0eb2b 100644 --- a/cmd/powerproto/subcommands/tidy/command.go +++ b/cmd/powerproto/subcommands/tidy/command.go @@ -18,6 +18,7 @@ import ( "context" "os" "path/filepath" + "time" "github.com/spf13/cobra" @@ -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) @@ -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 } diff --git a/examples/multi-config-item/README.md b/examples/multi-config-item/README.md new file mode 100644 index 0000000..e5437e1 --- /dev/null +++ b/examples/multi-config-item/README.md @@ -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 +``` diff --git a/examples/multi-config-item/powerproto.yaml b/examples/multi-config-item/powerproto.yaml new file mode 100644 index 0000000..c130c53 --- /dev/null +++ b/examples/multi-config-item/powerproto.yaml @@ -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: "" diff --git a/examples/multi-config-item/using-gogo/service.proto b/examples/multi-config-item/using-gogo/service.proto new file mode 100644 index 0000000..9e6d59a --- /dev/null +++ b/examples/multi-config-item/using-gogo/service.proto @@ -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; +} diff --git a/examples/multi-config-item/using-googleapis/service.proto b/examples/multi-config-item/using-googleapis/service.proto new file mode 100644 index 0000000..74f3d10 --- /dev/null +++ b/examples/multi-config-item/using-googleapis/service.proto @@ -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; +} diff --git a/pkg/component/compilermanager/compiler.go b/pkg/component/compilermanager/compiler.go index a96b7bc..5046f4c 100644 --- a/pkg/component/compilermanager/compiler.go +++ b/pkg/component/compilermanager/compiler.go @@ -44,12 +44,6 @@ type BasicCompiler struct { logger.Logger config configs.ConfigItem pluginManager pluginmanager.PluginManager - - protocPath string - arguments []string - // map[name]local - repositories map[string]string - dir string } // NewCompiler is used to create a compiler @@ -69,43 +63,28 @@ func NewBasicCompiler( pluginManager pluginmanager.PluginManager, config configs.ConfigItem, ) (*BasicCompiler, error) { - basic := &BasicCompiler{ + return &BasicCompiler{ Logger: log.NewLogger("compiler"), config: config, pluginManager: pluginManager, - } - basic.repositories = map[string]string{ - consts.KeyNamePowerProtocInclude: basic.pluginManager.IncludePath(ctx), - } - if err := basic.calcRepositories(ctx); err != nil { - return nil, err - } - if err := basic.calcProto(ctx); err != nil { - return nil, err - } - if err := basic.calcArguments(ctx); err != nil { - return nil, err - } - if err := basic.calcDir(ctx); err != nil { - return nil, err - } - return basic, nil + }, nil } // Compile is used to compile proto file func (b *BasicCompiler) Compile(ctx context.Context, protoFilePath string) error { - arguments := make([]string, len(b.arguments)) - copy(arguments, b.arguments) - - // contains source relative - if util.Contains(b.config.Config().ImportPaths, - consts.KeySourceRelative) { - arguments = append(arguments, "--proto_path="+filepath.Dir(protoFilePath)) + dir := b.calcDir() + protocPath, err := b.calcProtocPath(ctx) + if err != nil { + return err + } + + arguments, err := b.calcArguments(ctx, protoFilePath) + if err != nil { + return err } - arguments = util.DeduplicateSliceStably(arguments) arguments = append(arguments, protoFilePath) - _, err := command.Execute(ctx, - b.Logger, b.dir, b.protocPath, arguments, nil) + _, err = command.Execute(ctx, + b.Logger, dir, protocPath, arguments, nil) if err != nil { return &ErrCompile{ ErrCommandExec: err.(*command.ErrCommandExec), @@ -119,99 +98,85 @@ func (b *BasicCompiler) GetConfig(ctx context.Context) configs.ConfigItem { return b.config } -func (b *BasicCompiler) calcDir(ctx context.Context) error { +func (b *BasicCompiler) calcProtocPath(ctx context.Context) (string, error) { + return b.pluginManager.GetPathForProtoc(ctx, b.config.Config().Protoc) +} + +func (b *BasicCompiler) calcDir() string { if dir := b.config.Config().ProtocWorkDir; dir != "" { dir = util.RenderPathWithEnv(dir, nil) if !filepath.IsAbs(dir) { dir = filepath.Join(b.config.Path(), dir) } - b.dir = dir + return dir } else { - b.dir = filepath.Dir(b.config.Path()) + return filepath.Dir(b.config.Path()) } - return nil } -func (b *BasicCompiler) calcProto(ctx context.Context) error { - cfg := b.config - protocVersion := cfg.Config().Protoc - if protocVersion == "latest" { - latestVersion, err := b.pluginManager.GetProtocLatestVersion(ctx) - if err != nil { - return err - } - protocVersion = latestVersion - } - localPath, err := b.pluginManager.InstallProtoc(ctx, protocVersion) +func (b *BasicCompiler) calcArguments(ctx context.Context, protoFilePath string) ([]string, error) { + variables, err := b.calcVariables(ctx, protoFilePath) if err != nil { - return err + return nil, err } - b.protocPath = localPath - return nil -} -func (b *BasicCompiler) calcArguments(ctx context.Context) error { cfg := b.config - arguments := make([]string, len(cfg.Config().Options)) - copy(arguments, cfg.Config().Options) + var arguments []string - dir := filepath.Dir(cfg.Path()) - // build import paths -Loop: - for _, path := range cfg.Config().ImportPaths { - if path == consts.KeySourceRelative { - continue Loop + // build plugin options + for name, pkg := range cfg.Config().Plugins { + path, version, ok := util.SplitGoPackageVersion(pkg) + if !ok { + return nil, errors.Errorf("failed to parse: %s", pkg) } - path = util.RenderPathWithEnv(path, b.repositories) + local, err := b.pluginManager.GetPathForPlugin(ctx, path, version) + if err != nil { + return nil, errors.Wrap(err, "failed to get plugin path") + } + arg := fmt.Sprintf("--plugin=%s=%s", name, local) + arguments = append(arguments, arg) + } + + // build compile options + for _, option := range cfg.Config().Options { + option = util.RenderWithEnv(option, variables) + arguments = append(arguments, option) + } + + // build import paths + dir := filepath.Dir(cfg.Path()) + for _, path := range cfg.Config().ImportPaths { + path = util.RenderPathWithEnv(path, variables) if !filepath.IsAbs(path) { path = filepath.Join(dir, path) } arguments = append(arguments, "--proto_path="+path) } - // build plugin options - for name, pkg := range cfg.Config().Plugins { - path, version, ok := util.SplitGoPackageVersion(pkg) - if !ok { - return errors.Errorf("failed to parse: %s", pkg) - } - if version == "latest" { - latestVersion, err := b.pluginManager.GetPluginLatestVersion(ctx, path) - if err != nil { - return err - } - version = latestVersion - } - local, err := b.pluginManager.InstallPlugin(ctx, path, version) - if err != nil { - return errors.Wrap(err, "failed to get plugin path") - } - arg := fmt.Sprintf("--plugin=%s=%s", name, local) - arguments = append(arguments, arg) - } - b.arguments = arguments - return nil + arguments = util.DeduplicateSliceStably(arguments) + + return arguments, nil } -func (b *BasicCompiler) calcRepositories(ctx context.Context) error { +func (b *BasicCompiler) calcVariables(ctx context.Context, protoFilePath string) (map[string]string, error) { cfg := b.config + variables := map[string]string{} for name, pkg := range cfg.Config().Repositories { - path, version, ok := util.SplitGoPackageVersion(pkg) + _, version, ok := util.SplitGoPackageVersion(pkg) if !ok { - return errors.Errorf("failed to parse: %s", pkg) + return nil, errors.Errorf("failed to parse: %s", pkg) } - if version == "latest" { - latestVersion, err := b.pluginManager.GetGitRepoLatestVersion(ctx, path) - if err != nil { - return err - } - version = latestVersion - } - local, err := b.pluginManager.InstallGitRepo(ctx, path, version) + repoPath, err := b.pluginManager.GitRepoPath(ctx, version) if err != nil { - return errors.Wrap(err, "failed to get plugin path") + return nil, err } - b.repositories[name] = local + variables[name] = repoPath } - return nil + includePath, err := b.pluginManager.IncludePath(ctx) + if err != nil { + return nil, err + } + variables[consts.KeyNamePowerProtocInclude] = includePath + variables[consts.KeyNameSourceRelative] = filepath.Dir(protoFilePath) + return variables, nil } diff --git a/pkg/component/pluginmanager/manager.go b/pkg/component/pluginmanager/manager.go index e447827..a4449f6 100644 --- a/pkg/component/pluginmanager/manager.go +++ b/pkg/component/pluginmanager/manager.go @@ -21,15 +21,12 @@ import ( "os" "strings" "sync" - "time" "github.com/storyicon/powerproto/pkg/consts" "github.com/storyicon/powerproto/pkg/util" "github.com/storyicon/powerproto/pkg/util/logger" ) -var defaultExecuteTimeout = time.Second * 60 - // PluginManager is used to manage plugins type PluginManager interface { // GetPluginLatestVersion is used to get the latest version of plugin @@ -40,6 +37,8 @@ type PluginManager interface { IsPluginInstalled(ctx context.Context, path string, version string) (bool, string, error) // InstallPlugin is used to install plugin InstallPlugin(ctx context.Context, path string, version string) (local string, err error) + // GetPathForPlugin is used to get path for plugin executable file + GetPathForPlugin(ctx context.Context, path string, version string) (local string, err error) // GetGitRepoLatestVersion is used to get the latest version of google apis GetGitRepoLatestVersion(ctx context.Context, uri string) (string, error) @@ -47,8 +46,8 @@ type PluginManager interface { InstallGitRepo(ctx context.Context, uri string, commitId string) (local string, err error) // IsGitRepoInstalled is used to check whether the protoc is installed IsGitRepoInstalled(ctx context.Context, uri string, commitId string) (bool, string, error) - // GitRepoPath returns the googleapis path - GitRepoPath(ctx context.Context, commitId string) string + // GitRepoPath returns the git repo path + GitRepoPath(ctx context.Context, commitId string) (string, error) // GetProtocLatestVersion is used to get the latest version of protoc GetProtocLatestVersion(ctx context.Context) (string, error) @@ -59,7 +58,9 @@ type PluginManager interface { // InstallProtoc is used to install protoc of specified version InstallProtoc(ctx context.Context, version string) (local string, err error) // IncludePath returns the default include path - IncludePath(ctx context.Context) string + IncludePath(ctx context.Context) (string, error) + // GetPathForProtoc is used to get the path of protoc + GetPathForProtoc(ctx context.Context, version string) (string, error) } // Config defines the config of PluginManager @@ -98,6 +99,9 @@ func NewBasicPluginManager(storageDir string, log logger.Logger) (*BasicPluginMa // GetPluginLatestVersion is used to get the latest version of plugin func (b *BasicPluginManager) GetPluginLatestVersion(ctx context.Context, path string) (string, error) { + ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx) + defer cancel() + versions, err := b.ListPluginVersions(ctx, path) if err != nil { return "", err @@ -110,7 +114,7 @@ func (b *BasicPluginManager) GetPluginLatestVersion(ctx context.Context, path st // ListPluginVersions is used to list the versions of plugin func (b *BasicPluginManager) ListPluginVersions(ctx context.Context, path string) ([]string, error) { - ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout) + ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx) defer cancel() b.versionsLock.RLock() @@ -134,6 +138,11 @@ func (b *BasicPluginManager) IsPluginInstalled(ctx context.Context, path string, return IsPluginInstalled(ctx, b.storageDir, path, version) } +// GetPathForPlugin is used to get path for plugin executable file +func (b *BasicPluginManager) GetPathForPlugin(ctx context.Context, path string, version string) (local string, err error) { + return PathForPlugin(b.storageDir, path, version) +} + // InstallPlugin is used to install plugin func (b *BasicPluginManager) InstallPlugin(ctx context.Context, path string, version string) (local string, err error) { return InstallPluginUsingGo(ctx, b.Logger, b.storageDir, path, version) @@ -146,8 +155,9 @@ func (b *BasicPluginManager) GetGitRepoLatestVersion(ctx context.Context, url st // InstallGitRepo is used to install google apis func (b *BasicPluginManager) InstallGitRepo(ctx context.Context, uri string, commitId string) (string, error) { - ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout) + ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx) defer cancel() + exists, local, err := b.IsGitRepoInstalled(ctx, uri, commitId) if err != nil { return "", err @@ -182,8 +192,8 @@ func (b *BasicPluginManager) IsGitRepoInstalled(ctx context.Context, uri string, } // GitRepoPath returns the googleapis path -func (b *BasicPluginManager) GitRepoPath(ctx context.Context, commitId string) string { - return PathForGitRepos(b.storageDir, commitId) +func (b *BasicPluginManager) GitRepoPath(ctx context.Context, commitId string) (string, error) { + return PathForGitRepos(b.storageDir, commitId), nil } // IsProtocInstalled is used to check whether the protoc is installed @@ -196,6 +206,9 @@ func (b *BasicPluginManager) IsProtocInstalled(ctx context.Context, version stri // GetProtocLatestVersion is used to geet the latest version of protoc func (b *BasicPluginManager) GetProtocLatestVersion(ctx context.Context) (string, error) { + ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx) + defer cancel() + versions, err := b.ListProtocVersions(ctx) if err != nil { return "", err @@ -208,7 +221,7 @@ func (b *BasicPluginManager) GetProtocLatestVersion(ctx context.Context) (string // ListProtocVersions is used to list protoc version func (b *BasicPluginManager) ListProtocVersions(ctx context.Context) ([]string, error) { - ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout) + ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx) defer cancel() b.versionsLock.RLock() @@ -227,14 +240,16 @@ func (b *BasicPluginManager) ListProtocVersions(ctx context.Context) ([]string, return versions, nil } +// GetPathForProtoc is used to get the path for protoc +func (b *BasicPluginManager) GetPathForProtoc(ctx context.Context, version string) (string, error) { + return PathForProtoc(b.storageDir, version), nil +} + // InstallProtoc is used to install protoc of specified version func (b *BasicPluginManager) InstallProtoc(ctx context.Context, version string) (string, error) { - ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout) + ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx) defer cancel() - if strings.HasPrefix(version, "v") { - version = strings.TrimPrefix(version, "v") - } local := PathForProtoc(b.storageDir, version) exists, err := util.IsFileExists(local) if err != nil { @@ -266,6 +281,6 @@ func (b *BasicPluginManager) InstallProtoc(ctx context.Context, version string) } // IncludePath returns the default include path -func (b *BasicPluginManager) IncludePath(ctx context.Context) string { - return PathForInclude(b.storageDir) +func (b *BasicPluginManager) IncludePath(ctx context.Context) (string, error) { + return PathForInclude(b.storageDir), nil } diff --git a/pkg/component/pluginmanager/paths.go b/pkg/component/pluginmanager/paths.go index 967bbaa..f5398d2 100644 --- a/pkg/component/pluginmanager/paths.go +++ b/pkg/component/pluginmanager/paths.go @@ -18,6 +18,7 @@ import ( "net/url" "path" "path/filepath" + "strings" "golang.org/x/mod/module" @@ -31,6 +32,9 @@ func PathForInclude(storageDir string) string { // PathForProtoc is used to get the local binary location where the specified version protoc should be stored func PathForProtoc(storageDir string, version string) string { + if strings.HasPrefix(version, "v") { + version = strings.TrimPrefix(version, "v") + } return filepath.Join(storageDir, "protoc", version, util.GetBinaryFileName("protoc")) } @@ -72,6 +76,7 @@ func PathForPluginDir(storageDir string, path string, version string) (string, e } // PathForPlugin is used to get the binary path of plugin +// Path: e.g "google.golang.org/protobuf/cmd/protoc-gen-go" func PathForPlugin(storageDir string, path string, version string) (string, error) { name := GetGoPkgExecName(path) dir, err := PathForPluginDir(storageDir, path, version) diff --git a/pkg/component/pluginmanager/protoc.go b/pkg/component/pluginmanager/protoc.go index bc2da3f..139b5c7 100644 --- a/pkg/component/pluginmanager/protoc.go +++ b/pkg/component/pluginmanager/protoc.go @@ -53,6 +53,9 @@ func (p *ProtocRelease) Clear() error { // GetProtocRelease is used to download protoc release func GetProtocRelease(ctx context.Context, version string) (*ProtocRelease, error) { + if strings.HasPrefix(version, "v") { + version = strings.TrimPrefix(version, "v") + } workspace, err := os.MkdirTemp("", "") if err != nil { return nil, err diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index 41434f7..ffa2997 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -31,9 +31,10 @@ const ( KeyNamePowerProtocInclude = "POWERPROTO_INCLUDE" // The default include can be referenced by this key in import paths KeyPowerProtoInclude = "$" + KeyNamePowerProtocInclude + KeyNameSourceRelative = "SOURCE_RELATIVE" // KeySourceRelative can be specified in import paths to refer to // the folder where the current proto file is located - KeySourceRelative = "$SOURCE_RELATIVE" + KeySourceRelative = "$" + KeyNameSourceRelative // Defines the program directory of PowerProto, including various binary and include files EnvHomeDir = "POWERPROTO_HOME" // ProtobufRepository defines the protobuf repository diff --git a/pkg/consts/context.go b/pkg/consts/context.go index d71b64f..59f720f 100644 --- a/pkg/consts/context.go +++ b/pkg/consts/context.go @@ -16,12 +16,31 @@ package consts import ( "context" + "time" ) type debugMode struct{} type dryRun struct{} type ignoreDryRun struct{} type disableAction struct{} +type perCommandTimeout struct{} + +func GetContextWithPerCommandTimeout(ctx context.Context) (context.Context, context.CancelFunc) { + val := ctx.Value(perCommandTimeout{}) + if val == nil { + return ctx, func() {} + } + duration, ok := val.(time.Duration) + if !ok { + return ctx, func() {} + } + return context.WithTimeout(ctx, duration) +} + +// WithPerCommandTimeout is used to inject per command timeout +func WithPerCommandTimeout(ctx context.Context, timeout time.Duration) context.Context { + return context.WithValue(ctx, perCommandTimeout{}, timeout) +} // WithDebugMode is used to set debug mode func WithDebugMode(ctx context.Context) context.Context { diff --git a/pkg/util/util.go b/pkg/util/util.go index cd958a0..16fdbc6 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -117,9 +117,9 @@ func GetExitCode(err error) int { var regexpEnvironmentVar = regexp.MustCompile(`\$[A-Za-z_]+`) -// RenderPathWithEnv is used to render path with environment -func RenderPathWithEnv(path string, ext map[string]string) string { - matches := regexpEnvironmentVar.FindAllString(path, -1) +// RenderWithEnv is used to render string with env +func RenderWithEnv(s string, ext map[string]string) string { + matches := regexpEnvironmentVar.FindAllString(s, -1) for _, match := range matches { key := match[1:] val := ext[key] @@ -127,10 +127,15 @@ func RenderPathWithEnv(path string, ext map[string]string) string { val = os.Getenv(key) } if val != "" { - path = strings.ReplaceAll(path, match, val) + s = strings.ReplaceAll(s, match, val) } } - return filepath.Clean(path) + return s +} + +// RenderPathWithEnv is used to render path with environment +func RenderPathWithEnv(path string, ext map[string]string) string { + return filepath.Clean(RenderWithEnv(path, ext)) } // SplitGoPackageVersion is used to split go package version