Skip to content

Commit

Permalink
Use slices.Contains for fun and profit
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjohansson committed Sep 4, 2023
1 parent 7fd671c commit b6ac7f5
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 131 deletions.
8 changes: 2 additions & 6 deletions linters/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linters
import (
"fmt"
"github.com/springernature/halfpipe/manifest"
"golang.org/x/exp/slices"
"regexp"
)

Expand Down Expand Up @@ -42,10 +43,5 @@ func LintArtifacts(currentTask manifest.Task, previousTasks []manifest.Task) (er
}

func previousTasksSavesArtifact(tasks []manifest.Task) bool {
for _, task := range tasks {
if task.SavesArtifacts() {
return true
}
}
return false
return slices.ContainsFunc(tasks, func(t manifest.Task) bool { return t.SavesArtifacts() })
}
8 changes: 2 additions & 6 deletions linters/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package linters

import (
"errors"
"golang.org/x/exp/slices"
"testing"
)

Expand All @@ -20,10 +21,5 @@ func assertNotContainsError(t *testing.T, errs []error, expected error) {
}

func containsError(errs []error, expected error) bool {
for _, e := range errs {
if errors.Is(e, expected) {
return true
}
}
return false
return slices.ContainsFunc(errs, func(e error) bool { return errors.Is(e, expected) })
}
8 changes: 2 additions & 6 deletions linters/feature_toggles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package linters

