Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
kozmod committed Jul 10, 2024
1 parent 357456b commit cd0391a
Show file tree
Hide file tree
Showing 16 changed files with 725 additions and 274 deletions.
68 changes: 67 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,72 @@ type Config struct {
FS []Section[[]string] `yaml:"fs,flow"`
}

func (c Config) CommandActions() []entity.Action[[]entity.Command] {
return toActionsSlice(c.Cmd, func(cmd Command) entity.Command {
return entity.Command{
Cmd: cmd.Exec,
Args: cmd.Args,
Dir: cmd.Dir,
}
})
}

func (c Config) FilesActions() []entity.Action[[]entity.UndefinedFile] {
return toActionsSlice(c.Files, func(file File) entity.UndefinedFile {
uFile := entity.UndefinedFile{
Path: file.Path,
}
if file.Data != nil {
*uFile.Data = *file.Data
}
if get := file.Get; get != nil {
uFile.Get = &entity.HTTPClientParams{
URL: get.URL,
Headers: get.Headers,
QueryParams: get.QueryParams,
}
}
if file.Local != nil {
uFile.Local = file.Local
}
return uFile
})
}

func (c Config) DirActions() []entity.Action[[]string] {
return toActionsSlice(c.Dirs, func(dir string) string {
return dir
})
}

func (c Config) RmActions() []entity.Action[[]string] {
return toActionsSlice(c.Rm, func(rms string) string {
return rms
})
}

func (c Config) FsActions() []entity.Action[[]string] {
return toActionsSlice(c.FS, func(fss string) string {
return fss
})
}

func toActionsSlice[S any, T any](sections []Section[[]S], mapFn func(s S) T) []entity.Action[[]T] {
actions := make([]entity.Action[[]T], len(sections))
for i, section := range sections {
action := entity.Action[[]T]{
Priority: section.Line,
Name: section.Tag,
Val: make([]T, len(section.Val)),
}
for j, val := range section.Val {
action.Val[j] = mapFn(val)
}
actions[i] = action
}
return actions
}

