diff --git a/configs/configs.go b/configs/configs.go index e608100..7e1da9c 100644 --- a/configs/configs.go +++ b/configs/configs.go @@ -11,6 +11,9 @@ import ( "strings" "github.com/gabrie30/ghorg/colorlog" + "github.com/gabrie30/ghorg/scm" + "github.com/gabrie30/ghorg/utils" + "github.com/mitchellh/go-homedir" "github.com/spf13/viper" ) @@ -29,7 +32,7 @@ var ( ErrNoBitbucketAppPassword = errors.New("Could not find a valid bitbucket app password. GHORG_BITBUCKET_APP_PASSWORD or (--token, -t) must be set to clone repos from bitbucket, see 'BitBucket Setup' in README.md") // ErrIncorrectScmType indicates an unsupported scm type being used - ErrIncorrectScmType = errors.New("GHORG_SCM_TYPE or --scm must be one of github, gitlab, gitea or bitbucket") + ErrIncorrectScmType = errors.New("GHORG_SCM_TYPE or --scm must be one of " + strings.Join(scm.SupportedClients(), ", ")) // ErrIncorrectCloneType indicates an unsupported clone type being used ErrIncorrectCloneType = errors.New("GHORG_CLONE_TYPE or --clone-type must be one of org or user") @@ -307,7 +310,7 @@ func VerifyConfigsSetCorrectly() error { cloneType := os.Getenv("GHORG_CLONE_TYPE") protocol := os.Getenv("GHORG_CLONE_PROTOCOL") - if scmType != "github" && scmType != "gitlab" && scmType != "bitbucket" && scmType != "gitea" { + if !utils.IsStringInSlice(scmType, scm.SupportedClients()) { return ErrIncorrectScmType } diff --git a/scm/client.go b/scm/client.go index 2182d4a..475bcb7 100644 --- a/scm/client.go +++ b/scm/client.go @@ -1,5 +1,6 @@ package scm +// Client define the interface a scm client has to have type Client interface { GetUserRepos(targetUsername string) ([]Repo, error) GetOrgRepos(targetOrg string) ([]Repo, error) @@ -24,3 +25,12 @@ func GetClient(cType string) Client { } return nil } + +// SupportedClients return list of all supported clients +func SupportedClients() []string { + types := make([]string, 0, len(clients)) + for i := range clients { + types = append(types, clients[i].GetType()) + } + return types +} diff --git a/scm/gitea.go b/scm/gitea.go index 40f5de6..4fe08b0 100644 --- a/scm/gitea.go +++ b/scm/gitea.go @@ -7,7 +7,6 @@ import ( "strings" "code.gitea.io/sdk/gitea" - "github.com/gabrie30/ghorg/colorlog" ) var ( @@ -30,7 +29,7 @@ func (c Gitea) GetOrgRepos(targetOrg string) ([]Repo, error) { client, err := c.determineClient() if err != nil { - colorlog.PrintError(err) + return nil, err } perPage := 10 @@ -46,9 +45,9 @@ func (c Gitea) GetOrgRepos(targetOrg string) ([]Repo, error) { if err != nil { if resp != nil && resp.StatusCode == http.StatusNotFound { - colorlog.PrintError(fmt.Errorf("org \"%s\" not found", targetOrg)) + err = fmt.Errorf("org \"%s\" not found", targetOrg) } - return []Repo{}, err + return nil, err } repoDataFiltered, err := c.filter(client, rps) @@ -72,7 +71,7 @@ func (c Gitea) GetUserRepos(targetUsername string) ([]Repo, error) { client, err := c.determineClient() if err != nil { - colorlog.PrintError(err) + return nil, err } perPage := 10 @@ -88,9 +87,9 @@ func (c Gitea) GetUserRepos(targetUsername string) ([]Repo, error) { if err != nil { if resp != nil && resp.StatusCode == http.StatusNotFound { - colorlog.PrintError(fmt.Errorf("org \"%s\" not found", targetUsername)) + err = fmt.Errorf("org \"%s\" not found", targetUsername) } - return []Repo{}, err + return nil, err } repoDataFiltered, err := c.filter(client, rps) diff --git a/scm/github.go b/scm/github.go index aef7a59..04bfe0a 100644 --- a/scm/github.go +++ b/scm/github.go @@ -6,7 +6,6 @@ import ( "os" "strings" - "github.com/gabrie30/ghorg/configs" "github.com/google/go-github/v32/github" "golang.org/x/oauth2" ) @@ -34,8 +33,7 @@ func (c Github) GetOrgRepos(targetOrg string) ([]Repo, error) { } if os.Getenv("GHORG_SCM_BASE_URL") != "" { - u := configs.EnsureTrailingSlash(os.Getenv("GHORG_SCM_BASE_URL")) - c.client.BaseURL, _ = url.Parse(u) + c.client.BaseURL, _ = url.Parse(os.Getenv("GHORG_SCM_BASE_URL")) } opt := &github.RepositoryListByOrgOptions{ @@ -72,8 +70,7 @@ func (c Github) GetUserRepos(targetUser string) ([]Repo, error) { } if os.Getenv("GHORG_SCM_BASE_URL") != "" { - u := configs.EnsureTrailingSlash(os.Getenv("GHORG_SCM_BASE_URL")) - c.client.BaseURL, _ = url.Parse(u) + c.client.BaseURL, _ = url.Parse(os.Getenv("GHORG_SCM_BASE_URL")) } opt := &github.RepositoryListOptions{ diff --git a/scm/gitlab.go b/scm/gitlab.go index f646e72..3439366 100644 --- a/scm/gitlab.go +++ b/scm/gitlab.go @@ -5,7 +5,6 @@ import ( "os" "strings" - "github.com/gabrie30/ghorg/colorlog" gitlab "github.com/xanzy/go-gitlab" ) @@ -30,7 +29,7 @@ func (c Gitlab) GetOrgRepos(targetOrg string) ([]Repo, error) { client, err := c.determineClient() if err != nil { - colorlog.PrintError(err) + return nil, err } opt := &gitlab.ListGroupProjectsOptions{ @@ -47,8 +46,7 @@ func (c Gitlab) GetOrgRepos(targetOrg string) ([]Repo, error) { if err != nil { if resp != nil && resp.StatusCode == 404 { - colorlog.PrintError(fmt.Sprintf("group '%s' does not exist", targetOrg)) - return []Repo{}, nil + return nil, fmt.Errorf("group '%s' does not exist", targetOrg) } return []Repo{}, err } @@ -87,7 +85,7 @@ func (c Gitlab) GetUserRepos(targetUsername string) ([]Repo, error) { client, err := c.determineClient() if err != nil { - colorlog.PrintError(err) + return nil, err } opt := &gitlab.ListProjectsOptions{ @@ -102,8 +100,7 @@ func (c Gitlab) GetUserRepos(targetUsername string) ([]Repo, error) { ps, resp, err := client.Projects.ListUserProjects(targetUsername, opt) if err != nil { if resp != nil && resp.StatusCode == 404 { - colorlog.PrintError(fmt.Sprintf("user '%s' does not exist", targetUsername)) - return []Repo{}, nil + return nil, fmt.Errorf("user '%s' does not exist", targetUsername) } return []Repo{}, err } diff --git a/utils/slice.go b/utils/slice.go new file mode 100644 index 0000000..0e17cf1 --- /dev/null +++ b/utils/slice.go @@ -0,0 +1,11 @@ +package utils + +// IsStringInSlice check if a string is in a given slice +func IsStringInSlice(s string, sl []string) bool { + for i := range sl { + if sl[i] == s { + return true + } + } + return false +}