Browse Source

Return errors for failed host clients

Peter Gallagher 6 years ago
parent
commit
f89b8f2b29
3 changed files with 11 additions and 8 deletions
  1. 3 3
      hosts/github.go
  2. 4 2
      hosts/gitlab.go
  3. 4 3
      hosts/host.go

+ 3 - 3
hosts/github.go

@@ -27,7 +27,8 @@ type Github struct {
 
 // NewGithubClient accepts a manager struct and returns a Github host pointer which will be used to
 // perform a github audit on an organization, user, or PR.
-func NewGithubClient(m manager.Manager) *Github {
+func NewGithubClient(m manager.Manager) (*Github, error) {
+	var err error
 	ctx := context.Background()
 	token := oauth2.StaticTokenSource(
 		&oauth2.Token{AccessToken: options.GetAccessToken(m.Opts)},
@@ -39,7 +40,6 @@ func NewGithubClient(m manager.Manager) *Github {
 	if m.Opts.BaseURL == "" {
 		githubClient = github.NewClient(httpClient)
 	} else {
-		var err error
 		githubClient, err = github.NewEnterpriseClient(m.Opts.BaseURL, m.Opts.BaseURL, httpClient)
 		if err != nil {
 			log.Error(err)
@@ -49,7 +49,7 @@ func NewGithubClient(m manager.Manager) *Github {
 	return &Github{
 		manager: m,
 		client:  githubClient,
-	}
+	}, err
 }
 
 // Audit will audit a github user or organization's repos.

+ 4 - 2
hosts/gitlab.go

@@ -22,7 +22,9 @@ type Gitlab struct {
 
 // NewGitlabClient accepts a manager struct and returns a Gitlab host pointer which will be used to
 // perform a gitlab audit on an group or user.
-func NewGitlabClient(m manager.Manager) *Gitlab {
+func NewGitlabClient(m manager.Manager) (*Gitlab, error) {
+	var err error
+
 	gitlabClient := &Gitlab{
 		manager: m,
 		ctx:     context.Background(),
@@ -36,7 +38,7 @@ func NewGitlabClient(m manager.Manager) *Gitlab {
 		}
 	}
 
-	return gitlabClient
+	return gitlabClient, err
 }
 
 // Audit will audit a github user or organization's repos.

+ 4 - 3
hosts/host.go

@@ -20,11 +20,12 @@ type Host interface {
 // Run kicks off a host audit. This function accepts a manager and determines what host it should audit
 func Run(m *manager.Manager) error {
 	var host Host
+	var err error
 	switch getHost(m.Opts.Host) {
 	case _github:
-		host = NewGithubClient(*m)
+		host, err = NewGithubClient(*m)
 	case _gitlab:
-		host = NewGitlabClient(*m)
+		host, err = NewGitlabClient(*m)
 	default:
 		return nil
 	}
@@ -34,7 +35,7 @@ func Run(m *manager.Manager) error {
 	} else {
 		host.Audit()
 	}
-	return nil
+	return err
 }
 
 func getHost(host string) int {