Skip to content

Commit

Permalink
Refactor config package and add functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed May 25, 2022
1 parent b7131b0 commit 7c2c2f1
Show file tree
Hide file tree
Showing 14 changed files with 1,277 additions and 1,071 deletions.
35 changes: 13 additions & 22 deletions gh.go
Expand Up @@ -14,11 +14,11 @@ import (
"os/exec"

iapi "github.com/cli/go-gh/internal/api"
"github.com/cli/go-gh/internal/config"
iconfig "github.com/cli/go-gh/internal/config"
"github.com/cli/go-gh/internal/git"
irepo "github.com/cli/go-gh/internal/repository"
"github.com/cli/go-gh/pkg/api"
"github.com/cli/go-gh/pkg/auth"
"github.com/cli/go-gh/pkg/config"
repo "github.com/cli/go-gh/pkg/repository"
"github.com/cli/go-gh/pkg/ssh"
"github.com/cli/safeexec"
Expand Down Expand Up @@ -62,7 +62,7 @@ func RESTClient(opts *api.ClientOptions) (api.RESTClient, error) {
opts = &api.ClientOptions{}
}
if optionsNeedResolution(opts) {
cfg, err := config.Load()
cfg, err := config.Read()
if err != nil {
return nil, err
}
Expand All @@ -83,7 +83,7 @@ func GQLClient(opts *api.ClientOptions) (api.GQLClient, error) {
opts = &api.ClientOptions{}
}
if optionsNeedResolution(opts) {
cfg, err := config.Load()
cfg, err := config.Read()
if err != nil {
return nil, err
}
Expand All @@ -109,7 +109,7 @@ func HTTPClient(opts *api.ClientOptions) (*http.Client, error) {
opts = &api.ClientOptions{}
}
if optionsNeedResolution(opts) {
cfg, err := config.Load()
cfg, err := config.Read()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,12 +141,12 @@ func CurrentRepository() (repo.Repository, error) {
translator := ssh.NewTranslator()
translateRemotes(remotes, translator)

cfg, err := config.Load()
cfg, err := config.Read()
if err != nil {
return nil, err
}

hosts := cfg.Hosts()
hosts := auth.KnownHosts(cfg)

filteredRemotes := remotes.FilterByHosts(hosts)
if len(filteredRemotes) == 0 {
Expand All @@ -170,27 +170,18 @@ func optionsNeedResolution(opts *api.ClientOptions) bool {
return false
}

func resolveOptions(opts *api.ClientOptions, cfg config.Config) error {
var token string
var err error
func resolveOptions(opts *api.ClientOptions, cfg *config.Config) error {
if opts.Host == "" {
opts.Host = cfg.Host()
opts.Host, _ = auth.DefaultHost(cfg)
}
if opts.AuthToken == "" {
token, err = cfg.AuthToken(opts.Host)
if err != nil {
var notFoundError iconfig.NotFoundError
if errors.As(err, &notFoundError) {
return fmt.Errorf("authentication token not found for host %s", opts.Host)
} else {
return err
}
opts.AuthToken, _ = auth.TokenForHost(cfg, opts.Host)
if opts.AuthToken == "" {
return fmt.Errorf("authentication token not found for host %s", opts.Host)
}
opts.AuthToken = token
}
if opts.UnixDomainSocket == "" {
unixSocket, _ := cfg.Get("http_unix_socket")
opts.UnixDomainSocket = unixSocket
opts.UnixDomainSocket, _ = config.Get(cfg, []string{"http_unix_socket"})
}
return nil
}
Expand Down
7 changes: 3 additions & 4 deletions gh_test.go
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"testing"

"github.com/cli/go-gh/internal/config"
"github.com/cli/go-gh/pkg/api"
"github.com/cli/go-gh/pkg/config"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestOptionsNeedResolution(t *testing.T) {
}
}

func testConfig() config.Config {
func testConfig() *config.Config {
var data = `
hosts:
github.com:
Expand All @@ -312,8 +312,7 @@ hosts:
git_protocol: ssh
http_unix_socket: socket
`
cfg, _ := config.FromString(data)
return cfg
return config.ReadFromString(data)
}

func printPendingMocks(mocks []gock.Mock) string {
Expand Down

0 comments on commit 7c2c2f1

Please sign in to comment.