Determine Supported Clients based on registered once (#106)

* Determine Supported Clients based on registered once

* rm dependencys from scm
This commit is contained in:
6543
2020-10-24 20:34:35 +02:00
committed by GitHub
parent b914f94aa4
commit ab9fabfba2
6 changed files with 38 additions and 21 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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{

View File

@@ -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
}

11
utils/slice.go Normal file
View File

@@ -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
}