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

@ -52,6 +52,7 @@ func CommandBuild(log logger.Logger) *cobra.Command {
var dryRun bool var dryRun bool
var debugMode bool var debugMode bool
var postScriptEnabled bool var postScriptEnabled bool
var tags []string
perCommandTimeout := time.Second * 300 perCommandTimeout := time.Second * 300
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "build [dir|proto file]", Use: "build [dir|proto file]",
@ -108,12 +109,12 @@ func CommandBuild(log logger.Logger) *cobra.Command {
log.LogWarn(nil, "no file to compile") log.LogWarn(nil, "no file to compile")
return 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) log.LogFatal(nil, "failed to tidy config: %+v", err)
return 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) 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(&debugMode, "debug", "d", debugMode, "debug mode")
flags.BoolVarP(&dryRun, "dryRun", "y", dryRun, "dryRun mode") flags.BoolVarP(&dryRun, "dryRun", "y", dryRun, "dryRun mode")
flags.DurationVarP(&perCommandTimeout, "timeout", "t", perCommandTimeout, "execution timeout for per command") flags.DurationVarP(&perCommandTimeout, "timeout", "t", perCommandTimeout, "execution timeout for per command")
flags.StringSliceVar(&tags, "tags", tags, "filter configs by tag")
return cmd return cmd
} }

View file

@ -33,17 +33,13 @@ import (
) )
// StepLookUpConfigs is used to lookup config files according to target proto files // StepLookUpConfigs is used to lookup config files according to target proto files
func StepLookUpConfigs( func StepLookUpConfigs(ctx context.Context, targets []string, tags []string, configManager configmanager.ConfigManager) ([]configs.ConfigItem, error) {
ctx context.Context,
targets []string,
configManager configmanager.ConfigManager,
) ([]configs.ConfigItem, error) {
progress := progressbar.GetProgressBar(ctx, len(targets)) progress := progressbar.GetProgressBar(ctx, len(targets))
progress.SetPrefix("Lookup configs of proto files") progress.SetPrefix("Lookup configs of proto files")
var configItems []configs.ConfigItem var configItems []configs.ConfigItem
deduplicate := map[string]struct{}{} deduplicate := map[string]struct{}{}
for _, target := range targets { for _, target := range targets {
cfg, err := configManager.GetConfig(ctx, target) cfg, err := configManager.GetConfig(ctx, target, tags)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -233,6 +229,7 @@ func StepInstallPlugins(ctx context.Context,
func StepCompile(ctx context.Context, func StepCompile(ctx context.Context,
compilerManager compilermanager.CompilerManager, compilerManager compilermanager.CompilerManager,
targets []string, targets []string,
tags []string,
) error { ) error {
progress := progressbar.GetProgressBar(ctx, len(targets)) progress := progressbar.GetProgressBar(ctx, len(targets))
progress.SetPrefix("Compile Proto Files") progress.SetPrefix("Compile Proto Files")
@ -241,7 +238,7 @@ func StepCompile(ctx context.Context,
func(target string) { func(target string) {
c.Go(func(ctx context.Context) error { c.Go(func(ctx context.Context) error {
progress.SetSuffix(target) progress.SetSuffix(target)
comp, err := compilerManager.GetCompiler(ctx, target) comp, err := compilerManager.GetCompiler(ctx, target, tags)
if err != nil { if err != nil {
return err return err
} }
@ -295,7 +292,7 @@ func StepPostShell(ctx context.Context,
} }
// Compile is used to compile proto files // 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 := logger.NewDefault("compile")
log.SetLogLevel(logger.LevelInfo) log.SetLogLevel(logger.LevelInfo)
if consts.IsDebugMode(ctx) { if consts.IsDebugMode(ctx) {
@ -319,7 +316,7 @@ func Compile(ctx context.Context, targets []string) error {
return err return err
} }
configItems, err := StepLookUpConfigs(ctx, targets, configManager) configItems, err := StepLookUpConfigs(ctx, targets, tags, configManager)
if err != nil { if err != nil {
return err return err
} }
@ -333,7 +330,7 @@ func Compile(ctx context.Context, targets []string) error {
if err := StepInstallPlugins(ctx, pluginManager, configItems); err != nil { if err := StepInstallPlugins(ctx, pluginManager, configItems); err != nil {
return err return err
} }
if err := StepCompile(ctx, compilerManager, targets); err != nil { if err := StepCompile(ctx, compilerManager, targets, tags); err != nil {
return err return err
} }

View file

@ -29,7 +29,7 @@ import (
) )
// StepTidyConfig is used to tidy configs by proto file targets // 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 := logger.NewDefault("tidy")
log.SetLogLevel(logger.LevelInfo) log.SetLogLevel(logger.LevelInfo)
if consts.IsDebugMode(ctx) { if consts.IsDebugMode(ctx) {
@ -47,7 +47,7 @@ func StepTidyConfig(ctx context.Context, targets []string) error {
configPaths := map[string]struct{}{} configPaths := map[string]struct{}{}
for _, target := range targets { for _, target := range targets {
cfg, err := configManager.GetConfig(ctx, target) cfg, err := configManager.GetConfig(ctx, target, tags)
if err != nil { if err != nil {
return err return err
} }

View file

@ -26,7 +26,7 @@ import (
// CompilerManager is to manage compiler // CompilerManager is to manage compiler
type CompilerManager interface { type CompilerManager interface {
// GetCompiler is used to get compiler of specified proto file path // 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 // 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 // GetCompiler is used to get compiler of specified proto file path
func (b *BasicCompilerManager) GetCompiler(ctx context.Context, protoFilePath string) (Compiler, error) { func (b *BasicCompilerManager) GetCompiler(ctx context.Context, protoFilePath string, tags []string) (Compiler, error) {
config, err := b.configManager.GetConfig(ctx, protoFilePath) config, err := b.configManager.GetConfig(ctx, protoFilePath, tags)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -30,7 +30,7 @@ import (
// ConfigManager is used to manage config // ConfigManager is used to manage config
type ConfigManager interface { type ConfigManager interface {
// GetCompiler is used to get config of specified proto file path // 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 // 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 // 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)) possiblePath := configs.ListConfigPaths(filepath.Dir(protoFilePath))
for _, configFilePath := range possiblePath { for _, configFilePath := range possiblePath {
items, err := b.loadConfig(configFilePath) items, err := b.loadConfig(configFilePath)
@ -67,7 +67,15 @@ func (b *BasicConfigManager) GetConfig(ctx context.Context, protoFilePath string
for _, scope := range config.Config().Scopes { for _, scope := range config.Config().Scopes {
scopePath := filepath.Join(dir, scope) scopePath := filepath.Join(dir, scope)
if strings.Contains(protoFilePath, scopePath) { 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 // Config defines the config model
type Config struct { type Config struct {
Tag string `json:"tag" yaml:"tag"`
Scopes []string `json:"scopes" yaml:"scopes"` Scopes []string `json:"scopes" yaml:"scopes"`
Protoc string `json:"protoc" yaml:"protoc"` Protoc string `json:"protoc" yaml:"protoc"`
ProtocWorkDir string `json:"protocWorkDir" yaml:"protocWorkDir"` ProtocWorkDir string `json:"protocWorkDir" yaml:"protocWorkDir"`