Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsAuthenticated #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package gh

import (
"bytes"
"errors"
"fmt"
"net/http"
"net/url"
Expand All @@ -20,6 +19,7 @@ import (
irepo "github.com/cli/go-gh/internal/repository"
"github.com/cli/go-gh/internal/ssh"
"github.com/cli/go-gh/pkg/api"
gherrors "github.com/cli/go-gh/pkg/errors"
repo "github.com/cli/go-gh/pkg/repository"
"github.com/cli/safeexec"
)
Expand Down Expand Up @@ -125,7 +125,7 @@ func CurrentRepository() (repo.Repository, error) {
return nil, err
}
if len(remotes) == 0 {
return nil, errors.New("unable to determine current repository, no git remotes configured for this repository")
return nil, gherrors.ErrNoRepositories
}

sshConfig := ssh.ParseConfig()
Expand All @@ -140,13 +140,29 @@ func CurrentRepository() (repo.Repository, error) {

filteredRemotes := remotes.FilterByHosts(hosts)
if len(filteredRemotes) == 0 {
return nil, errors.New("unable to determine current repository, none of the git remotes configured for this repository point to a known GitHub host")
return nil, gherrors.ErrNoRepositoryHosts
}

r := filteredRemotes[0]
return irepo.New(r.Host, r.Owner, r.Repo), nil
}

// IsAuthenticated returns whether the user has authenticated the given host.
// The default host "github.com" is used if host is "".
func IsAuthenticated(host string) bool {
cfg, err := config.Load()
if err != nil {
return false
}

if host == "" {
host = cfg.Host()
}

_, err = cfg.AuthToken(host)
return err == nil
}

func resolveOptions(opts *api.ClientOptions, cfg config.Config) error {
var token string
var err error
Expand Down
5 changes: 3 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/cli/go-gh/internal/set"
gherrors "github.com/cli/go-gh/pkg/errors"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -89,7 +90,7 @@ func (c config) AuthToken(host string) (string, error) {
if token, err := c.GetForHost(hostname, oauthToken); err == nil {
return token, nil
}
return "", NotFoundError{errors.New("not found")}
return "", gherrors.ErrNotFound
}

if token := os.Getenv(ghToken); token != "" {
Expand All @@ -101,7 +102,7 @@ func (c config) AuthToken(host string) (string, error) {
if token, err := c.GetForHost(hostname, oauthToken); err == nil {
return token, nil
}
return "", NotFoundError{errors.New("not found")}
return "", gherrors.ErrNotFound
}

func isEnterprise(host string) bool {
Expand Down
11 changes: 4 additions & 7 deletions internal/config/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"

gherrors "github.com/cli/go-gh/pkg/errors"
"gopkg.in/yaml.v3"
)

Expand All @@ -19,10 +20,6 @@ type configEntry struct {
Index int
}

type NotFoundError struct {
error
}

func (cm *configMap) empty() bool {
return cm.Root == nil || len(cm.Root.Content) == 0
}
Expand All @@ -42,7 +39,7 @@ func (cm *configMap) setStringValue(key, value string) error {
return nil
}

var notFound *NotFoundError
var notFound *gherrors.NotFoundError
if err != nil && !errors.As(err, &notFound) {
return err
}
Expand All @@ -63,7 +60,7 @@ func (cm *configMap) setStringValue(key, value string) error {

func (cm *configMap) findEntry(key string) (*configEntry, error) {
if cm.empty() {
return nil, &NotFoundError{errors.New("not found")}
return nil, &gherrors.ErrNotFound
}

ce := &configEntry{}
Expand All @@ -85,7 +82,7 @@ func (cm *configMap) findEntry(key string) (*configEntry, error) {
}
}

return nil, &NotFoundError{errors.New("not found")}
return nil, &gherrors.ErrNotFound
}

func (cm *configMap) removeEntry(key string) {
Expand Down
26 changes: 26 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package errors

import "errors"

// NoRepositoriesError is returned when go-gh is unable to determine current repository,
// when no git remotes configured for this repository.
type NoRepositoriesError struct {
error
}

// NoRepositoryHostsError is returned when go-gh is unable to determine current repository,
// when none of the git remotes configured for this repository point to a known GitHub host.
type NoRepositoryHostsError struct {
error
}

// NotFoundError is returned when there is no authentication token for the host.
type NotFoundError struct {
error
}

var (
ErrNoRepositories = NoRepositoriesError{errors.New("unable to determine current repository, no git remotes configured for this repository")}
ErrNoRepositoryHosts = NoRepositoryHostsError{errors.New("unable to determine current repository, none of the git remotes configured for this repository point to a known GitHub host")}
ErrNotFound = NotFoundError{errors.New("not found")}
)