feat: add tags feature

This commit is contained in:
Technofab 2022-12-04 20:25:41 +01:00
parent f394814c2e
commit 538ff9c6f6
No known key found for this signature in database
GPG key ID: A0AA746B951C8830
6 changed files with 28 additions and 20 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}
}
}
}
}

View file

@ -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"`