Skip to content

Commit

Permalink
create a util function to clean white spaces from slice
Browse files Browse the repository at this point in the history
  • Loading branch information
danielm-codefresh committed Feb 20, 2023
1 parent c5acda2 commit 156090c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
code.gitea.io/sdk/gitea v0.15.1
github.com/argoproj/argo-cd/v2 v2.5.9
github.com/argoproj/gitops-engine v0.7.1-0.20221004132320-98ccd3d43fd9
github.com/beaconsoftwarellc/gadget v1.3.8
github.com/briandowns/spinner v1.18.1
github.com/ghodss/yaml v1.0.0
github.com/go-git/go-billy/v5 v5.3.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ github.com/aws/aws-sdk-go v1.33.16/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZve
github.com/aws/aws-sdk-go v1.35.24/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
github.com/aws/aws-sdk-go v1.38.49/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/beaconsoftwarellc/gadget v1.3.8 h1:B9E4VyUeZhg6WwDhEkDOF07dQ9ds1J7oke/0o2kwbBo=
github.com/beaconsoftwarellc/gadget v1.3.8/go.mod h1:VjsazzjXTA2Aibxhh9b4oouC4/Qk0MQHyq89gO13/6s=
github.com/beevik/ntp v0.2.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
5 changes: 3 additions & 2 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/argoproj-labs/argocd-autopilot/pkg/log"
"github.com/argoproj-labs/argocd-autopilot/pkg/store"
"github.com/argoproj-labs/argocd-autopilot/pkg/util"
"github.com/beaconsoftwarellc/gadget/stringutil"

billy "github.com/go-git/go-billy/v5"
gg "github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -506,8 +505,10 @@ var createRepo = func(ctx context.Context, opts *CloneOptions) (defaultBranch st
}

_, orgRepo, _, _, _, _, _ := util.ParseGitUrl(opts.Repo)

// It depends on the provider, but org repo strucure should at least contain org and repo name
if len(stringutil.CleanWhiteSpace(strings.Split(orgRepo, "/"))) < 2 {
slc := util.CleanSliceWhiteSpaces(strings.Split(orgRepo, "/"))
if len(slc) < 2 {
return "", errors.New("repo name can't be empty")
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,14 @@ func StealFlags(cmd *cobra.Command, exceptFor []string) (*pflag.FlagSet, error)

return fs, nil
}

func CleanSliceWhiteSpaces(slc []string) []string {
var res []string
for i := range slc {
if slc[i] != "" {
res = append(res, slc[i])
}
}

return res
}
29 changes: 29 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package util

import (
"reflect"
"testing"
)

func TestCleanSliceWhiteSpaces(t *testing.T) {
testcases := []struct {
input []string
expected []string
}{
{
input: []string{"org", "repo"},
expected: []string{"org", "repo"},
},
{
input: []string{"org", ""},
expected: []string{"org"},
},
}

for _, testcase := range testcases {
res := CleanSliceWhiteSpaces(testcase.input)
if !reflect.DeepEqual(res, testcase.expected) {
t.Errorf("slice expected to be %v, but got %v", testcase.expected, res)
}
}
}

0 comments on commit 156090c

Please sign in to comment.