Skip to content

Commit

Permalink
Move config parsing to a dedicated pkg
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Sep 17, 2021
1 parent e07f388 commit 9cd28ae
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
4 changes: 1 addition & 3 deletions cmd/buildkitd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"github.com/moby/buildkit/util/resolver"
)
import "github.com/moby/buildkit/util/resolver"

// Config provides containerd configuration data for the server
type Config struct {
Expand Down
14 changes: 12 additions & 2 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver/bboltcachestorage"
"github.com/moby/buildkit/util/apicaps"
"github.com/moby/buildkit/util/appconfig"
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/archutil"
Expand Down Expand Up @@ -204,7 +205,7 @@ func main() {
ctx, cancel := context.WithCancel(appcontext.Context())
defer cancel()

cfg, err := LoadFile(c.GlobalString("config"))
cfg, err := appconfig.LoadFile(c.GlobalString("config"))
if err != nil {
return err
}
Expand Down Expand Up @@ -368,7 +369,7 @@ func defaultConfigPath() string {
}

func defaultConf() (config.Config, error) {
cfg, err := LoadFile(defaultConfigPath())
cfg, err := appconfig.LoadFile(defaultConfigPath())
if err != nil {
var pe *os.PathError
if !errors.As(err, &pe) {
Expand Down Expand Up @@ -777,6 +778,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
15 changes: 3 additions & 12 deletions cmd/buildkitd/config.go → util/appconfig/load.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main
package appconfig

import (
"io"
"os"
"strconv"
"strings"

"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)

// Load loads buildkitd config
func Load(r io.Reader) (config.Config, error) {
var c config.Config
t, err := toml.LoadReader(r)
Expand All @@ -24,6 +23,7 @@ func Load(r io.Reader) (config.Config, error) {
return c, nil
}

// LoadFile loads buildkitd config file
func LoadFile(fp string) (config.Config, error) {
f, err := os.Open(fp)
if err != nil {
Expand All @@ -35,12 +35,3 @@ func LoadFile(fp string) (config.Config, error) {
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
}
4 changes: 2 additions & 2 deletions cmd/buildkitd/config_test.go → util/appconfig/load_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package appconfig

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
3 changes: 2 additions & 1 deletion util/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/remotes/docker"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/config"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/buildkit/util/imageutil"
Expand Down Expand Up @@ -54,7 +55,7 @@ func Push(ctx context.Context, sm *session.Manager, sid string, provider content
if insecure {
insecureTrue := true
httpTrue := true
hosts = resolver.NewRegistryConfig(map[string]resolver.RegistryConfig{
hosts = resolver.NewRegistryConfig(map[string]config.RegistryConfig{
reference.Domain(parsed): {
Insecure: &insecureTrue,
PlainHTTP: &httpTrue,
Expand Down

0 comments on commit 9cd28ae

Please sign in to comment.