Skip to content

Commit 3054cba

Browse files
refactor: Reduce nesting
1 parent 49614f5 commit 3054cba

16 files changed

Lines changed: 478 additions & 399 deletions

File tree

build/ci/golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ linters:
149149
ignore-rules:
150150
- Statuser
151151
nestif:
152-
min-complexity: 7
152+
min-complexity: 6
153153
staticcheck:
154154
checks:
155155
- all

internal/pkg/diff/reporter.go

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,47 +43,49 @@ func (r *Reporter) PushStep(ps cmp.PathStep) {
4343
}
4444

4545
func (r *Reporter) Report(rs cmp.Result) {
46-
if !rs.Equal() {
47-
var lines []string
48-
remoteValue, localValue := r.path.Last().Values()
49-
50-
// Diff values
51-
if v, ok := r.relationsDiff(remoteValue, localValue); ok {
52-
// Relations diff
53-
lines = append(lines, v...)
54-
} else if v, ok := r.stringsDiff(remoteValue, localValue); ok {
55-
// Strings diff
56-
lines = append(lines, v...)
57-
} else {
58-
// Other types
59-
lines = append(lines, valuesDiff(remoteValue, localValue)...)
60-
}
46+
if rs.Equal() {
47+
return
48+
}
6149

62-
// Format lines
63-
var mark string
64-
switch {
65-
case remoteValue.IsValid() && !localValue.IsValid():
66-
mark = OnlyInRemoteMark
67-
case !remoteValue.IsValid() && localValue.IsValid():
68-
mark = OnlyInLocalMark
69-
default:
70-
// Values are present in both: local and remote value
71-
// Individual lines are already marked.
72-
mark = " "
73-
}
74-
pathStr := r.pathToString(r.path)
75-
if len(pathStr) > 0 {
76-
r.paths = append(r.paths, pathStr)
77-
if !r.isPathHidden() {
78-
r.diffs = append(r.diffs, fmt.Sprintf(`%s %s:`, mark, pathStr))
79-
// Ident values inside path
80-
mark += ` `
81-
}
82-
}
83-
for _, line := range lines {
84-
r.diffs = append(r.diffs, fmt.Sprintf(`%s %s`, mark, line))
50+
var lines []string
51+
remoteValue, localValue := r.path.Last().Values()
52+
53+
// Diff values
54+
if v, ok := r.relationsDiff(remoteValue, localValue); ok {
55+
// Relations diff
56+
lines = append(lines, v...)
57+
} else if v, ok := r.stringsDiff(remoteValue, localValue); ok {
58+
// Strings diff
59+
lines = append(lines, v...)
60+
} else {
61+
// Other types
62+
lines = append(lines, valuesDiff(remoteValue, localValue)...)
63+
}
64+
65+
// Format lines
66+
var mark string
67+
switch {
68+
case remoteValue.IsValid() && !localValue.IsValid():
69+
mark = OnlyInRemoteMark
70+
case !remoteValue.IsValid() && localValue.IsValid():
71+
mark = OnlyInLocalMark
72+
default:
73+
// Values are present in both: local and remote value
74+
// Individual lines are already marked.
75+
mark = " "
76+
}
77+
pathStr := r.pathToString(r.path)
78+
if len(pathStr) > 0 {
79+
r.paths = append(r.paths, pathStr)
80+
if !r.isPathHidden() {
81+
r.diffs = append(r.diffs, fmt.Sprintf(`%s %s:`, mark, pathStr))
82+
// Ident values inside path
83+
mark += ` `
8584
}
8685
}
86+
for _, line := range lines {
87+
r.diffs = append(r.diffs, fmt.Sprintf(`%s %s`, mark, line))
88+
}
8789
}
8890

8991
func (r *Reporter) PopStep() {
@@ -185,15 +187,22 @@ func (r *Reporter) pathToString(path cmp.Path) string {
185187
}
186188

187189
func (r *Reporter) objectPath(value reflect.Value) string {
188-
if value.IsValid() {
189-
if v, ok := value.Interface().(model.RecordPaths); ok {
190-
objectPath := v.Path()
191-
if objectPath != "" {
192-
if v, err := filesystem.Rel(r.manifest.Path(), objectPath); err == nil {
193-
return v
194-
}
195-
}
196-
}
190+
if !value.IsValid() {
191+
return ""
192+
}
193+
194+
v, ok := value.Interface().(model.RecordPaths)
195+
if !ok {
196+
return ""
197+
}
198+
199+
objectPath := v.Path()
200+
if objectPath == "" {
201+
return ""
202+
}
203+
204+
if v, err := filesystem.Rel(r.manifest.Path(), objectPath); err == nil {
205+
return v
197206
}
198207
return ""
199208
}

internal/pkg/plan/diffop/plan.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,27 @@ func (p *Plan) Log(w io.Writer) {
5151

5252
if len(actions) == 0 {
5353
fmt.Fprintln(w, " no difference")
54-
} else {
55-
skippedDeleteCount := 0
56-
for _, action := range actions {
57-
msg := action.String()
58-
if !p.allowedRemoteDelete && action.action == ActionDeleteRemote {
59-
// determine if it is ignored or skipped
60-
if action.IsIgnored() {
61-
msg += " - IGNORED"
62-
} else {
63-
msg += " - SKIPPED"
64-
}
65-
66-
skippedDeleteCount++
54+
return
55+
}
56+
57+
skippedDeleteCount := 0
58+
for _, action := range actions {
59+
msg := action.String()
60+
if !p.allowedRemoteDelete && action.action == ActionDeleteRemote {
61+
// determine if it is ignored or skipped
62+
if action.IsIgnored() {
63+
msg += " - IGNORED"
64+
} else {
65+
msg += " - SKIPPED"
6766
}
68-
fmt.Fprintln(w, " "+msg)
69-
}
7067

71-
if skippedDeleteCount > 0 {
72-
fmt.Fprintln(w, "Skipped remote objects deletion, use \"--force\" to delete them.")
68+
skippedDeleteCount++
7369
}
70+
fmt.Fprintln(w, " "+msg)
71+
}
72+
73+
if skippedDeleteCount > 0 {
74+
fmt.Fprintln(w, "Skipped remote objects deletion, use \"--force\" to delete them.")
7475
}
7576
}
7677

internal/pkg/service/cli/cmd/template/test/run/cmd.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/keboola/keboola-as-code/internal/pkg/service/cli/helpmsg"
99
"github.com/keboola/keboola-as-code/internal/pkg/service/common/configmap"
1010
"github.com/keboola/keboola-as-code/internal/pkg/template"
11+
"github.com/keboola/keboola-as-code/internal/pkg/template/repository"
1112
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
1213
testOp "github.com/keboola/keboola-as-code/pkg/lib/operation/template/local/test/run"
1314
)
@@ -49,34 +50,9 @@ func Command(p dependencies.Provider) *cobra.Command {
4950
}
5051

5152
// Load templates
52-
templates := make([]*template.Template, 0)
53-
if len(args) >= 1 {
54-
// Optional version argument
55-
var versionArg string
56-
if len(args) > 1 {
57-
versionArg = args[1]
58-
}
59-
tmpl, err := d.TemplateForTests(cmd.Context(), model.NewTemplateRef(repo.Definition(), args[0], versionArg), f.TestProjectsFile.Value)
60-
if err != nil {
61-
return errors.Errorf(`loading test for template "%s" failed: %w`, args[0], err)
62-
}
63-
templates = append(templates, tmpl)
64-
} else {
65-
for _, t := range repo.Templates() {
66-
// Don't test deprecated templates
67-
if t.Deprecated {
68-
continue
69-
}
70-
v, err := t.DefaultVersionOrErr()
71-
if err != nil {
72-
return errors.Errorf(`loading default version for template "%s" failed: %w`, t.ID, err)
73-
}
74-
tmpl, err := d.TemplateForTests(cmd.Context(), model.NewTemplateRef(repo.Definition(), t.ID, v.Version.String()), f.TestProjectsFile.Value)
75-
if err != nil {
76-
return errors.Errorf(`loading test for template "%s" failed: %w`, t.ID, err)
77-
}
78-
templates = append(templates, tmpl)
79-
}
53+
templates, err := loadTemplates(cmd, args, d, repo, f)
54+
if err != nil {
55+
return err
8056
}
8157

8258
// Options
@@ -103,3 +79,37 @@ func Command(p dependencies.Provider) *cobra.Command {
10379

10480
return cmd
10581
}
82+
83+
func loadTemplates(cmd *cobra.Command, args []string, d dependencies.LocalCommandScope, repo *repository.Repository, f Flags) ([]*template.Template, error) {
84+
templates := make([]*template.Template, 0)
85+
if len(args) >= 1 {
86+
// Optional version argument
87+
var versionArg string
88+
if len(args) > 1 {
89+
versionArg = args[1]
90+
}
91+
tmpl, err := d.TemplateForTests(cmd.Context(), model.NewTemplateRef(repo.Definition(), args[0], versionArg), f.TestProjectsFile.Value)
92+
if err != nil {
93+
return nil, errors.Errorf(`loading test for template "%s" failed: %w`, args[0], err)
94+
}
95+
templates = append(templates, tmpl)
96+
return templates, nil
97+
}
98+
99+
for _, t := range repo.Templates() {
100+
// Don't test deprecated templates
101+
if t.Deprecated {
102+
continue
103+
}
104+
v, err := t.DefaultVersionOrErr()
105+
if err != nil {
106+
return nil, errors.Errorf(`loading default version for template "%s" failed: %w`, t.ID, err)
107+
}
108+
tmpl, err := d.TemplateForTests(cmd.Context(), model.NewTemplateRef(repo.Definition(), t.ID, v.Version.String()), f.TestProjectsFile.Value)
109+
if err != nil {
110+
return nil, errors.Errorf(`loading test for template "%s" failed: %w`, t.ID, err)
111+
}
112+
templates = append(templates, tmpl)
113+
}
114+
return templates, nil
115+
}

internal/pkg/service/cli/dialog/configs.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,7 @@ func (p *Dialogs) SelectConfig(all []*model.ConfigWithRows, label string, cfg co
3636

3737
func (p *Dialogs) SelectConfigs(all []*model.ConfigWithRows, label string, configs configmap.Value[string]) (results []*model.ConfigWithRows, err error) {
3838
if configs.IsSet() {
39-
// Create configs map
40-
configByKey := make(map[string]*model.ConfigWithRows)
41-
for _, config := range all {
42-
configByKey[fmt.Sprintf(`%s:%s`, config.ComponentID, config.ID)] = config
43-
}
44-
45-
// Parse user input
46-
errs := errors.NewMultiError()
47-
for item := range strings.SplitSeq(configs.Value, `,`) {
48-
item = strings.TrimSpace(item)
49-
if len(item) == 0 {
50-
continue
51-
}
52-
53-
// Check [componentId]:[configId] format
54-
if len(strings.Split(item, `:`)) != 2 {
55-
errs.Append(errors.Errorf(`cannot parse "%s", must be in "[componentId]:[configId]" format`, item))
56-
continue
57-
}
58-
59-
// Map to config
60-
if config, ok := configByKey[item]; ok {
61-
results = append(results, config)
62-
} else {
63-
errs.Append(errors.Errorf(`config "%s" not found`, item))
64-
}
65-
}
39+
results = parseUserInput(all, configs, results)
6640
} else {
6741
indexes, ok := p.MultiSelectIndex(&prompt.MultiSelectIndex{
6842
Label: label,
@@ -83,6 +57,38 @@ func (p *Dialogs) SelectConfigs(all []*model.ConfigWithRows, label string, confi
8357
return results, nil
8458
}
8559

60+
func parseUserInput(all []*model.ConfigWithRows, configs configmap.Value[string], results []*model.ConfigWithRows) []*model.ConfigWithRows {
61+
// Create configs map
62+
configByKey := make(map[string]*model.ConfigWithRows)
63+
for _, config := range all {
64+
configByKey[fmt.Sprintf(`%s:%s`, config.ComponentID, config.ID)] = config
65+
}
66+
67+
// Parse user input
68+
errs := errors.NewMultiError()
69+
for item := range strings.SplitSeq(configs.Value, `,`) {
70+
item = strings.TrimSpace(item)
71+
if len(item) == 0 {
72+
continue
73+
}
74+
75+
// Check [componentId]:[configId] format
76+
if len(strings.Split(item, `:`)) != 2 {
77+
errs.Append(errors.Errorf(`cannot parse "%s", must be in "[componentId]:[configId]" format`, item))
78+
continue
79+
}
80+
81+
// Map to config
82+
if config, ok := configByKey[item]; ok {
83+
results = append(results, config)
84+
} else {
85+
errs.Append(errors.Errorf(`config "%s" not found`, item))
86+
}
87+
}
88+
89+
return results
90+
}
91+
8692
func configsSelectOpts(all []*model.ConfigWithRows) []string {
8793
selectOpts := make([]string, 0)
8894
for _, c := range all {

0 commit comments

Comments
 (0)