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 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
53 changes: 14 additions & 39 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,11 +62,7 @@ func RESTClient(opts *api.ClientOptions) (api.RESTClient, error) {
opts = &api.ClientOptions{}
}
if optionsNeedResolution(opts) {
cfg, err := config.Load()
if err != nil {
return nil, err
}
err = resolveOptions(opts, cfg)
err := resolveOptions(opts)
if err != nil {
return nil, err
}
Expand All @@ -83,11 +79,7 @@ func GQLClient(opts *api.ClientOptions) (api.GQLClient, error) {
opts = &api.ClientOptions{}
}
if optionsNeedResolution(opts) {
cfg, err := config.Load()
if err != nil {
return nil, err
}
err = resolveOptions(opts, cfg)
err := resolveOptions(opts)
if err != nil {
return nil, err
}
Expand All @@ -109,11 +101,7 @@ func HTTPClient(opts *api.ClientOptions) (*http.Client, error) {
opts = &api.ClientOptions{}
}
if optionsNeedResolution(opts) {
cfg, err := config.Load()
if err != nil {
return nil, err
}
err = resolveOptions(opts, cfg)
err := resolveOptions(opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,12 +129,7 @@ func CurrentRepository() (repo.Repository, error) {
translator := ssh.NewTranslator()
translateRemotes(remotes, translator)

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

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

filteredRemotes := remotes.FilterByHosts(hosts)
if len(filteredRemotes) == 0 {
Expand All @@ -170,27 +153,19 @@ 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) error {
cfg, _ := config.Read()
if opts.Host == "" {
opts.Host = cfg.Host()
opts.Host, _ = auth.DefaultHost()
}
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(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
if opts.UnixDomainSocket == "" && cfg != nil {
opts.UnixDomainSocket, _ = cfg.Get([]string{"http_unix_socket"})
}
return nil
}
Expand Down
37 changes: 26 additions & 11 deletions gh_test.go
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"testing"

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

func TestResolveOptions(t *testing.T) {
cfg := testConfig()
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
globalFilePath := filepath.Join(tempDir, "config.yml")
hostsFilePath := filepath.Join(tempDir, "hosts.yml")
err := os.WriteFile(globalFilePath, []byte(testGlobalData()), 0755)
assert.NoError(t, err)
err = os.WriteFile(hostsFilePath, []byte(testHostsData()), 0755)
assert.NoError(t, err)

tests := []struct {
name string
Expand Down Expand Up @@ -193,7 +204,7 @@ func TestResolveOptions(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := resolveOptions(tt.opts, cfg)
err := resolveOptions(tt.opts)
assert.NoError(t, err)
assert.Equal(t, tt.wantHost, tt.opts.Host)
assert.Equal(t, tt.wantAuthToken, tt.opts.AuthToken)
Expand Down Expand Up @@ -303,17 +314,21 @@ func TestOptionsNeedResolution(t *testing.T) {
}
}

func testConfig() config.Config {
func testGlobalData() string {
var data = `
hosts:
github.com:
user: user1
oauth_token: token
git_protocol: ssh
http_unix_socket: socket
`
cfg, _ := config.FromString(data)
return cfg
return data
}

func testHostsData() string {
var data = `
github.com:
user: user1
oauth_token: token
git_protocol: ssh
`
return data
}

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