import (
"github.com/springernature/halfpipe/manifest"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -39,10 +40,5 @@ func (f featureToggleLinter) Lint(manifest manifest.Manifest) (result LintResult
}

func (f featureToggleLinter) featureInAvailableFeatures(feature string) bool {
for _, availableFeature := range f.availableFeatures {
if feature == availableFeature {
return true
}
}
return false
return slices.ContainsFunc(f.availableFeatures, func(availableFeature string) bool { return availableFeature == feature })
}
12 changes: 2 additions & 10 deletions linters/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ package linters
import (
"fmt"
"github.com/springernature/halfpipe/manifest"
"golang.org/x/exp/slices"
"strings"
)

func contains(allowedStatus []string, status string) bool {
for _, a := range allowedStatus {
if a == status {
return true
}
}
return false
}

func LintPipelineTrigger(man manifest.Manifest, pipeline manifest.PipelineTrigger) (errs []error) {
if man.Team != pipeline.Team {
errs = append(errs, NewErrInvalidField("team", fmt.Sprintf("you can only trigger on pipelines in your team, '%s'!", man.Team)))
Expand All @@ -32,7 +24,7 @@ func LintPipelineTrigger(man manifest.Manifest, pipeline manifest.PipelineTrigge
}

allowedStatus := []string{"succeeded", "failed", "errored", "aborted"}
if !contains(allowedStatus, pipeline.Status) {
if !slices.Contains(allowedStatus, pipeline.Status) {
errs = append(errs, NewErrInvalidField("status", fmt.Sprintf("must be one of %s", strings.Join(allowedStatus, ", "))))
}
return errs
Expand Down
29 changes: 5 additions & 24 deletions linters/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@ import (
"errors"
"fmt"
"github.com/gookit/color"
"golang.org/x/exp/slices"
)

type LintResults []LintResult

func (lrs LintResults) HasWarnings() bool {
for _, lintResult := range lrs {
if lintResult.HasWarnings() {
return true
}
}
return false
return slices.ContainsFunc(lrs, func(lr LintResult) bool { return lr.HasWarnings() })
}

func (lrs LintResults) HasErrors() bool {
for _, lintResult := range lrs {
if lintResult.HasErrors() {
return true
}
}
return false
return slices.ContainsFunc(lrs, func(lr LintResult) bool { return lr.HasErrors() })
}

func (lrs LintResults) Error() (out string) {
Expand Down Expand Up @@ -65,21 +56,11 @@ func (lr *LintResult) Error() (out string) {
}

func (lr *LintResult) HasErrors() bool {
for _, e := range lr.Issues {
if !isWarning(e) {
return true
}
}
return false
return slices.ContainsFunc(lr.Issues, func(e error) bool { return !isWarning(e) })
}

func (lr *LintResult) HasWarnings() bool {
for _, e := range lr.Issues {
if isWarning(e) {
return true
}
}
return false
return slices.ContainsFunc(lr.Issues, func(e error) bool { return isWarning(e) })
}

func isWarning(e error) bool {
Expand Down
15 changes: 3 additions & 12 deletions manifest/deploy_cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifest

import (
"code.cloudfoundry.org/cli/util/manifestparser"
"golang.org/x/exp/slices"
"strings"
)

Expand Down Expand Up @@ -87,12 +88,7 @@ func (r DeployCF) NotifiesOnSuccess() bool {
}

func (r DeployCF) SavesArtifactsOnFailure() bool {
for _, task := range r.PrePromote {
if task.SavesArtifactsOnFailure() {
return true
}
}
return false
return slices.ContainsFunc(r.PrePromote, func(t Task) bool { return t.SavesArtifactsOnFailure() })
}

func (r DeployCF) IsManualTrigger() bool {
Expand All @@ -108,12 +104,7 @@ func (r DeployCF) ReadsFromArtifacts() bool {
return true
}

for _, pp := range r.PrePromote {
if pp.ReadsFromArtifacts() {
return true
}
}
return false
return slices.ContainsFunc(r.PrePromote, func(t Task) bool { return t.ReadsFromArtifacts() })
}

func (r DeployCF) GetAttempts() int {
Expand Down
17 changes: 5 additions & 12 deletions manifest/feature_toggles.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package manifest

import "golang.org/x/exp/slices"

type FeatureToggles []string

const (
Expand All @@ -14,23 +16,14 @@ var AvailableFeatureToggles = FeatureToggles{
FeatureGithubStatuses,
}

func (f FeatureToggles) contains(aFeature string) bool {
for _, feature := range f {
if feature == aFeature {
return true
}
}
return false
}

func (f FeatureToggles) UpdatePipeline() bool {
return f.contains(FeatureUpdatePipeline) || f.contains(FeatureUpdatePipelineAndTag)
return slices.Contains(f, FeatureUpdatePipeline) || slices.Contains(f, FeatureUpdatePipelineAndTag)
}

func (f FeatureToggles) TagGitRepo() bool {
return f.contains(FeatureUpdatePipelineAndTag)
return slices.Contains(f, FeatureUpdatePipelineAndTag)
}

func (f FeatureToggles) GithubStatuses() bool {
return f.contains(FeatureGithubStatuses)
return slices.Contains(f, FeatureGithubStatuses)
}
15 changes: 3 additions & 12 deletions manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifest

import (
"fmt"
"golang.org/x/exp/slices"
"regexp"
"strings"
)
Expand All @@ -22,21 +23,11 @@ func (n Notifications) NotificationsDefined() bool {
type TaskList []Task

func (tl TaskList) SavesArtifacts() bool {
for _, task := range tl {
if task.SavesArtifacts() {
return true
}
}
return false
return slices.ContainsFunc(tl, func(t Task) bool { return t.SavesArtifacts() })
}

func (tl TaskList) SavesArtifactsOnFailure() bool {
for _, task := range tl {
if task.SavesArtifactsOnFailure() {
return true
}
}
return false
return slices.ContainsFunc(tl, func(t Task) bool { return t.SavesArtifactsOnFailure() })
}

func (tl TaskList) UsesNotifications() bool {
Expand Down
23 changes: 5 additions & 18 deletions manifest/parallel.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package manifest

import "golang.org/x/exp/slices"

type Parallel struct {
Type string
Tasks TaskList `yaml:"tasks,omitempty"`
Expand Down Expand Up @@ -39,34 +41,19 @@ func (p Parallel) MarshalYAML() (interface{}, error) {
}

func (p Parallel) ReadsFromArtifacts() bool {
for _, task := range p.Tasks {
if task.ReadsFromArtifacts() {
return true
}
}
return false
return slices.ContainsFunc(p.Tasks, func(t Task) bool { return t.ReadsFromArtifacts() })
}

func (Parallel) GetAttempts() int {
panic("GetAttempts should never be used in the rendering for a parallel task as we only care about sub tasks")
}

func (p Parallel) SavesArtifacts() bool {
for _, task := range p.Tasks {
if task.SavesArtifacts() {
return true
}
}
return false
return slices.ContainsFunc(p.Tasks, func(t Task) bool { return t.SavesArtifacts() })
}

func (p Parallel) SavesArtifactsOnFailure() bool {
for _, task := range p.Tasks {
if task.SavesArtifactsOnFailure() {
return true
}
}
return false
return slices.ContainsFunc(p.Tasks, func(t Task) bool { return t.SavesArtifactsOnFailure() })
}

func (Parallel) IsManualTrigger() bool {
Expand Down
8 changes: 2 additions & 6 deletions manifest/secret_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manifest
import (
"code.cloudfoundry.org/cli/util/manifestparser"
"fmt"
"golang.org/x/exp/slices"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -211,10 +212,5 @@ func (s secretValidator) Validate(man Manifest) (errors []error) {
}

func (s secretValidator) IsReservedKeyName(keyName string) bool {
for _, name := range reservedKeyNames {
if keyName == name {
return true
}
}
return false
return slices.ContainsFunc(reservedKeyNames, func(s string) bool { return s == keyName })
}
23 changes: 5 additions & 18 deletions manifest/sequence.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package manifest

import "golang.org/x/exp/slices"

type Sequence struct {
Type string
Tasks TaskList
Expand Down Expand Up @@ -34,34 +36,19 @@ func (s Sequence) SetName(name string) Task {
}

func (s Sequence) ReadsFromArtifacts() bool {
for _, task := range s.Tasks {
if task.ReadsFromArtifacts() {
return true
}
}
return false
return slices.ContainsFunc(s.Tasks, func(t Task) bool { return t.ReadsFromArtifacts() })
}

func (s Sequence) GetAttempts() int {
panic("GetAttempts should never be used in the rendering for a sequence task as we only care about sub tasks")
}

func (s Sequence) SavesArtifacts() bool {
for _, task := range s.Tasks {
if task.SavesArtifacts() {
return true
}
}
return false
return slices.ContainsFunc(s.Tasks, func(t Task) bool { return t.SavesArtifacts() })
}

func (s Sequence) SavesArtifactsOnFailure() bool {
for _, task := range s.Tasks {
if task.SavesArtifactsOnFailure() {
return true
}
}
return false
return slices.ContainsFunc(s.Tasks, func(t Task) bool { return t.SavesArtifactsOnFailure() })
}

func (s Sequence) IsManualTrigger() bool {
Expand Down
4 changes: 3 additions & 1 deletion renderers/concourse/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,15 @@ func pathToVersionFile(repoName string, basePath string) (gitRefPath string) {

func onErrorScript(artifactPaths []string, basePath string) string {
var returnScript []string

if len(artifactPaths) != 0 {
returnScript = append(returnScript, " # Artifacts to copy in case of failure")
}

for _, artifactPath := range artifactPaths {
returnScript = append(returnScript, fmt.Sprintf(" copyArtifact %s %s", artifactPath, fullPathToArtifactsDir(gitDir, basePath, artifactsOutDirOnFailure, artifactPath)))

}

returnScript = append(returnScript, " exit 1")
return strings.Join(returnScript, "\n")
}
Expand Down

0 comments on commit b6ac7f5

Please sign in to comment.