osbuild-composer: move config parsing into separate file

The configuration file is API. Let's give it a bit more prominence to
help people treat it as such, and a chance to test it. A basic test is
included in this commit.

Also, this cuts down on the noise in main.go a bit.
This commit is contained in:
Lars Karlitski 2020-09-25 10:21:04 +02:00 committed by Tom Gundersen
parent af9471e4a2
commit db9bf1c659
4 changed files with 71 additions and 27 deletions

View file

@ -0,0 +1,37 @@
package main
import (
"io"
"github.com/BurntSushi/toml"
)
type ComposerConfigFile struct {
Koji *struct {
Servers map[string]struct {
Kerberos *struct {
Principal string `toml:"principal"`
KeyTab string `toml:"keytab"`
} `toml:"kerberos,omitempty"`
} `toml:"servers"`
AllowedDomains []string `toml:"allowed_domains"`
CA *string `toml:"ca"`
} `toml:"koji"`
Worker *struct {
AllowedDomains []string `toml:"allowed_domains"`
CA *string `toml:"ca"`
} `toml:"worker,omitempty"`
}
func LoadConfig(name string) (*ComposerConfigFile, error) {
var c ComposerConfigFile
_, err := toml.DecodeFile(name, &c)
if err != nil {
return nil, err
}
return &c, nil
}
func DumpConfig(c *ComposerConfigFile, w io.Writer) error {
return toml.NewEncoder(w).Encode(c)
}

View file

@ -0,0 +1,22 @@
package main
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestEmpty(t *testing.T) {
config, err := LoadConfig("testdata/empty-config.toml")
require.NoError(t, err)
require.Nil(t, config.Koji)
require.Nil(t, config.Worker)
}
func TestNonExisting(t *testing.T) {
config, err := LoadConfig("testdata/non-existing-config.toml")
require.Error(t, err)
require.True(t, os.IsNotExist(err))
require.Nil(t, config)
}

View file

@ -10,8 +10,6 @@ import (
"os"
"path"
"github.com/BurntSushi/toml"
"github.com/osbuild/osbuild-composer/internal/distro/fedora31"
"github.com/osbuild/osbuild-composer/internal/distro/fedora32"
"github.com/osbuild/osbuild-composer/internal/distro/rhel8"
@ -79,36 +77,23 @@ func createTLSConfig(c *connectionConfig) (*tls.Config, error) {
}
func main() {
var config struct {
Koji *struct {
Servers map[string]struct {
Kerberos *struct {
Principal string `toml:"principal"`
KeyTab string `toml:"keytab"`
} `toml:"kerberos,omitempty"`
} `toml:"servers"`
AllowedDomains []string `toml:"allowed_domains"`
CA *string `toml:"ca"`
} `toml:"koji"`
Worker *struct {
AllowedDomains []string `toml:"allowed_domains"`
CA *string `toml:"ca"`
} `toml:"worker,omitempty"`
}
var verbose bool
flag.BoolVar(&verbose, "v", false, "Print access log")
flag.Parse()
_, err := toml.DecodeFile(configFile, &config)
if err == nil {
log.Println("Composer configuration:")
encoder := toml.NewEncoder(log.Writer())
err := encoder.Encode(&config)
config, err := LoadConfig(configFile)
if err != nil {
log.Fatalf("Could not print config: %v", err)
if os.IsNotExist(err) {
config = &ComposerConfigFile{}
} else {
log.Fatalf("Error loading configuration: %v", err)
}
} else if !os.IsNotExist(err) {
log.Fatalf("Could not load config file '%s': %v", configFile, err)
}
log.Println("Loaded configuration:")
err = DumpConfig(config, log.Writer())
if err != nil {
log.Fatalf("Error printing configuration: %v", err)
}
stateDir, ok := os.LookupEnv("STATE_DIRECTORY")

View file