Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move config parsing to a dedicated pkg #2361

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions cmd/buildkitd/config.go

This file was deleted.

36 changes: 36 additions & 0 deletions cmd/buildkitd/config/load.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import (
"bytes"
Expand All @@ -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"
Expand Down
13 changes: 11 additions & 2 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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})
Expand Down