Merge pull request #502 from aluzzardi/pkg-cleanup

cleanup: go packages
This commit is contained in:
Andrea Luzzardi 2021-05-25 17:05:33 -07:00 committed by GitHub
commit 78fcb503c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 174 additions and 158 deletions

View File

@ -1,7 +1,7 @@
run: run:
skip-dirs: skip-dirs:
# progressui is a modified 3rd party library from buildkit # progressui is a modified 3rd party library from buildkit
- pkg/progressui - util/progressui
linters: linters:
disable-all: true disable-all: true

View File

@ -38,8 +38,8 @@ lint: shellcheck cuelint golint check-buildkit-version
check-buildkit-version: check-buildkit-version:
@test \ @test \
"$(shell grep buildkit ./go.mod | cut -d' ' -f2)" = \ "$(shell grep buildkit ./go.mod | cut -d' ' -f2)" = \
"$(shell grep ' = "v' ./pkg/buildkitd/buildkitd.go | sed -E 's/^.*version.*=.*\"(v.*)\"/\1/' )" \ "$(shell grep ' = "v' ./util/buildkitd/buildkitd.go | sed -E 's/^.*version.*=.*\"(v.*)\"/\1/' )" \
|| { echo buildkit version mismatch go.mod != pkg/buildkitd/buildkitd.go ; exit 1; } || { echo buildkit version mismatch go.mod != util/buildkitd/buildkitd.go ; exit 1; }
.PHONY: integration .PHONY: integration
integration: dagger-debug integration: dagger-debug

View File

