checkpoint: config to instance

This commit is contained in:
Dan Anglin 2021-09-09 00:04:08 +01:00
parent 649ee06c63
commit d2c52510a6
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
5 changed files with 31 additions and 26 deletions

View file

@ -1,42 +1,47 @@
package config
package instance
import (
"errors"
"encoding/json"
"fmt"
"io/ioutil"
)
// NewConfig creates a new Config value from a given JSON file.
func NewConfig(file string) (Config, error) {
// ReadInstance reads an instance's details from a given JSON file.
func ReadInstance(file string) (Instance, error) {
var err error
c := defaultConfig()
i := defaultInstance()
data, err := ioutil.ReadFile(file)
if err != nil {
return c, fmt.Errorf("unable to read data from %s...\n%v", file, err)
return i, fmt.Errorf("unable to read data from %s...\n%v", file, err)
}
if err = json.Unmarshal(data, &c); err != nil {
return c, fmt.Errorf("unable to decode the JSON configuration from %s...\n%v", file, err)
if err = json.Unmarshal(data, &i); err != nil {
return i, fmt.Errorf("unable to decode the JSON configuration from %s...\n%v", file, err)
}
if len(i.Project) == 0 {
return i, errors.New("the value for 'project' must not be empty")
}
// Propagate the domain to the services as appropriate
c.Services.Gitea.Domain = c.Domain
c.Services.Traefik.Domain = c.Domain
c.Services.Gitea.RootUrl = fmt.Sprintf("https://%s/%s", c.Domain, c.Services.Gitea.BaseUri)
c.Services.Gitea.SshDomain = c.Domain
i.Services.Gitea.Domain = i.Domain
i.Services.Traefik.Domain = i.Domain
i.Services.Gitea.RootUrl = fmt.Sprintf("https://%s/%s", i.Domain, i.Services.Gitea.BaseUri)
i.Services.Gitea.SshDomain = i.Domain
// Propagate the shared Group ID to the serivces as appropriate
c.Services.Traefik.GroupId = c.SharedGroupId
c.Services.Gitea.GroupId = c.SharedGroupId
i.Services.Traefik.GroupId = i.SharedGroupId
i.Services.Gitea.GroupId = i.SharedGroupId
return c, nil
return i, nil
}
func defaultConfig() Config {
c := Config{
ProjectName: "",
func defaultInstance() Instance {
c := Instance{
Project: "",
Domain: "localhost",
SharedGroupId: 2239,

View file

@ -1,4 +1,4 @@
package config
package instance
import (
"reflect"
@ -10,12 +10,12 @@ func TestValidConfig(t *testing.T) {
name string
description string
file string
want Config
want Instance
}{
name: "Test case: A valid Config",
description: "Testing the parsing of valid configuration.",
file: "./testdata/config-valid.json",
want: Config{
want: Instance{
ProjectName: "forge-platform-test-config",
Docker: DockerConfig{
Network: DockerNetworkConfig{
@ -60,7 +60,7 @@ func TestValidConfig(t *testing.T) {
testFunc := func(t *testing.T) {
t.Log(testCase.description)
got, err := NewConfig(testCase.file)
got, err := ReadInstance(testCase.file)
if err != nil {
t.Fatalf("Unable to create the configuration from file: %v", err)
}
@ -88,7 +88,7 @@ func TestInvalidConfig(t *testing.T) {
testFunc := func(t *testing.T) {
t.Log(testCase.description)
_, err := NewConfig(testCase.file)
_, err := ReadInstance(testCase.file)
if err == nil {
t.Error("Expected an error with this invalid configuration.")
} else {

View file

@ -1,8 +1,8 @@
package config
package instance
// Config is the whole configuration for the forge platform deployment.
type Config struct {
ProjectName string `json:"project"`
// Instance is the whole configuration for the forge platform deployment.
type Instance struct {
Project string `json:"project"`
Domain string `json:"domain"`
SharedGroupId int `json:"sharedGroupId"`
Docker DockerConfig `json:"docker"`