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

Refactor config package and add functionality #44

Merged
merged 5 commits into from Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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