From 538ff9c6f6059efb320d8921a4c3fcb069ca4015 Mon Sep 17 00:00:00 2001 From: Technofab Date: Sun, 4 Dec 2022 20:25:41 +0100 Subject: [PATCH] feat: add tags feature --- cmd/powerproto/subcommands/build/command.go | 6 ++++-- pkg/bootstraps/build.go | 17 +++++++---------- pkg/bootstraps/tidy.go | 4 ++-- pkg/component/compilermanager/manager.go | 6 +++--- pkg/component/configmanager/manager.go | 14 +++++++++++--- pkg/configs/configs.go | 1 + 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/cmd/powerproto/subcommands/build/command.go b/cmd/powerproto/subcommands/build/command.go index 664915e..3f6f01e 100644 --- a/cmd/powerproto/subcommands/build/command.go +++ b/cmd/powerproto/subcommands/build/command.go @@ -52,6 +52,7 @@ func CommandBuild(log logger.Logger) *cobra.Command { var dryRun bool var debugMode bool var postScriptEnabled bool + var tags []string perCommandTimeout := time.Second * 300 cmd := &cobra.Command{ Use: "build [dir|proto file]", @@ -108,12 +109,12 @@ func CommandBuild(log logger.Logger) *cobra.Command { log.LogWarn(nil, "no file to compile") return } - if err := bootstraps.StepTidyConfig(ctx, targets); err != nil { + if err := bootstraps.StepTidyConfig(ctx, targets, tags); err != nil { log.LogFatal(nil, "failed to tidy config: %+v", err) return } - if err := bootstraps.Compile(ctx, targets); err != nil { + if err := bootstraps.Compile(ctx, targets, tags); err != nil { log.LogFatal(nil, "failed to compile: %+v", err) } @@ -126,5 +127,6 @@ func CommandBuild(log logger.Logger) *cobra.Command { 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") + flags.StringSliceVar(&tags, "tags", tags, "filter configs by tag") return cmd } diff --git a/pkg/bootstraps/build.go b/pkg/bootstraps/build.go index c4c8675..1d5b1da 100644 --- a/pkg/bootstraps/build.go +++ b/pkg/bootstraps/build.go @@ -33,17 +33,13 @@ import ( ) // StepLookUpConfigs is used to lookup config files according to target proto files -func StepLookUpConfigs( - ctx context.Context, - targets []string, - configManager configmanager.ConfigManager, -) ([]configs.ConfigItem, error) { +func StepLookUpConfigs(ctx context.Context, targets []string, tags []string, configManager configmanager.ConfigManager) ([]configs.ConfigItem, error) { progress := progressbar.GetProgressBar(ctx, len(targets)) progress.SetPrefix("Lookup configs of proto files") var configItems []configs.ConfigItem deduplicate := map[string]struct{}{} for _, target := range targets { - cfg, err := configManager.GetConfig(ctx, target) + cfg, err := configManager.GetConfig(ctx, target, tags) if err != nil { return nil, err } @@ -233,6 +229,7 @@ func StepInstallPlugins(ctx context.Context, func StepCompile(ctx context.Context, compilerManager compilermanager.CompilerManager, targets []string, + tags []string, ) error { progress := progressbar.GetProgressBar(ctx, len(targets)) progress.SetPrefix("Compile Proto Files") @@ -241,7 +238,7 @@ func StepCompile(ctx context.Context, func(target string) { c.Go(func(ctx context.Context) error { progress.SetSuffix(target) - comp, err := compilerManager.GetCompiler(ctx, target) + comp, err := compilerManager.GetCompiler(ctx, target, tags) if err != nil { return err } @@ -295,7 +292,7 @@ func StepPostShell(ctx context.Context, } // Compile is used to compile proto files -func Compile(ctx context.Context, targets []string) error { +func Compile(ctx context.Context, targets []string, tags []string) error { log := logger.NewDefault("compile") log.SetLogLevel(logger.LevelInfo) if consts.IsDebugMode(ctx) { @@ -319,7 +316,7 @@ func Compile(ctx context.Context, targets []string) error { return err } - configItems, err := StepLookUpConfigs(ctx, targets, configManager) + configItems, err := StepLookUpConfigs(ctx, targets, tags, configManager) if err != nil { return err } @@ -333,7 +330,7 @@ func Compile(ctx context.Context, targets []string) error { if err := StepInstallPlugins(ctx, pluginManager, configItems); err != nil { return err } - if err := StepCompile(ctx, compilerManager, targets); err != nil { + if err := StepCompile(ctx, compilerManager, targets, tags); err != nil { return err } diff --git a/pkg/bootstraps/tidy.go b/pkg/bootstraps/tidy.go index 89121a3..f27acd6 100644 --- a/pkg/bootstraps/tidy.go +++ b/pkg/bootstraps/tidy.go @@ -29,7 +29,7 @@ import ( ) // StepTidyConfig is used to tidy configs by proto file targets -func StepTidyConfig(ctx context.Context, targets []string) error { +func StepTidyConfig(ctx context.Context, targets []string, tags []string) error { log := logger.NewDefault("tidy") log.SetLogLevel(logger.LevelInfo) if consts.IsDebugMode(ctx) { @@ -47,7 +47,7 @@ func StepTidyConfig(ctx context.Context, targets []string) error { configPaths := map[string]struct{}{} for _, target := range targets { - cfg, err := configManager.GetConfig(ctx, target) + cfg, err := configManager.GetConfig(ctx, target, tags) if err != nil { return err } diff --git a/pkg/component/compilermanager/manager.go b/pkg/component/compilermanager/manager.go index 71c5f35..812c4cb 100644 --- a/pkg/component/compilermanager/manager.go +++ b/pkg/component/compilermanager/manager.go @@ -26,7 +26,7 @@ import ( // CompilerManager is to manage compiler type CompilerManager interface { // GetCompiler is used to get compiler of specified proto file path - GetCompiler(ctx context.Context, protoFilePath string) (Compiler, error) + GetCompiler(ctx context.Context, protoFilePath string, tags []string) (Compiler, error) } // BasicCompilerManager is the basic implement of CompilerManager @@ -66,8 +66,8 @@ func NewBasicCompilerManager(ctx context.Context, } // GetCompiler is used to get compiler of specified proto file path -func (b *BasicCompilerManager) GetCompiler(ctx context.Context, protoFilePath string) (Compiler, error) { - config, err := b.configManager.GetConfig(ctx, protoFilePath) +func (b *BasicCompilerManager) GetCompiler(ctx context.Context, protoFilePath string, tags []string) (Compiler, error) { + config, err := b.configManager.GetConfig(ctx, protoFilePath, tags) if err != nil { return nil, err } diff --git a/pkg/component/configmanager/manager.go b/pkg/component/configmanager/manager.go index 94632dd..dc77f90 100644 --- a/pkg/component/configmanager/manager.go +++ b/pkg/component/configmanager/manager.go @@ -30,7 +30,7 @@ import ( // ConfigManager is used to manage config type ConfigManager interface { // GetCompiler is used to get config of specified proto file path - GetConfig(ctx context.Context, protoFilePath string) (configs.ConfigItem, error) + GetConfig(ctx context.Context, protoFilePath string, tags []string) (configs.ConfigItem, error) } // NewConfigManager is used to create ConfigManager @@ -55,7 +55,7 @@ func NewBasicConfigManager(log logger.Logger) (*BasicConfigManager, error) { } // GetConfig is used to get config of specified proto file path -func (b *BasicConfigManager) GetConfig(ctx context.Context, protoFilePath string) (configs.ConfigItem, error) { +func (b *BasicConfigManager) GetConfig(ctx context.Context, protoFilePath string, tags []string) (configs.ConfigItem, error) { possiblePath := configs.ListConfigPaths(filepath.Dir(protoFilePath)) for _, configFilePath := range possiblePath { items, err := b.loadConfig(configFilePath) @@ -67,7 +67,15 @@ func (b *BasicConfigManager) GetConfig(ctx context.Context, protoFilePath string for _, scope := range config.Config().Scopes { scopePath := filepath.Join(dir, scope) if strings.Contains(protoFilePath, scopePath) { - return config, nil + if len(tags) == 0 { + return config, nil + } + b.Logger.LogInfo(nil, "tags %s passed, filtering...", tags) + for _, tag := range tags { + if strings.Contains(tag, config.Config().Tag) { + return config, nil + } + } } } } diff --git a/pkg/configs/configs.go b/pkg/configs/configs.go index 3537fde..8e7f7b6 100644 --- a/pkg/configs/configs.go +++ b/pkg/configs/configs.go @@ -28,6 +28,7 @@ import ( // Config defines the config model type Config struct { + Tag string `json:"tag" yaml:"tag"` Scopes []string `json:"scopes" yaml:"scopes"` Protoc string `json:"protoc" yaml:"protoc"` ProtocWorkDir string `json:"protocWorkDir" yaml:"protocWorkDir"`