@ -1,7 +1,8 @@
package dagger package client
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -22,11 +23,13 @@ import (
"github.com/moby/buildkit/session" "github.com/moby/buildkit/session"
// docker output // docker output
"dagger.io/go/pkg/buildkitd" "go.dagger.io/dagger/util/buildkitd"
"dagger.io/go/pkg/progressui" "go.dagger.io/dagger/util/progressui"
"dagger.io/go/dagger/compiler" "go.dagger.io/dagger/compiler"
"dagger.io/go/dagger/state" "go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
) )
// A dagger client // A dagger client
@ -35,7 +38,7 @@ type Client struct {
noCache bool noCache bool
} }
func NewClient(ctx context.Context, host string, noCache bool) (*Client, error) { func New(ctx context.Context, host string, noCache bool) (*Client, error) {
if host == "" { if host == "" {
host = os.Getenv("BUILDKIT_HOST") host = os.Getenv("BUILDKIT_HOST")
} }
@ -61,14 +64,14 @@ func NewClient(ctx context.Context, host string, noCache bool) (*Client, error)
}, nil }, nil
} }
type ClientDoFunc func(context.Context, *Environment, Solver) error type DoFunc func(context.Context, *environment.Environment, solver.Solver) error
// FIXME: return completed *Route, instead of *compiler.Value // FIXME: return completed *Route, instead of *compiler.Value
func (c *Client) Do(ctx context.Context, state *state.State, fn ClientDoFunc) (*Environment, error) { func (c *Client) Do(ctx context.Context, state *state.State, fn DoFunc) (*environment.Environment, error) {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
eg, gctx := errgroup.WithContext(ctx) eg, gctx := errgroup.WithContext(ctx)
environment, err := NewEnvironment(state) environment, err := environment.New(state)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -90,11 +93,11 @@ func (c *Client) Do(ctx context.Context, state *state.State, fn ClientDoFunc) (*
return environment, eg.Wait() return environment, eg.Wait()
} }
func (c *Client) buildfn(ctx context.Context, environment *Environment, fn ClientDoFunc, ch chan *bk.SolveStatus) error { func (c *Client) buildfn(ctx context.Context, env *environment.Environment, fn DoFunc, ch chan *bk.SolveStatus) error {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
// Scan local dirs to grant access // Scan local dirs to grant access
localdirs := environment.LocalDirs() localdirs := env.LocalDirs()
for label, dir := range localdirs { for label, dir := range localdirs {
abs, err := filepath.Abs(dir) abs, err := filepath.Abs(dir)
if err != nil { if err != nil {
@ -104,7 +107,7 @@ func (c *Client) buildfn(ctx context.Context, environment *Environment, fn Clien
} }
// buildkit auth provider (registry) // buildkit auth provider (registry)
auth := newRegistryAuthProvider() auth := solver.NewRegistryAuthProvider()
// Setup solve options // Setup solve options
opts := bk.SolveOpt{ opts := bk.SolveOpt{
@ -119,16 +122,22 @@ func (c *Client) buildfn(ctx context.Context, environment *Environment, fn Clien
Msg("spawning buildkit job") Msg("spawning buildkit job")
resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) { resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) {
s := NewSolver(c.c, gw, ch, auth, c.noCache) s := solver.New(solver.Opts{
Control: c.c,
Gateway: gw,
Events: ch,
Auth: auth,
NoCache: c.noCache,
})
lg.Debug().Msg("loading configuration") lg.Debug().Msg("loading configuration")
if err := environment.LoadPlan(ctx, s); err != nil { if err := env.LoadPlan(ctx, s); err != nil {
return nil, err return nil, err
} }
// Compute output overlay // Compute output overlay
if fn != nil { if fn != nil {
if err := fn(ctx, environment, s); err != nil { if err := fn(ctx, env, s); err != nil {
return nil, compiler.Err(err) return nil, compiler.Err(err)
} }
} }
@ -139,7 +148,7 @@ func (c *Client) buildfn(ctx context.Context, environment *Environment, fn Clien
span, _ := opentracing.StartSpanFromContext(ctx, "Environment.Export") span, _ := opentracing.StartSpanFromContext(ctx, "Environment.Export")
defer span.Finish() defer span.Finish()
computed := environment.Computed().JSON().PrettyString() computed := env.Computed().JSON().PrettyString()
st := llb. st := llb.
Scratch(). Scratch().
File( File(
@ -234,3 +243,22 @@ func (c *Client) logSolveStatus(ctx context.Context, ch chan *bk.SolveStatus) er
}, },
) )
} }
// A helper to remove noise from buildkit error messages.
// FIXME: Obviously a cleaner solution would be nice.
func bkCleanError(err error) error {
noise := []string{
"executor failed running ",
"buildkit-runc did not terminate successfully",
"rpc error: code = Unknown desc = ",
"failed to solve: ",
}
msg := err.Error()
for _, s := range noise {
msg = strings.ReplaceAll(msg, s, "")
}
return errors.New(msg)
}

View File

@ -3,10 +3,12 @@ package common
import ( import (
"context" "context"
"dagger.io/go/dagger"
"dagger.io/go/dagger/state"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
) )
func CurrentWorkspace(ctx context.Context) *state.Workspace { func CurrentWorkspace(ctx context.Context) *state.Workspace {
@ -79,14 +81,14 @@ func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *s
} }
// Re-compute an environment (equivalent to `dagger up`). // Re-compute an environment (equivalent to `dagger up`).
func EnvironmentUp(ctx context.Context, state *state.State, noCache bool) *dagger.Environment { func EnvironmentUp(ctx context.Context, state *state.State, noCache bool) *environment.Environment {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
c, err := dagger.NewClient(ctx, "", noCache) c, err := client.New(ctx, "", noCache)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }
result, err := c.Do(ctx, state, func(ctx context.Context, environment *dagger.Environment, s dagger.Solver) error { result, err := c.Do(ctx, state, func(ctx context.Context, environment *environment.Environment, s solver.Solver) error {
log.Ctx(ctx).Debug().Msg("bringing environment up") log.Ctx(ctx).Debug().Msg("bringing environment up")
return environment.Up(ctx, s) return environment.Up(ctx, s)
}) })

View File

@ -8,10 +8,10 @@ import (
"strings" "strings"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"dagger.io/go/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"dagger.io/go/dagger/compiler" "go.dagger.io/dagger/compiler"
"dagger.io/go/dagger/state" "go.dagger.io/dagger/state"
"go.mozilla.org/sops/v3" "go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/decrypt" "go.mozilla.org/sops/v3/decrypt"

View File

@ -3,10 +3,10 @@ package cmd
import ( import (
"os" "os"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
) )
var initCmd = &cobra.Command{ var initCmd = &cobra.Command{

View File

@ -1,10 +1,10 @@
package input package input
import ( import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
) )
var containerCmd = &cobra.Command{ var containerCmd = &cobra.Command{

View File

@ -4,11 +4,11 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
) )
var dirCmd = &cobra.Command{ var dirCmd = &cobra.Command{

View File

@ -1,10 +1,10 @@
package input package input
import ( import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
) )
var gitCmd = &cobra.Command{ var gitCmd = &cobra.Command{

View File

@ -1,10 +1,10 @@
package input package input
import ( import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
) )
var jsonCmd = &cobra.Command{ var jsonCmd = &cobra.Command{

View File

@ -6,11 +6,13 @@ import (
"os" "os"
"text/tabwriter" "text/tabwriter"
"dagger.io/go/cmd/dagger/cmd/common" "go.dagger.io/dagger/client"
"dagger.io/go/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"dagger.io/go/dagger" "go.dagger.io/dagger/cmd/dagger/logger"
"dagger.io/go/dagger/compiler" "go.dagger.io/dagger/compiler"
"dagger.io/go/dagger/state" "go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -32,19 +34,19 @@ var listCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
workspace := common.CurrentWorkspace(ctx) workspace := common.CurrentWorkspace(ctx)
environment := common.CurrentEnvironmentState(ctx, workspace) st := common.CurrentEnvironmentState(ctx, workspace)
lg = lg.With(). lg = lg.With().
Str("environment", environment.Name). Str("environment", st.Name).
Logger() Logger()
c, err := dagger.NewClient(ctx, "", false) c, err := client.New(ctx, "", false)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }
_, err = c.Do(ctx, environment, func(lCtx context.Context, lDeploy *dagger.Environment, lSolver dagger.Solver) error { _, err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
inputs := lDeploy.ScanInputs(ctx) inputs := env.ScanInputs(ctx)
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "Input\tType\tValue\tSet by user") fmt.Fprintln(w, "Input\tType\tValue\tSet by user")
@ -71,7 +73,7 @@ var listCmd = &cobra.Command{
inp.Path(), inp.Path(),
getType(inp), getType(inp),
valStr, valStr,
isUserSet(environment, inp), isUserSet(st, inp),
) )
} }

View File

@ -5,11 +5,11 @@ import (
"io" "io"
"os" "os"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/dagger/state"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/state"
) )
// Cmd exposes the top-level command // Cmd exposes the top-level command

View File

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
"syscall" "syscall"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
"golang.org/x/term" "golang.org/x/term"
) )

View File

@ -1,10 +1,10 @@
package input package input
import ( import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
) )
var textCmd = &cobra.Command{ var textCmd = &cobra.Command{

View File

@ -1,10 +1,10 @@
package input package input
import ( import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
) )
var unsetCmd = &cobra.Command{ var unsetCmd = &cobra.Command{

View File

@ -1,10 +1,10 @@
package input package input
import ( import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
) )
var yamlCmd = &cobra.Command{ var yamlCmd = &cobra.Command{

View File

@ -8,10 +8,10 @@ import (
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
) )
var listCmd = &cobra.Command{ var listCmd = &cobra.Command{

View File

@ -1,10 +1,10 @@
package cmd package cmd
import ( import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
) )
var newCmd = &cobra.Command{ var newCmd = &cobra.Command{

View File

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"dagger.io/go/cmd/dagger/cmd/common" "go.dagger.io/dagger/client"
"dagger.io/go/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"dagger.io/go/dagger" "go.dagger.io/dagger/cmd/dagger/logger"
"dagger.io/go/dagger/compiler" "go.dagger.io/dagger/compiler"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -42,7 +42,7 @@ var queryCmd = &cobra.Command{
cuePath = cue.ParsePath(args[0]) cuePath = cue.ParsePath(args[0])
} }
c, err := dagger.NewClient(ctx, "", false) c, err := client.New(ctx, "", false)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }

View File

@ -4,14 +4,14 @@ import (
"os" "os"
"strings" "strings"
"dagger.io/go/cmd/dagger/cmd/input"
"dagger.io/go/cmd/dagger/cmd/output"
"dagger.io/go/cmd/dagger/logger"
"github.com/moby/buildkit/util/appcontext" "github.com/moby/buildkit/util/appcontext"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log" otlog "github.com/opentracing/opentracing-go/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/input"
"go.dagger.io/dagger/cmd/dagger/cmd/output"
"go.dagger.io/dagger/cmd/dagger/logger"
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{

View File

@ -1,8 +1,8 @@
package cmd package cmd
import ( import (
"dagger.io/go/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"

View File

@ -25,7 +25,7 @@ const (
) )
// set by goreleaser or other builder using // set by goreleaser or other builder using
// -ldflags='-X dagger.io/go/cmd/dagger/cmd.version=<version>' // -ldflags='-X go.dagger.io/dagger/cmd/dagger/cmd.version=<version>'
var ( var (
version = defaultVersion version = defaultVersion
versionMessage = "" versionMessage = ""

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"dagger.io/go/cmd/dagger/cmd" "go.dagger.io/dagger/cmd/dagger/cmd"
) )
func main() { func main() {

View File

@ -1,4 +1,4 @@
package dagger package environment
import ( import (
"context" "context"
@ -9,9 +9,10 @@ import (
"cuelang.org/go/cue" "cuelang.org/go/cue"
cueflow "cuelang.org/go/tools/flow" cueflow "cuelang.org/go/tools/flow"
"dagger.io/go/dagger/compiler" "go.dagger.io/dagger/compiler"
"dagger.io/go/dagger/state" "go.dagger.io/dagger/solver"
"dagger.io/go/stdlib" "go.dagger.io/dagger/state"
"go.dagger.io/dagger/stdlib"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext" "github.com/opentracing/opentracing-go/ext"
@ -32,7 +33,7 @@ type Environment struct {
computed *compiler.Value computed *compiler.Value
} }
func NewEnvironment(st *state.State) (*Environment, error) { func New(st *state.State) (*Environment, error) {
e := &Environment{ e := &Environment{
state: st, state: st,
@ -81,7 +82,7 @@ func (e *Environment) Computed() *compiler.Value {
} }
// LoadPlan loads the plan // LoadPlan loads the plan
func (e *Environment) LoadPlan(ctx context.Context, s Solver) error { func (e *Environment) LoadPlan(ctx context.Context, s solver.Solver) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "environment.LoadPlan") span, ctx := opentracing.StartSpanFromContext(ctx, "environment.LoadPlan")
defer span.Finish() defer span.Finish()
@ -165,7 +166,7 @@ func (e *Environment) LocalDirs() map[string]string {
} }
// Up missing values in environment configuration, and write them to state. // Up missing values in environment configuration, and write them to state.
func (e *Environment) Up(ctx context.Context, s Solver) error { func (e *Environment) Up(ctx context.Context, s solver.Solver) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "environment.Up") span, ctx := opentracing.StartSpanFromContext(ctx, "environment.Up")
defer span.Finish() defer span.Finish()
@ -216,7 +217,7 @@ func noOpRunner(t *cueflow.Task) error {
return nil return nil
} }
func newPipelineRunner(computed *compiler.Value, s Solver) cueflow.RunnerFunc { func newPipelineRunner(computed *compiler.Value, s solver.Solver) cueflow.RunnerFunc {
return cueflow.RunnerFunc(func(t *cueflow.Task) error { return cueflow.RunnerFunc(func(t *cueflow.Task) error {
ctx := t.Context() ctx := t.Context()
lg := log. lg := log.
@ -294,5 +295,5 @@ func newPipelineRunner(computed *compiler.Value, s Solver) cueflow.RunnerFunc {
} }
func (e *Environment) ScanInputs(ctx context.Context) []*compiler.Value { func (e *Environment) ScanInputs(ctx context.Context) []*compiler.Value {
return ScanInputs(ctx, e.plan) return scanInputs(ctx, e.plan)
} }

View File

@ -1,10 +1,10 @@
package dagger package environment
import ( import (
"testing" "testing"
"dagger.io/go/dagger/state"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.dagger.io/dagger/state"
) )
func TestLocalDirs(t *testing.T) { func TestLocalDirs(t *testing.T) {
@ -14,7 +14,7 @@ func TestLocalDirs(t *testing.T) {
} }
require.NoError(t, st.SetInput("www.source", state.DirInput("/", []string{}))) require.NoError(t, st.SetInput("www.source", state.DirInput("/", []string{})))
environment, err := NewEnvironment(st) environment, err := New(st)
require.NoError(t, err) require.NoError(t, err)
localdirs := environment.LocalDirs() localdirs := environment.LocalDirs()

View File

@ -1,11 +1,11 @@
package dagger package environment
import ( import (
"context" "context"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"dagger.io/go/dagger/compiler"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
) )
func isReference(val cue.Value) bool { func isReference(val cue.Value) bool {
@ -42,7 +42,7 @@ func isReference(val cue.Value) bool {
return isRef(val) return isRef(val)
} }
func ScanInputs(ctx context.Context, value *compiler.Value) []*compiler.Value { func scanInputs(ctx context.Context, value *compiler.Value) []*compiler.Value {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
inputs := []*compiler.Value{} inputs := []*compiler.Value{}

View File

@ -1,4 +1,4 @@
package dagger package environment
import ( import (
"bytes" "bytes"
@ -25,7 +25,8 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"dagger.io/go/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/solver"
) )
const ( const (
@ -36,14 +37,14 @@ const (
type Pipeline struct { type Pipeline struct {
code *compiler.Value code *compiler.Value
name string name string
s Solver s solver.Solver
state llb.State state llb.State
result bkgw.Reference result bkgw.Reference
image dockerfile2llb.Image image dockerfile2llb.Image
computed *compiler.Value computed *compiler.Value
} }
func NewPipeline(code *compiler.Value, s Solver) *Pipeline { func NewPipeline(code *compiler.Value, s solver.Solver) *Pipeline {
return &Pipeline{ return &Pipeline{
code: code, code: code,
name: code.Path().String(), name: code.Path().String(),
@ -70,7 +71,7 @@ func (p *Pipeline) Result() (llb.State, error) {
} }
func (p *Pipeline) FS() fs.FS { func (p *Pipeline) FS() fs.FS {
return NewBuildkitFS(p.result) return solver.NewBuildkitFS(p.result)
} }
func (p *Pipeline) ImageConfig() dockerfile2llb.Image { func (p *Pipeline) ImageConfig() dockerfile2llb.Image {
@ -641,7 +642,7 @@ func (p *Pipeline) DockerLogin(ctx context.Context, op *compiler.Value, st llb.S
return st, err return st, err
} }
p.s.auth.AddCredentials(target, username, secret) p.s.AddCredentials(target, username, secret)
log. log.
Ctx(ctx). Ctx(ctx).
Debug(). Debug().
@ -862,7 +863,7 @@ func (p *Pipeline) DockerBuild(ctx context.Context, op *compiler.Value, st llb.S
return st, err return st, err
} }
if p.s.noCache { if p.s.NoCache() {
opts["no-cache"] = "" opts["no-cache"] = ""
} }

2
go.mod
View File

@ -1,4 +1,4 @@
module dagger.io/go module go.dagger.io/dagger
go 1.16 go 1.16

View File

@ -1,4 +1,4 @@
package dagger package solver
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package dagger package solver
import ( import (
"context" "context"
@ -12,20 +12,20 @@ import (
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
// registryAuthProvider is a buildkit provider for registry authentication // RegistryAuthProvider is a buildkit provider for registry authentication
// Adapted from: https://github.com/moby/buildkit/blob/master/session/auth/authprovider/authprovider.go // Adapted from: https://github.com/moby/buildkit/blob/master/session/auth/authprovider/authprovider.go
type registryAuthProvider struct { type RegistryAuthProvider struct {
credentials map[string]*bkauth.CredentialsResponse credentials map[string]*bkauth.CredentialsResponse
m sync.RWMutex m sync.RWMutex
} }
func newRegistryAuthProvider() *registryAuthProvider { func NewRegistryAuthProvider() *RegistryAuthProvider {
return &registryAuthProvider{ return &RegistryAuthProvider{
credentials: map[string]*bkauth.CredentialsResponse{}, credentials: map[string]*bkauth.CredentialsResponse{},
} }
} }
func (a *registryAuthProvider) AddCredentials(target, username, secret string) { func (a *RegistryAuthProvider) AddCredentials(target, username, secret string) {
a.m.Lock() a.m.Lock()
defer a.m.Unlock() defer a.m.Unlock()
@ -35,11 +35,11 @@ func (a *registryAuthProvider) AddCredentials(target, username, secret string) {
} }
} }
func (a *registryAuthProvider) Register(server *grpc.Server) { func (a *RegistryAuthProvider) Register(server *grpc.Server) {
bkauth.RegisterAuthServer(server, a) bkauth.RegisterAuthServer(server, a)
} }
func (a *registryAuthProvider) Credentials(ctx context.Context, req *bkauth.CredentialsRequest) (*bkauth.CredentialsResponse, error) { func (a *RegistryAuthProvider) Credentials(ctx context.Context, req *bkauth.CredentialsRequest) (*bkauth.CredentialsResponse, error) {
reqURL, err := parseAuthHost(req.Host) reqURL, err := parseAuthHost(req.Host)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,14 +73,14 @@ func parseAuthHost(host string) (*url.URL, error) {
return url.Parse(host) return url.Parse(host)
} }
func (a *registryAuthProvider) FetchToken(ctx context.Context, req *bkauth.FetchTokenRequest) (rr *bkauth.FetchTokenResponse, err error) { func (a *RegistryAuthProvider) FetchToken(ctx context.Context, req *bkauth.FetchTokenRequest) (rr *bkauth.FetchTokenResponse, err error) {
return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented") return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented")
} }
func (a *registryAuthProvider) GetTokenAuthority(ctx context.Context, req *bkauth.GetTokenAuthorityRequest) (*bkauth.GetTokenAuthorityResponse, error) { func (a *RegistryAuthProvider) GetTokenAuthority(ctx context.Context, req *bkauth.GetTokenAuthorityRequest) (*bkauth.GetTokenAuthorityResponse, error) {
return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented") return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented")
} }
func (a *registryAuthProvider) VerifyTokenAuthority(ctx context.Context, req *bkauth.VerifyTokenAuthorityRequest) (*bkauth.VerifyTokenAuthorityResponse, error) { func (a *RegistryAuthProvider) VerifyTokenAuthority(ctx context.Context, req *bkauth.VerifyTokenAuthorityRequest) (*bkauth.VerifyTokenAuthorityResponse, error) {
return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented") return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented")
} }

View File

@ -1,11 +1,9 @@
package dagger package solver
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"strings"
bk "github.com/moby/buildkit/client" bk "github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/llb"
@ -19,20 +17,20 @@ import (
) )
type Solver struct { type Solver struct {
events chan *bk.SolveStatus opts Opts
control *bk.Client
gw bkgw.Client
auth *registryAuthProvider
noCache bool
} }
func NewSolver(control *bk.Client, gw bkgw.Client, events chan *bk.SolveStatus, auth *registryAuthProvider, noCache bool) Solver { type Opts struct {
Control *bk.Client
Gateway bkgw.Client
Events chan *bk.SolveStatus
Auth *RegistryAuthProvider
NoCache bool
}
func New(opts Opts) Solver {
return Solver{ return Solver{
events: events, opts: opts,
control: control,
gw: gw,
auth: auth,
noCache: noCache,
} }
} }
@ -55,6 +53,14 @@ func invalidateCache(def *llb.Definition) error {
return nil return nil
} }
func (s Solver) NoCache() bool {
return s.opts.NoCache
}
func (s Solver) AddCredentials(target, username, secret string) {
s.opts.Auth.AddCredentials(target, username, secret)
}
func (s Solver) Marshal(ctx context.Context, st llb.State) (*bkpb.Definition, error) { func (s Solver) Marshal(ctx context.Context, st llb.State) (*bkpb.Definition, error) {
// FIXME: do not hardcode the platform // FIXME: do not hardcode the platform
def, err := st.Marshal(ctx, llb.LinuxAmd64) def, err := st.Marshal(ctx, llb.LinuxAmd64)
@ -62,7 +68,7 @@ func (s Solver) Marshal(ctx context.Context, st llb.State) (*bkpb.Definition, er
return nil, err return nil, err
} }
if s.noCache { if s.opts.NoCache {
if err := invalidateCache(def); err != nil { if err := invalidateCache(def); err != nil {
return nil, err return nil, err
} }
@ -72,7 +78,7 @@ func (s Solver) Marshal(ctx context.Context, st llb.State) (*bkpb.Definition, er
} }
func (s Solver) SessionID() string { func (s Solver) SessionID() string {
return s.gw.BuildOpts().SessionID return s.opts.Gateway.BuildOpts().SessionID
} }
func (s Solver) ResolveImageConfig(ctx context.Context, ref string, opts llb.ResolveImageConfigOpt) (dockerfile2llb.Image, error) { func (s Solver) ResolveImageConfig(ctx context.Context, ref string, opts llb.ResolveImageConfigOpt) (dockerfile2llb.Image, error) {
@ -81,7 +87,7 @@ func (s Solver) ResolveImageConfig(ctx context.Context, ref string, opts llb.Res
// Load image metadata and convert to to LLB. // Load image metadata and convert to to LLB.
// Inspired by https://github.com/moby/buildkit/blob/master/frontend/dockerfile/dockerfile2llb/convert.go // Inspired by https://github.com/moby/buildkit/blob/master/frontend/dockerfile/dockerfile2llb/convert.go
// FIXME: this needs to handle platform // FIXME: this needs to handle platform
_, meta, err := s.gw.ResolveImageConfig(ctx, ref, opts) _, meta, err := s.opts.Gateway.ResolveImageConfig(ctx, ref, opts)
if err != nil { if err != nil {
return image, err return image, err
} }
@ -94,12 +100,7 @@ func (s Solver) ResolveImageConfig(ctx context.Context, ref string, opts llb.Res
// Solve will block until the state is solved and returns a Reference. // Solve will block until the state is solved and returns a Reference.
func (s Solver) SolveRequest(ctx context.Context, req bkgw.SolveRequest) (*bkgw.Result, error) { func (s Solver) SolveRequest(ctx context.Context, req bkgw.SolveRequest) (*bkgw.Result, error) {
// call solve return s.opts.Gateway.Solve(ctx, req)
res, err := s.gw.Solve(ctx, req)
if err != nil {
return nil, bkCleanError(err)
}
return res, nil
} }
// Solve will block until the state is solved and returns a Reference. // Solve will block until the state is solved and returns a Reference.
@ -149,7 +150,7 @@ func (s Solver) Export(ctx context.Context, st llb.State, img *dockerfile2llb.Im
opts := bk.SolveOpt{ opts := bk.SolveOpt{
Exports: []bk.ExportEntry{output}, Exports: []bk.ExportEntry{output},
Session: []session.Attachable{s.auth}, Session: []session.Attachable{s.opts.Auth},
} }
ch := make(chan *bk.SolveStatus) ch := make(chan *bk.SolveStatus)
@ -158,11 +159,11 @@ func (s Solver) Export(ctx context.Context, st llb.State, img *dockerfile2llb.Im
// purposes. // purposes.
go func() { go func() {
for event := range ch { for event := range ch {
s.events <- event s.opts.Events <- event
} }
}() }()
return s.control.Build(ctx, opts, "", func(ctx context.Context, c bkgw.Client) (*bkgw.Result, error) { return s.opts.Control.Build(ctx, opts, "", func(ctx context.Context, c bkgw.Client) (*bkgw.Result, error) {
res, err := c.Solve(ctx, bkgw.SolveRequest{ res, err := c.Solve(ctx, bkgw.SolveRequest{
Definition: def, Definition: def,
}) })
@ -203,22 +204,3 @@ func dumpLLB(def *bkpb.Definition) ([]byte, error) {
} }
return json.Marshal(ops) return json.Marshal(ops)
} }
// A helper to remove noise from buildkit error messages.
// FIXME: Obviously a cleaner solution would be nice.
func bkCleanError(err error) error {
noise := []string{
"executor failed running ",
"buildkit-runc did not terminate successfully",
"rpc error: code = Unknown desc = ",
"failed to solve: ",
}
msg := err.Error()
for _, s := range noise {
msg = strings.ReplaceAll(msg, s, "")
}
return errors.New(msg)
}

View File

@ -10,7 +10,7 @@ import (
"cuelang.org/go/cue" "cuelang.org/go/cue"
"dagger.io/go/dagger/compiler" "go.dagger.io/dagger/compiler"
) )
// An input is a value or artifact supplied by the user. // An input is a value or artifact supplied by the user.

View File

@ -9,8 +9,8 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"dagger.io/go/dagger/keychain"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.dagger.io/dagger/keychain"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )