-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24e12fb
commit 5402098
Showing
12 changed files
with
181 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package config | ||
|
||
import "fmt" | ||
|
||
type ConfigV2 struct { | ||
Hosts map[string]HostV2 `yaml:"hosts"` | ||
} | ||
|
||
type HostV2 struct { | ||
Default *User `yaml:"default"` | ||
Overrides []Override `yaml:"overrides"` | ||
} | ||
|
||
// Get TODO | ||
func (c *ConfigV2) Get(host string) (*HostV2, error) { | ||
hostConf, ok := (*&c.Hosts)[host] | ||
if !ok { | ||
return nil, fmt.Errorf("cannot find config for host %q", host) | ||
} | ||
|
||
return &hostConf, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func Load(filename string) (*ConfigV2, error) { | ||
isYaml := strings.HasSuffix(filename, ".yaml") | ||
isJsonnet := strings.HasSuffix(filename, ".jsonnet") | ||
|
||
if !isYaml && !isJsonnet { | ||
return nil, fmt.Errorf("unsupported config format") | ||
} | ||
|
||
r, err := os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if isYaml { | ||
buf := new(bytes.Buffer) | ||
buf.ReadFrom(r) | ||
confStr := buf.String() | ||
return parseYaml(confStr) | ||
} else if isJsonnet { | ||
hostMap, err := readHostMap(filename, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configV2 := toV2(hostMap) | ||
return configV2, nil | ||
} else { | ||
return nil, fmt.Errorf("something went horribly wrong") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package config | ||
|
||
import ( | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func parseYaml(s string) (*ConfigV2, error) { | ||
var config ConfigV2 | ||
if err := yaml.Unmarshal([]byte(s), &config); err != nil { | ||
return nil, err | ||
} | ||
return &config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseYaml(t *testing.T) { | ||
|
||
// TODO: improve this test | ||
t.Run("test YAML config parsing", func(t *testing.T) { | ||
s := `users: | ||
work: &work_user | ||
name: John Doe | ||
email: john.doe@corp.com | ||
personal: | ||
github_user: &personal_github_user | ||
name: JD42 | ||
email: JD42@users.noreply.github.com | ||
gitlab_user: &personal_gitlab_user | ||
name: JD42 | ||
email: 786972-JD42@users.noreply.gitlab.com | ||
hosts: | ||
github.com: | ||
default: *personal_github_user | ||
overrides: | ||
- owner: corp | ||
user: *work_user | ||
gitlab.com: | ||
default: *personal_gitlab_user` | ||
|
||
_, err := parseYaml(s) | ||
if err != nil { | ||
t.Errorf("%+v", err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v2.2.0+1 | ||
v2.3.0 |