Merge pull request #2361 from crazy-max/move-config

Move config parsing to a dedicated pkg
master
Tõnis Tiigi 2021-09-20 10:24:02 -07:00 committed by GitHub
commit 7fb8e74422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 50 deletions

View File

@ -1,46 +0,0 @@
package main
import (
"io"
"os"
"strconv"
"strings"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
func Load(r io.Reader) (config.Config, error) {
var c config.Config
t, err := toml.LoadReader(r)
if err != nil {
return c, errors.Wrap(err, "failed to parse config")
}
err = t.Unmarshal(&c)
if err != nil {
return c, errors.Wrap(err, "failed to parse config")
}
return c, nil
}
func LoadFile(fp string) (config.Config, error) {
f, err := os.Open(fp)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return config.Config{}, nil
}
return config.Config{}, errors.Wrapf(err, "failed to load config from %s", fp)
}
defer f.Close()
return Load(f)
}
// parseBoolOrAuto returns (nil, nil) if s is "auto"
func parseBoolOrAuto(s string) (*bool, error) {
if s == "" || strings.ToLower(s) == "auto" {
return nil, nil
}
b, err := strconv.ParseBool(s)
return &b, err
}

View File

@ -0,0 +1,36 @@
package config
import (
"io"
"os"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
// Load loads buildkitd config
func Load(r io.Reader) (Config, error) {
var c Config
t, err := toml.LoadReader(r)
if err != nil {
return c, errors.Wrap(err, "failed to parse config")
}
err = t.Unmarshal(&c)
if err != nil {
return c, errors.Wrap(err, "failed to parse config")
}
return c, nil
}
// LoadFile loads buildkitd config file
func LoadFile(fp string) (Config, error) {
f, err := os.Open(fp)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return Config{}, nil
}
return Config{}, errors.Wrapf(err, "failed to load config from %s", fp)
}
defer f.Close()
return Load(f)
}

View File

@ -1,4 +1,4 @@
package main
package config
import (
"bytes"
@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestConfig(t *testing.T) {
func TestLoad(t *testing.T) {
const testConfig = `
root = "/foo/bar"

View File

@ -204,7 +204,7 @@ func main() {
ctx, cancel := context.WithCancel(appcontext.Context())
defer cancel()
cfg, err := LoadFile(c.GlobalString("config"))
cfg, err := config.LoadFile(c.GlobalString("config"))
if err != nil {
return err
}
@ -368,7 +368,7 @@ func defaultConfigPath() string {
}
func defaultConf() (config.Config, error) {
cfg, err := LoadFile(defaultConfigPath())
cfg, err := config.LoadFile(defaultConfigPath())
if err != nil {
var pe *os.PathError
if !errors.As(err, &pe) {
@ -777,6 +777,15 @@ func getDNSConfig(cfg *config.DNSConfig) *oci.DNSConfig {
return dns
}
// parseBoolOrAuto returns (nil, nil) if s is "auto"
func parseBoolOrAuto(s string) (*bool, error) {
if s == "" || strings.ToLower(s) == "auto" {
return nil, nil
}
b, err := strconv.ParseBool(s)
return &b, err
}
func runTraceController(p string, exp sdktrace.SpanExporter) error {
server := grpc.NewServer()
tracev1.RegisterTraceServiceServer(server, &traceCollector{exporter: exp})