move config stuff to its own package

This commit is contained in:
vilmibm 2020-04-20 13:57:16 -05:00
parent 815f461e8b
commit c4693077aa
14 changed files with 54 additions and 38 deletions

View file

@ -10,7 +10,7 @@ import (
"strings"
"github.com/cli/cli/command"
"github.com/cli/cli/context"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/update"
"github.com/cli/cli/utils"
"github.com/mgutz/ansi"
@ -88,6 +88,6 @@ func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {
}
repo := updaterEnabled
stateFilePath := path.Join(context.ConfigDir(), "state.yml")
stateFilePath := path.Join(config.ConfigDir(), "state.yml")
return update.CheckForUpdate(client, stateFilePath, repo, currentVersion)
}

View file

@ -4,7 +4,7 @@ import (
"bytes"
"testing"
"github.com/cli/cli/context"
"github.com/cli/cli/internal/config"
)
func TestConfigGet(t *testing.T) {
@ -50,7 +50,7 @@ func TestConfigSet(t *testing.T) {
initBlankContext("", "OWNER/REPO", "master")
buf := bytes.NewBufferString("")
defer context.StubWriteConfig(buf)()
defer config.StubWriteConfig(buf)()
output, err := RunCommand(configSetCmd, "config set editor ed")
if err != nil {
t.Fatalf("error running command `config set editor ed`: %v", err)
@ -80,7 +80,7 @@ editor: ed
initBlankContext(cfg, "OWNER/REPO", "master")
buf := bytes.NewBufferString("")
defer context.StubWriteConfig(buf)()
defer config.StubWriteConfig(buf)()
output, err := RunCommand(configSetCmd, "config set editor vim")
if err != nil {
@ -122,7 +122,7 @@ func TestConfigSetHost(t *testing.T) {
initBlankContext("", "OWNER/REPO", "master")
buf := bytes.NewBufferString("")
defer context.StubWriteConfig(buf)()
defer config.StubWriteConfig(buf)()
output, err := RunCommand(configSetCmd, "config set -hgithub.com git_protocol ssh")
if err != nil {
t.Fatalf("error running command `config set editor ed`: %v", err)
@ -152,7 +152,7 @@ hosts:
initBlankContext(cfg, "OWNER/REPO", "master")
buf := bytes.NewBufferString("")
defer context.StubWriteConfig(buf)()
defer config.StubWriteConfig(buf)()
output, err := RunCommand(configSetCmd, "config set -hgithub.com git_protocol https")
if err != nil {

View file

@ -10,6 +10,7 @@ import (
"github.com/cli/cli/api"
"github.com/cli/cli/context"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/utils"
@ -17,7 +18,7 @@ import (
"github.com/spf13/pflag"
)
// TODO these are sprinkled across command, context, and ghrepo
// TODO these are sprinkled across command, context, config, and ghrepo
const defaultHostname = "github.com"
// Version is dynamically set by the toolchain or overridden by the Makefile.
@ -110,7 +111,7 @@ func BasicClient() (*api.Client, error) {
}
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)))
c, err := context.ParseDefaultConfig()
c, err := config.ParseDefaultConfig()
if err != nil {
return nil, err
}

View file

@ -10,6 +10,7 @@ import (
"github.com/cli/cli/api"
"github.com/cli/cli/context"
"github.com/cli/cli/internal/config"
)
const defaultTestConfig = `hosts:
@ -84,7 +85,7 @@ func initBlankContext(cfg, repo, branch string) {
// NOTE we are not restoring the original readConfig; we never want to touch the config file on
// disk during tests.
context.StubConfig(cfg)
config.StubConfig(cfg)
return ctx
}

View file

@ -5,6 +5,7 @@ import (
"strings"
"github.com/cli/cli/git"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghrepo"
)
@ -22,12 +23,12 @@ type blankContext struct {
remotes Remotes
}
func (c *blankContext) Config() (Config, error) {
config, err := ParseConfig("boom.txt")
func (c *blankContext) Config() (config.Config, error) {
cfg, err := config.ParseConfig("boom.txt")
if err != nil {
panic(fmt.Sprintf("failed to parse config during tests. did you remember to stub? error: %s", err))
}
return config, nil
return cfg, nil
}
func (c *blankContext) AuthToken() (string, error) {

View file

@ -3,15 +3,17 @@ package context
import (
"errors"
"fmt"
"path"
"sort"
"github.com/cli/cli/api"
"github.com/cli/cli/git"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghrepo"
"github.com/mitchellh/go-homedir"
)
// TODO these are sprinkled across command, context, config, and ghrepo
const defaultHostname = "github.com"
// Context represents the interface for querying information about the current environment
type Context interface {
AuthToken() (string, error)
@ -22,7 +24,7 @@ type Context interface {
Remotes() (Remotes, error)
BaseRepo() (ghrepo.Interface, error)
SetBaseRepo(string)
Config() (Config, error)
Config() (config.Config, error)
}
// cap the number of git remotes looked up, since the user might have an
@ -152,25 +154,16 @@ func New() Context {
// A Context implementation that queries the filesystem
type fsContext struct {
config Config
config config.Config
remotes Remotes
branch string
baseRepo ghrepo.Interface
authToken string
}
func ConfigDir() string {
dir, _ := homedir.Expand("~/.config/gh")
return dir
}
func configFile() string {
return path.Join(ConfigDir(), "config.yml")
}
func (c *fsContext) Config() (Config, error) {
func (c *fsContext) Config() (config.Config, error) {
if c.config == nil {
config, err := parseOrSetupConfigFile(configFile())
config, err := config.ParseOrSetupConfigFile(config.ConfigFile())
if err != nil {
return nil, err
}

View file

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"net/url"
"reflect"
"testing"
"github.com/cli/cli/api"
@ -11,6 +12,13 @@ import (
"github.com/cli/cli/internal/ghrepo"
)
func eq(t *testing.T, got interface{}, expected interface{}) {
t.Helper()
if !reflect.DeepEqual(got, expected) {
t.Errorf("expected: %v, got: %v", expected, got)
}
}
func Test_Remotes_FindByName(t *testing.T) {
list := Remotes{
&Remote{Remote: &git.Remote{Name: "mona"}, Owner: "monalisa", Repo: "myfork"},

View file

@ -1,4 +1,4 @@
package context
package config
import (
"errors"
@ -6,11 +6,22 @@ import (
"io"
"io/ioutil"
"os"
"path"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v3"
)
func parseOrSetupConfigFile(fn string) (Config, error) {
func ConfigDir() string {
dir, _ := homedir.Expand("~/.config/gh")
return dir
}
func ConfigFile() string {
return path.Join(ConfigDir(), "config.yml")
}
func ParseOrSetupConfigFile(fn string) (Config, error) {
config, err := ParseConfig(fn)
if err != nil && errors.Is(err, os.ErrNotExist) {
return setupConfigFile(fn)
@ -19,7 +30,7 @@ func parseOrSetupConfigFile(fn string) (Config, error) {
}
func ParseDefaultConfig() (Config, error) {
return ParseConfig(configFile())
return ParseConfig(ConfigFile())
}
var ReadConfigFile = func(fn string) ([]byte, error) {
@ -38,7 +49,7 @@ var ReadConfigFile = func(fn string) ([]byte, error) {
}
var WriteConfigFile = func(fn string, data []byte) error {
cfgFile, err := os.OpenFile(configFile(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) // cargo coded from setup
cfgFile, err := os.OpenFile(ConfigFile(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) // cargo coded from setup
if err != nil {
return err
}

View file

@ -1,4 +1,4 @@
package context
package config
import (
"bytes"

View file

@ -1,4 +1,4 @@
package context
package config
import (
"bufio"

View file

@ -1,4 +1,4 @@
package context
package config
const oauthSuccessPage = `
<!doctype html>

View file

@ -1,4 +1,4 @@
package context
package config
import (
"errors"
@ -149,7 +149,7 @@ func (c *fileConfig) Write() error {
return err
}
return WriteConfigFile(configFile(), marshalled)
return WriteConfigFile(ConfigFile(), marshalled)
}
func (c *fileConfig) Hosts() ([]*HostConfig, error) {

View file

@ -1,4 +1,4 @@
package context
package config
import (
"io"

View file

@ -6,6 +6,7 @@ import (
"strings"
)
// TODO these are sprinkled across command, context, config, and ghrepo
const defaultHostname = "github.com"
// Interface describes an object that represents a GitHub repository