type Settings struct {
HTTP *HTTPClient `yaml:"http"`
Groups Groups `yaml:"groups"`
Expand Down Expand Up @@ -76,7 +142,7 @@ type Group struct {
}

type Section[T any] struct {
Line int32
Line int
Tag string
Val T
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (u *YamlUnmarshaler) Unmarshal(rawConfig []byte) (Config, error) {
}

func decode[T any](target []Section[T], node yaml.Node, tag string) ([]Section[T], error) {
section := Section[T]{Line: int32(node.Line), Tag: tag}
section := Section[T]{Line: node.Line, Tag: tag}
err := node.Decode(&section.Val)
target = append(target, section)
return target, err
Expand Down
34 changes: 34 additions & 0 deletions internal/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,42 @@ type (
Debugf(format string, any ...any)
Fatalf(format string, any ...any)
}

ActionFilter interface {
MatchString(s string) bool
}
)

type ExecutorBuilder struct {
Action string
Priority int
ProcFn func() (Executor, error)
}

type Group struct {
Name string
Actions []string
Manual bool
}

type Action[T any] struct {
Priority int
Name string
Val T
}

func (a Action[T]) WithPriority(priority int) Action[T] {
a.Priority = priority
return a
}

type UndefinedFile struct {
Path string
Data *[]byte
Get *HTTPClientParams
Local *string
}

type DataFile struct {
FileInfo
Data []byte
Expand Down
13 changes: 13 additions & 0 deletions internal/entity/v.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package entity

type AppendVPlusOrV func(s string) string

func NewAppendVPlusOrV(vPlus bool) AppendVPlusOrV {
return func(s string) string {
const vp, v = "%+v", "%v"
if vPlus {
return s + vp
}
return s + v
}
}
66 changes: 54 additions & 12 deletions internal/exec/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,77 @@ package exec

import (
"golang.org/x/xerrors"
"sync"

"github.com/kozmod/progen/internal/entity"
)

type Chain struct {
executors []entity.Executor
}

func NewChain(executors []entity.Executor) *Chain {
return &Chain{executors: executors}
}

func (c *Chain) Exec() error {
for i, executor := range c.executors {
err := executor.Exec()
if err != nil {
return xerrors.Errorf("execute proc [%d]: %w", i, err)
}
}
return nil
}

type PreprocessingChain struct {
preprocessors []entity.Preprocessor
executors []entity.Executor
preprocessors *Preprocessors
chain *Chain
}

func NewPreprocessingChain(preprocessors []entity.Preprocessor, executors []entity.Executor) *PreprocessingChain {
func NewPreprocessingChain(preprocessors *Preprocessors, executors []entity.Executor) *PreprocessingChain {
return &PreprocessingChain{
preprocessors: preprocessors,
executors: executors,
chain: NewChain(executors),
}
}

func (c *PreprocessingChain) Exec() error {
for i, preprocessor := range c.preprocessors {
for i, preprocessor := range c.preprocessors.Get() {
err := preprocessor.Process()
if err != nil {
return xerrors.Errorf("preload [%d]: %w", i, err)
return xerrors.Errorf("preprocess [%d]: %w", i, err)
}
}
return c.chain.Exec()
}

type Preprocessors struct {
mx sync.RWMutex
val []entity.Preprocessor
}

func (p *Preprocessors) Add(in ...entity.Preprocessor) {
if p == nil {
return
}

for i, executor := range c.executors {
err := executor.Exec()
if err != nil {
return xerrors.Errorf("execute proc [%d]: %w", i, err)
}
p.mx.Lock()
defer p.mx.Unlock()
p.val = append(p.val, in...)
}

func (p *Preprocessors) Get() []entity.Preprocessor {
if p == nil {
return nil
}
return nil

p.mx.RLock()
defer p.mx.RUnlock()
if len(p.val) == 0 {
return nil
}
res := make([]entity.Preprocessor, len(p.val), len(p.val))
copy(res, p.val)
return res
}
4 changes: 2 additions & 2 deletions internal/exec/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (e *FilesExecutor) Exec() error {
return xerrors.Errorf("execute file: get file: %w", err)
}

for _, processor := range e.strategies {
file, err = processor.Apply(file)
for _, strategy := range e.strategies {
file, err = strategy.Apply(file)
if err != nil {
return xerrors.Errorf("execute file: process file: %w", err)
}
Expand Down
21 changes: 11 additions & 10 deletions internal/factory/action_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"github.com/kozmod/progen/internal/entity"
)

type (
actionFilter interface {
MatchString(s string) bool
}
)
type DummyActionFilter struct {
}

func (f DummyActionFilter) MatchString(_ string) bool {
return true
}

type ActionFilter struct {
skipFilter actionFilter
type FacadeActionFilter struct {
skipFilter entity.ActionFilter
selectedGroups map[string]struct{}
groupsByAction map[string]map[string]struct{}
manualActions map[string]struct{}
Expand All @@ -29,7 +30,7 @@ func NewActionFilter(
manualActionsSet map[string]struct{},
logger entity.Logger,

) *ActionFilter {
) *FacadeActionFilter {
selectedGroupsSet := entity.SliceSet(selectedGroups)
skipActions = slices.Compact(skipActions)

Expand All @@ -44,7 +45,7 @@ func NewActionFilter(
logger.Infof("manual actions will be skipped: [%s]", strings.Join(manualActions, entity.LogSliceSep))
}

return &ActionFilter{
return &FacadeActionFilter{
skipFilter: entity.NewRegexpChain(skipActions...),
selectedGroups: selectedGroupsSet,
groupsByAction: groupsByAction,
Expand All @@ -53,7 +54,7 @@ func NewActionFilter(
}
}

func (f *ActionFilter) MatchString(action string) bool {
func (f *FacadeActionFilter) MatchString(action string) bool {
if f.skipFilter.MatchString(action) {
f.logger.Infof("action will be skipped: [%s]", action)
return false
Expand Down
Loading

0 comments on commit cd0391a

Please sign in to comment.