partial cleanup

This commit is contained in:
vilmibm 2020-05-27 20:57:59 -05:00
parent 7b4c0c57b8
commit 37761c97fa
3 changed files with 34 additions and 27 deletions

View file

@ -23,6 +23,29 @@ func TestAliasSet_gh_command(t *testing.T) {
eq(t, err.Error(), `could not create alias: "pr" is already a gh command`)
}
func TestAliasSet_empty_aliases(t *testing.T) {
cfg := `---
aliases:
hosts:
github.com:
user: OWNER
oauth_token: token123
`
initBlankContext(cfg, "OWNER/REPO", "trunk")
buf := bytes.NewBufferString("")
defer config.StubWriteConfig(buf)()
output, err := RunCommand("alias set co pr checkout -Rcool/repo")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
test.ExpectLines(t, output.String(), "TODO")
// TODO
}
func TestAliasSet_existing_alias(t *testing.T) {
cfg := `---
hosts:

View file

@ -30,7 +30,7 @@ func (a *AliasConfig) Get(alias string) string {
func (a *AliasConfig) Add(alias, expansion string) error {
if a.Root == nil {
// TODO awful hack bad type conversion i'm sorry
entry, err := a.Parent.(*fileConfig).FindEntryPrime("aliases")
entry, err := a.Parent.(*fileConfig).FindEntry("aliases")
var notFound *NotFoundError
@ -54,9 +54,6 @@ func (a *AliasConfig) Add(alias, expansion string) error {
// Empty aliases; inject new value node after existing aliases key
newContent := []*yaml.Node{}
for i := 0; i < len(a.Parent.Root().Content); i++ {
node := a.Parent.Root().Content[i]
if i == entry.Index {
}
if i == entry.Index {
newContent = append(newContent, entry.KeyNode, valueNode)
i++

View file

@ -41,18 +41,20 @@ func (cm *ConfigMap) Empty() bool {
}
func (cm *ConfigMap) GetStringValue(key string) (string, error) {
_, valueNode, err := cm.FindEntry(key)
entry, err := cm.FindEntry(key)
if err != nil {
return "", err
}
return valueNode.Value, nil
return entry.ValueNode.Value, nil
}
func (cm *ConfigMap) SetStringValue(key, value string) error {
_, valueNode, err := cm.FindEntry(key)
entry, err := cm.FindEntry(key)
var notFound *NotFoundError
valueNode := entry.ValueNode
if err != nil && errors.As(err, &notFound) {
keyNode := &yaml.Node{
Kind: yaml.ScalarNode,
@ -79,7 +81,7 @@ type ConfigEntry struct {
Index int
}
func (cm *ConfigMap) FindEntryPrime(key string) (ce *ConfigEntry, err error) {
func (cm *ConfigMap) FindEntry(key string) (ce *ConfigEntry, err error) {
err = nil
ce = &ConfigEntry{}
@ -99,21 +101,6 @@ func (cm *ConfigMap) FindEntryPrime(key string) (ce *ConfigEntry, err error) {
return ce, &NotFoundError{errors.New("not found")}
}
func (cm *ConfigMap) FindEntry(key string) (keyNode, valueNode *yaml.Node, err error) {
err = nil
topLevelKeys := cm.Root.Content
for i, v := range topLevelKeys {
if v.Value == key && i+1 < len(topLevelKeys) {
keyNode = v
valueNode = topLevelKeys[i+1]
return
}
}
return nil, nil, &NotFoundError{errors.New("not found")}
}
func NewConfig(root *yaml.Node) Config {
return &fileConfig{
ConfigMap: ConfigMap{Root: root.Content[0]},
@ -204,14 +191,14 @@ func (c *fileConfig) Write() error {
}
func (c *fileConfig) Aliases() (*AliasConfig, error) {
_, aliasesEntry, err := c.FindEntry("aliases")
entry, err := c.FindEntry("aliases")
var nfe *NotFoundError
if err != nil && errors.As(err, &nfe) {
// TODO does this make sense at all? want to simulate existing but empty key.
return &AliasConfig{Parent: c}, nil
}
aliasConfig, err := c.parseAliasConfig(aliasesEntry)
aliasConfig, err := c.parseAliasConfig(entry.ValueNode)
if err != nil {
return nil, err
}
@ -239,12 +226,12 @@ func (c *fileConfig) Hosts() ([]*HostConfig, error) {
return c.hosts, nil
}
_, hostsEntry, err := c.FindEntry("hosts")
entry, err := c.FindEntry("hosts")
if err != nil {
return nil, fmt.Errorf("could not find hosts config: %w", err)
}
hostConfigs, err := c.parseHosts(hostsEntry)
hostConfigs, err := c.parseHosts(entry.ValueNode)
if err != nil {
return nil, fmt.Errorf("could not parse hosts config: %w", err)
}