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 ( import (
"errors"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
) )
// NewConfig creates a new Config value from a given JSON file. // ReadInstance reads an instance's details from a given JSON file.
func NewConfig(file string) (Config, error) { func ReadInstance(file string) (Instance, error) {
var err error var err error
c := defaultConfig() i := defaultInstance()
data, err := ioutil.ReadFile(file) data, err := ioutil.ReadFile(file)
if err != nil { 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 { if err = json.Unmarshal(data, &i); err != nil {
return c, fmt.Errorf("unable to decode the JSON configuration from %s...\n%v", file, err) 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 // Propagate the domain to the services as appropriate
c.Services.Gitea.Domain = c.Domain i.Services.Gitea.Domain = i.Domain
c.Services.Traefik.Domain = c.Domain i.Services.Traefik.Domain = i.Domain
c.Services.Gitea.RootUrl = fmt.Sprintf("https://%s/%s", c.Domain, c.Services.Gitea.BaseUri) i.Services.Gitea.RootUrl = fmt.Sprintf("https://%s/%s", i.Domain, i.Services.Gitea.BaseUri)
c.Services.Gitea.SshDomain = c.Domain i.Services.Gitea.SshDomain = i.Domain
// Propagate the shared Group ID to the serivces as appropriate // Propagate the shared Group ID to the serivces as appropriate
c.Services.Traefik.GroupId = c.SharedGroupId i.Services.Traefik.GroupId = i.SharedGroupId
c.Services.Gitea.GroupId = c.SharedGroupId i.Services.Gitea.GroupId = i.SharedGroupId
return c, nil return i, nil
} }
func defaultConfig() Config { func defaultInstance() Instance {
c := Config{ c := Instance{
ProjectName: "", Project: "",
Domain: "localhost", Domain: "localhost",
SharedGroupId: 2239, SharedGroupId: 2239,

View file

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

View file

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