Skip to content

Commit

Permalink
[#89] Add groups of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kozmod committed Feb 5, 2024
1 parent 7e2fd24 commit c82ca43
Show file tree
Hide file tree
Showing 13 changed files with 500 additions and 319 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ out/
cover.out
progen
main

progen.yaml
progen.yml
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ cmd2:
```

```console
% progen -v -dr -f progen.yml -skip=^dirs$$ -skip=cmd.+
% progen -v -dr -f progen.yml -skip=^dirs$ -skip=cmd.+
2023-02-05 14:18:11 INFO application working directory: /Users/user_1/GoProjects/service
2023-02-05 14:18:11 INFO configuration file: progen.yml
2023-02-05 14:18:11 INFO action tag will be skipped: cmd1
Expand Down
108 changes: 101 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package config

import (
"net/url"
"strings"

"github.com/kozmod/progen/internal/entity"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

"github.com/kozmod/progen/internal/entity"
"net/url"
"strings"
)

const (
Expand All @@ -27,7 +25,8 @@ type Config struct {
}

type Settings struct {
HTTP *HTTPClient `yaml:"http"`
HTTP *HTTPClient `yaml:"http"`
Groups Groups `yaml:"groups"`
}

type HTTPClient struct {
Expand All @@ -36,6 +35,42 @@ type HTTPClient struct {
Debug bool `yaml:"debug"`
}

type Groups []Group

func (g Groups) ManualActions() map[string]struct{} {
manual := make(map[string]struct{})
for _, group := range g {
if !group.Manual {
continue
}
for _, action := range group.Actions {
manual[action] = struct{}{}
}
}
return manual
}

func (g Groups) GroupByAction() map[string]map[string]struct{} {
manual := make(map[string]map[string]struct{})
for _, group := range g {
for _, action := range group.Actions {
groups, ok := manual[action]
if !ok {
groups = make(map[string]struct{})
}
groups[group.Name] = struct{}{}
manual[action] = groups
}
}
return manual
}

type Group struct {
Name string `yaml:"name"`
Actions []string `yaml:"actions"`
Manual bool `yaml:"manual"`
}

type Section[T any] struct {
Line int32
Tag string
Expand Down Expand Up @@ -114,7 +149,27 @@ func (addr *AddrURL) UnmarshalYAML(unmarshal func(any) error) error {
return nil
}

func ValidateFile(file File) error {
func (c Config) Validate() error {
for i, files := range c.Files {
for _, file := range files.Val {
err := validateFile(file)
if err != nil {
return xerrors.Errorf("files: %d [%s]: %w", i, file.Path, err)
}
}
}

if err := validateGroups(c.Settings.Groups); err != nil {
return xerrors.Errorf("groups: %w", err)
}

if err := validateConfigSections(c); err != nil {
return xerrors.Errorf("sections: %w", err)
}
return nil
}

func validateFile(file File) error {
notNil := entity.NotNilValues(file.Get, file.Data, file.Local)
switch {
case notNil == 0:
Expand All @@ -124,5 +179,44 @@ func ValidateFile(file File) error {
case strings.TrimSpace(file.Path) == entity.Empty:
return xerrors.Errorf("files: save `path` are empty")
}

return nil
}

func validateGroups(groups Groups) error {
var (
groupNameSet = make(map[string]int, len(groups))
groupNames = make([]string, 0, len(groups))
)
for _, group := range groups {
var (
name = group.Name
quantity, ok = groupNameSet[name]
)
if ok && quantity == 1 {
groupNames = append(groupNames, name)
}
groupNameSet[name] = groupNameSet[name] + 1
}

if len(groupNames) > 0 {
return xerrors.Errorf("duplicate names [%s]", strings.Join(groupNames, entity.LogSliceSep))
}
return nil
}

func validateConfigSections(conf Config) error {
var (
files = len(conf.Files)
dirs = len(conf.Dirs)
cmd = len(conf.Cmd)
fs = len(conf.FS)
)
if files == 0 && dirs == 0 && cmd == 0 && fs == 0 {
return xerrors.Errorf(
"config not contains executable actions [dirs: %d, files: %d, cms: %d, fs: %d]",
dirs, files, cmd, fs,
)
}
return nil
}
Loading

0 comments on commit c82ca43

Please sign in to comment.