From e09723861f5f7e2c95f7556d84fabc8d2a989b64 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Wed, 13 Jan 2021 17:38:16 -0800 Subject: [PATCH 1/3] re-wire logging on top of zerolog Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/compute.go | 30 ++++++++----- cmd/dagger/cmd/root.go | 22 ++++++--- cmd/dagger/logger/logger.go | 51 +++++++++++++++++++++ dagger/client.go | 90 +++++++++++++++++++++++-------------- dagger/compiler.go | 7 +-- dagger/component.go | 4 +- dagger/component_test.go | 3 +- dagger/compute.go | 8 ++-- dagger/env.go | 74 +++++++++++++++++++++++------- dagger/op.go | 13 +++--- dagger/op_test.go | 9 +++- dagger/script.go | 8 ++-- dagger/script_test.go | 13 ++++-- dagger/spec.go | 6 +-- dagger/types.go | 2 +- dagger/utils.go | 14 +----- dagger/value.go | 6 --- go.mod | 3 ++ go.sum | 18 ++++++++ 19 files changed, 270 insertions(+), 111 deletions(-) create mode 100644 cmd/dagger/logger/logger.go diff --git a/cmd/dagger/cmd/compute.go b/cmd/dagger/cmd/compute.go index 3a617e72..d2abe936 100644 --- a/cmd/dagger/cmd/compute.go +++ b/cmd/dagger/cmd/compute.go @@ -1,37 +1,45 @@ package cmd import ( - "context" "fmt" - "dagger.cloud/go/cmd/dagger/ui" + "dagger.cloud/go/cmd/dagger/logger" "dagger.cloud/go/dagger" + + "github.com/moby/buildkit/util/appcontext" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) var computeCmd = &cobra.Command{ Use: "compute CONFIG", Short: "Compute a configuration", Args: cobra.ExactArgs(1), + PreRun: func(cmd *cobra.Command, args []string) { + // Fix Viper bug for duplicate flags: + // https://github.com/spf13/viper/issues/233 + if err := viper.BindPFlags(cmd.Flags()); err != nil { + panic(err) + } + }, Run: func(cmd *cobra.Command, args []string) { - ctx := context.TODO() + lg := logger.New() + ctx := lg.WithContext(appcontext.Context()) + // FIXME: boot and bootdir should be config fields, not args c, err := dagger.NewClient(ctx, "", "", args[0]) if err != nil { - ui.FatalErr(err) + lg.Fatal().Err(err).Msg("unable to create client") } // FIXME: configure which config to compute (duh) // FIXME: configure inputs - ui.Info("Running") + lg.Info().Msg("running") output, err := c.Compute(ctx) if err != nil { - ui.FatalErr(err) + lg.Fatal().Err(err).Msg("failed to compute") } - ui.Info("Processing output") + lg.Info().Msg("processing output") fmt.Println(output.JSON()) }, } - -func init() { - // computeCmd.Flags().StringP("catalog", "c", "", "Cue package catalog") -} diff --git a/cmd/dagger/cmd/root.go b/cmd/dagger/cmd/root.go index 551f9714..7b75a7a2 100644 --- a/cmd/dagger/cmd/root.go +++ b/cmd/dagger/cmd/root.go @@ -1,8 +1,11 @@ package cmd import ( - "dagger.cloud/go/cmd/dagger/ui" + "strings" + + "dagger.cloud/go/cmd/dagger/logger" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var rootCmd = &cobra.Command{ @@ -11,9 +14,9 @@ var rootCmd = &cobra.Command{ } func init() { - // --debug - rootCmd.PersistentFlags().Bool("debug", false, "Enable debug mode") - // --workspace + rootCmd.PersistentFlags().String("log-format", "", "Log format (json, pretty). Defaults to json if the terminal is not a tty") + rootCmd.PersistentFlags().StringP("log-level", "l", "debug", "Log level") + rootCmd.AddCommand( computeCmd, // Create an env @@ -26,10 +29,19 @@ func init() { // computeCmd, // listCmd, ) + + if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil { + panic(err) + } + viper.SetEnvPrefix("dagger") + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) + viper.AutomaticEnv() } func Execute() { + lg := logger.New() + if err := rootCmd.Execute(); err != nil { - ui.FatalErr(err) + lg.Fatal().Err(err).Msg("failed to execute command") } } diff --git a/cmd/dagger/logger/logger.go b/cmd/dagger/logger/logger.go new file mode 100644 index 00000000..a0c2a55f --- /dev/null +++ b/cmd/dagger/logger/logger.go @@ -0,0 +1,51 @@ +// Logger utilities for the CLI +// +// These utilities rely on command line flags to set up the logger, therefore +// they are tightly coupled with the CLI and should not be used outside. + +package logger + +import ( + "fmt" + "os" + + "github.com/rs/zerolog" + "github.com/spf13/viper" + "golang.org/x/term" +) + +func New() zerolog.Logger { + logger := zerolog. + New(os.Stderr). + With(). + Timestamp(). + Logger() + + if prettyLogs() { + logger = logger.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + } else { + logger = logger.With().Timestamp().Caller().Logger() + } + + level := viper.GetString("log-level") + lvl, err := zerolog.ParseLevel(level) + if err != nil { + panic(err) + } + return logger.Level(lvl) +} + +func prettyLogs() bool { + switch f := viper.GetString("log-format"); f { + case "json": + return false + case "pretty": + return true + case "": + return term.IsTerminal(int(os.Stdout.Fd())) + default: + fmt.Fprintf(os.Stderr, "invalid --log-format %q\n", f) + os.Exit(1) + } + return false +} diff --git a/dagger/client.go b/dagger/client.go index ee2c782b..3a57d38e 100644 --- a/dagger/client.go +++ b/dagger/client.go @@ -11,6 +11,8 @@ import ( "github.com/pkg/errors" "golang.org/x/sync/errgroup" + "github.com/rs/zerolog/log" + // Cue "cuelang.org/go/cue" @@ -80,12 +82,12 @@ func NewClient(ctx context.Context, host, boot, bootdir string) (*Client, error) }, nil } -func (c *Client) LocalDirs() ([]string, error) { +func (c *Client) LocalDirs(ctx context.Context) ([]string, error) { boot, err := c.BootScript() if err != nil { return nil, err } - return boot.LocalDirs() + return boot.LocalDirs(ctx) } func (c *Client) BootScript() (*Script, error) { @@ -102,6 +104,8 @@ func (c *Client) BootScript() (*Script, error) { } func (c *Client) Compute(ctx context.Context) (*Value, error) { + lg := log.Ctx(ctx) + cc := &Compiler{} out, err := cc.EmptyStruct() if err != nil { @@ -140,7 +144,7 @@ func (c *Client) Compute(ctx context.Context) (*Value, error) { }) eg.Go(func() error { - return c.dockerprintfn(dispCtx, eventsDockerPrint, os.Stderr) + return c.dockerprintfn(dispCtx, eventsDockerPrint, lg) }) } else { eg.Go(func() error { @@ -157,6 +161,8 @@ func (c *Client) Compute(ctx context.Context) (*Value, error) { } func (c *Client) buildfn(ctx context.Context, ch chan *bk.SolveStatus, w io.WriteCloser) error { + lg := log.Ctx(ctx) + boot, err := c.BootScript() if err != nil { return errors.Wrap(err, "assemble boot script") @@ -165,7 +171,7 @@ func (c *Client) buildfn(ctx context.Context, ch chan *bk.SolveStatus, w io.Writ if err != nil { return errors.Wrap(err, "serialize boot script") } - debugf("client: assembled boot script: %s\n", bootSource) + lg.Debug().Bytes("bootSource", bootSource).Msg("assembled boot script") // Setup solve options opts := bk.SolveOpt{ FrontendAttrs: map[string]string{ @@ -184,7 +190,7 @@ func (c *Client) buildfn(ctx context.Context, ch chan *bk.SolveStatus, w io.Writ }, } // Connect local dirs - localdirs, err := c.LocalDirs() + localdirs, err := c.LocalDirs(ctx) if err != nil { return errors.Wrap(err, "connect local dirs") } @@ -199,14 +205,19 @@ func (c *Client) buildfn(ctx context.Context, ch chan *bk.SolveStatus, w io.Writ } for k, v := range resp.ExporterResponse { // FIXME consume exporter response - fmt.Printf("exporter response: %s=%s\n", k, v) + lg. + Debug(). + Str("key", k). + Str("value", v). + Msg("exporter response") } return nil } // Read tar export stream from buildkit Build(), and extract cue output -func (c *Client) outputfn(_ context.Context, r io.Reader, out *Value) error { - defer debugf("outputfn complete") +func (c *Client) outputfn(ctx context.Context, r io.Reader, out *Value) error { + lg := log.Ctx(ctx) + tr := tar.NewReader(r) for { h, err := tr.Next() @@ -216,11 +227,17 @@ func (c *Client) outputfn(_ context.Context, r io.Reader, out *Value) error { if err != nil { return errors.Wrap(err, "read tar stream") } + + lg := lg. + With(). + Str("file", h.Name). + Logger() + if !strings.HasSuffix(h.Name, ".cue") { - debugf("skipping non-cue file from exporter tar stream: %s", h.Name) + lg.Debug().Msg("skipping non-cue file from exporter tar stream") continue } - debugf("outputfn: compiling & merging %q", h.Name) + lg.Debug().Msg("outputfn: compiling & merging") cc := out.Compiler() v, err := cc.Compile(h.Name, tr) @@ -230,7 +247,6 @@ func (c *Client) outputfn(_ context.Context, r io.Reader, out *Value) error { if err := out.Fill(v); err != nil { return errors.Wrap(err, h.Name) } - debugf("outputfn: DONE: compiling & merging %q", h.Name) } return nil } @@ -254,7 +270,7 @@ func (n Node) ComponentPath() cue.Path { return cue.MakePath(parts...) } -func (n Node) Logf(msg string, args ...interface{}) { +func (n Node) Logf(ctx context.Context, msg string, args ...interface{}) { componentPath := n.ComponentPath().String() args = append([]interface{}{componentPath}, args...) if msg != "" && !strings.HasSuffix(msg, "\n") { @@ -263,34 +279,41 @@ func (n Node) Logf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, "[%s] "+msg, args...) } -func (n Node) LogStream(nStream int, data []byte) { - var stream string +func (n Node) LogStream(ctx context.Context, nStream int, data []byte) { + lg := log. + Ctx(ctx). + With(). + Str("path", n.ComponentPath().String()). + Logger() + switch nStream { case 1: - stream = "stdout" + lg = lg.With().Str("stream", "stdout").Logger() case 2: - stream = "stderr" + lg = lg.With().Str("stream", "stderr").Logger() default: - stream = fmt.Sprintf("%d", nStream) - } - // FIXME: use bufio reader? - lines := strings.Split(string(data), "\n") - for _, line := range lines { - n.Logf("[%s] %s", stream, line) + lg = lg.With().Str("stream", fmt.Sprintf("%d", nStream)).Logger() } + + lg.Debug().Msg(string(data)) } -func (n Node) LogError(errmsg string) { - n.Logf("ERROR: %s", bkCleanError(errmsg)) +func (n Node) LogError(ctx context.Context, errmsg string) { + log. + Ctx(ctx). + Error(). + Str("path", n.ComponentPath().String()). + Msg(bkCleanError(errmsg)) } func (c *Client) printfn(ctx context.Context, ch chan *bk.SolveStatus) error { + lg := log.Ctx(ctx) + // Node status mapped to buildkit vertex digest nodesByDigest := map[string]*Node{} // Node status mapped to cue path nodesByPath := map[string]*Node{} - defer debugf("printfn complete") for { select { case <-ctx.Done(): @@ -299,11 +322,13 @@ func (c *Client) printfn(ctx context.Context, ch chan *bk.SolveStatus) error { if !ok { return nil } - debugf("status event: vertexes:%d statuses:%d logs:%d\n", - len(status.Vertexes), - len(status.Statuses), - len(status.Logs), - ) + lg. + Debug(). + Int("vertexes", len(status.Vertexes)). + Int("statuses", len(status.Statuses)). + Int("logs", len(status.Logs)). + Msg("status event") + for _, v := range status.Vertexes { // FIXME: insert raw buildkit telemetry here (ie for debugging, etc.) @@ -320,12 +345,12 @@ func (c *Client) printfn(ctx context.Context, ch chan *bk.SolveStatus) error { nodesByPath[n.Path.String()] = n nodesByDigest[n.Digest.String()] = n if n.Error != "" { - n.LogError(n.Error) + n.LogError(ctx, n.Error) } } for _, log := range status.Logs { if n, ok := nodesByDigest[log.Vertex.String()]; ok { - n.LogStream(log.Stream, log.Data) + n.LogStream(ctx, log.Stream, log.Data) } } // debugJSON(status) @@ -351,7 +376,6 @@ func bkCleanError(msg string) string { } func (c *Client) dockerprintfn(ctx context.Context, ch chan *bk.SolveStatus, out io.Writer) error { - defer debugf("dockerprintfn complete") var cons console.Console // FIXME: use smarter writer from blr return progressui.DisplaySolveStatus(ctx, "", cons, out, ch) diff --git a/dagger/compiler.go b/dagger/compiler.go index 237cbe3f..9ca9d271 100644 --- a/dagger/compiler.go +++ b/dagger/compiler.go @@ -11,6 +11,7 @@ import ( cueerrors "cuelang.org/go/cue/errors" cueload "cuelang.org/go/cue/load" "github.com/pkg/errors" + "github.com/rs/zerolog/log" ) // Polyfill for a cue runtime @@ -66,8 +67,8 @@ func (cc *Compiler) CompileScript(name string, src interface{}) (*Script, error) // Build a cue configuration tree from the files in fs. func (cc *Compiler) Build(ctx context.Context, fs FS, args ...string) (*Value, error) { - debugf("Compiler.Build") - defer debugf("COMPLETE: Compiler.Build") + lg := log.Ctx(ctx) + // The CUE overlay needs to be prefixed by a non-conflicting path with the // local filesystem, otherwise Cue will merge the Overlay with whatever Cue // files it finds locally. @@ -80,7 +81,7 @@ func (cc *Compiler) Build(ctx context.Context, fs FS, args ...string) (*Value, e buildArgs := args err := fs.Walk(ctx, func(p string, f Stat) error { - debugf(" Compiler.Build: processing %q", p) + lg.Debug().Str("path", p).Msg("Compiler.Build: processing") if f.IsDir() { return nil } diff --git a/dagger/component.go b/dagger/component.go index d831d4aa..6acdeb21 100644 --- a/dagger/component.go +++ b/dagger/component.go @@ -67,10 +67,10 @@ func (c *Component) Execute(ctx context.Context, fs FS, out Fillable) (FS, error return script.Execute(ctx, fs, out) } -func (c *Component) Walk(fn func(*Op) error) error { +func (c *Component) Walk(ctx context.Context, fn func(*Op) error) error { script, err := c.ComputeScript() if err != nil { return err } - return script.Walk(fn) + return script.Walk(ctx, fn) } diff --git a/dagger/component_test.go b/dagger/component_test.go index 874ea752..02d4bc45 100644 --- a/dagger/component_test.go +++ b/dagger/component_test.go @@ -1,6 +1,7 @@ package dagger import ( + "context" "testing" ) @@ -31,7 +32,7 @@ func TestValidateSimpleComponent(t *testing.T) { t.Fatal(err) } n := 0 - if err := s.Walk(func(op *Op) error { + if err := s.Walk(context.TODO(), func(op *Op) error { n++ return nil }); err != nil { diff --git a/dagger/compute.go b/dagger/compute.go index 046e0b0a..d615a621 100644 --- a/dagger/compute.go +++ b/dagger/compute.go @@ -6,17 +6,19 @@ import ( cueerrors "cuelang.org/go/cue/errors" bkgw "github.com/moby/buildkit/frontend/gateway/client" + "github.com/rs/zerolog/log" ) // Buildkit compute entrypoint (BK calls if "solve" or "build") // Use by wrapping in a buildkit client Build call, or buildkit frontend. func Compute(ctx context.Context, c bkgw.Client) (r *bkgw.Result, err error) { + lg := log.Ctx(ctx) + // FIXME: wrap errors to avoid crashing buildkit Build() // with cue error types (why??) defer func() { if err != nil { err = fmt.Errorf("%s", cueerrors.Details(err, nil)) - debugf("execute returned an error. Wrapping...") } }() // Retrieve boot script form client @@ -24,12 +26,12 @@ func Compute(ctx context.Context, c bkgw.Client) (r *bkgw.Result, err error) { if err != nil { return nil, err } - debugf("computing env") + lg.Debug().Msg("computing env") // Compute output overlay if err := env.Compute(ctx); err != nil { return nil, err } - debugf("exporting env") + lg.Debug().Msg("exporting env") // Export env to a cue directory outdir := NewSolver(c).Scratch() outdir, err = env.Export(outdir) diff --git a/dagger/env.go b/dagger/env.go index 2f9763bc..726b5707 100644 --- a/dagger/env.go +++ b/dagger/env.go @@ -8,6 +8,7 @@ import ( "cuelang.org/go/cue" cueflow "cuelang.org/go/tools/flow" "github.com/pkg/errors" + "github.com/rs/zerolog/log" ) type Env struct { @@ -29,7 +30,14 @@ type Env struct { // Initialize a new environment func NewEnv(ctx context.Context, s Solver, bootsrc, inputsrc string) (*Env, error) { - debugf("NewEnv(boot=%q input=%q)", bootsrc, inputsrc) + lg := log.Ctx(ctx) + + lg. + Debug(). + Str("boot", bootsrc). + Str("input", inputsrc). + Msg("New Env") + cc := &Compiler{} // 1. Compile & execute boot script boot, err := cc.CompileScript("boot.cue", bootsrc) @@ -42,13 +50,13 @@ func NewEnv(ctx context.Context, s Solver, bootsrc, inputsrc string) (*Env, erro } // 2. load cue files produced by boot script // FIXME: BuildAll() to force all files (no required package..) - debugf("building cue configuration from boot state") + lg.Debug().Msg("building cue configuration from boot state") base, err := cc.Build(ctx, bootfs) if err != nil { return nil, errors.Wrap(err, "load base config") } // 3. Compile & merge input overlay (user settings, input directories, secrets.) - debugf("Loading input overlay") + lg.Debug().Msg("loading input overlay") input, err := cc.Compile("input.cue", inputsrc) if err != nil { return nil, err @@ -58,7 +66,11 @@ func NewEnv(ctx context.Context, s Solver, bootsrc, inputsrc string) (*Env, erro return nil, errors.Wrap(err, "merge base & input") } - debugf("ENV: base=%q input=%q", base.JSON(), input.JSON()) + lg. + Debug(). + Str("base", base.JSON().String()). + Str("input", input.JSON().String()). + Msg("ENV") return &Env{ base: base, @@ -70,9 +82,18 @@ func NewEnv(ctx context.Context, s Solver, bootsrc, inputsrc string) (*Env, erro // Compute missing values in env configuration, and write them to state. func (env *Env) Compute(ctx context.Context) error { - debugf("Computing environment") + lg := log.Ctx(ctx) + output, err := env.Walk(ctx, func(c *Component, out Fillable) error { - debugf(" [Env.Compute] processing %s", c.Value().Path().String()) + lg := lg. + With(). + Str("path", c.Value().Path().String()). + Logger() + ctx := lg.WithContext(ctx) + + lg. + Debug(). + Msg("[Env.Compute] processing") _, err := c.Compute(ctx, env.s, out) return err }) @@ -103,35 +124,51 @@ type EnvWalkFunc func(*Component, Fillable) error // Walk components and return any computed values func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { - debugf("Env.Walk") - defer debugf("COMPLETE: Env.Walk") - l := sync.Mutex{} + lg := log.Ctx(ctx) + // Cueflow cue instance // FIXME: make this cleaner in *Value by keeping intermediary instances flowInst, err := env.base.CueInst().Fill(env.input.CueInst().Value()) if err != nil { return nil, err } - debugf("walking: \n----\n%s\n----\n", env.cc.Wrap(flowInst.Value(), flowInst).JSON()) - // Initialize empty output + + lg. + Debug(). + Str("value", env.cc.Wrap(flowInst.Value(), flowInst).JSON().String()). + Msg("walking") + + // Initialize empty output out, err := env.cc.EmptyStruct() if err != nil { return nil, err } + + l := sync.Mutex{} + // Cueflow config flowCfg := &cueflow.Config{ UpdateFunc: func(c *cueflow.Controller, t *cueflow.Task) error { l.Lock() defer l.Unlock() - debugf("compute step") + if t == nil { return nil } - debugf("cueflow task %q: %s", t.Path().String(), t.State().String()) + + lg := lg. + With(). + Str("path", t.Path().String()). + Str("state", t.State().String()). + Logger() + + lg. + Debug(). + Msg("cueflow task") if t.State() != cueflow.Terminated { return nil } - debugf("cueflow task %q: filling result", t.Path().String()) + lg.Debug().Msg("cueflow task: filling result") // Merge task value into output var err error // FIXME: does cueflow.Task.Value() contain only filled values, @@ -147,7 +184,13 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { flowMatchFn := func(v cue.Value) (cueflow.Runner, error) { l.Lock() defer l.Unlock() - debugf("Env.Walk: processing %s", v.Path().String()) + + lg := lg. + With(). + Str("path", v.Path().String()). + Logger() + + lg.Debug().Msg("Env.Walk: processing") val := env.cc.Wrap(v, flowInst) c, err := val.Component() if os.IsNotExist(err) { @@ -160,6 +203,7 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { return cueflow.RunnerFunc(func(t *cueflow.Task) error { l.Lock() defer l.Unlock() + return fn(c, t) }), nil } diff --git a/dagger/op.go b/dagger/op.go index 11a89710..72ea0b02 100644 --- a/dagger/op.go +++ b/dagger/op.go @@ -7,6 +7,7 @@ import ( "github.com/moby/buildkit/client/llb" "github.com/pkg/errors" + "github.com/rs/zerolog/log" ) type Op struct { @@ -21,14 +22,15 @@ func (op *Op) Execute(ctx context.Context, fs FS, out Fillable) (FS, error) { return action(ctx, fs, out) } -func (op *Op) Walk(fn func(*Op) error) error { - debugf("Walk %v", op.v) +func (op *Op) Walk(ctx context.Context, fn func(*Op) error) error { + lg := log.Ctx(ctx) + + lg.Debug().Interface("v", op.v).Msg("Op.Walk") isCopy := (op.Validate("#Copy") == nil) isLoad := (op.Validate("#Load") == nil) if isCopy || isLoad { - debugf("MATCH %v", op.v) if from, err := op.Get("from").Executable(); err == nil { - if err := from.Walk(fn); err != nil { + if err := from.Walk(ctx, fn); err != nil { return err } } @@ -37,7 +39,7 @@ func (op *Op) Walk(fn func(*Op) error) error { if err := op.Validate("#Exec"); err == nil { return op.Get("mount").RangeStruct(func(k string, v *Value) error { if from, err := op.Get("from").Executable(); err == nil { - if err := from.Walk(fn); err != nil { + if err := from.Walk(ctx, fn); err != nil { return err } } @@ -75,7 +77,6 @@ func (op *Op) Action() (Action, error) { } for def, action := range actions { if err := op.Validate(def); err == nil { - op.v.Debugf("OP MATCH: %s", def) return action, nil } } diff --git a/dagger/op_test.go b/dagger/op_test.go index 053db9c6..28eab6ca 100644 --- a/dagger/op_test.go +++ b/dagger/op_test.go @@ -1,10 +1,13 @@ package dagger import ( + "context" "testing" ) func TestLocalMatch(t *testing.T) { + ctx := context.TODO() + cc := &Compiler{} src := `do: "local", dir: "foo"` v, err := cc.Compile("", src) @@ -16,7 +19,7 @@ func TestLocalMatch(t *testing.T) { t.Fatal(err) } n := 0 - err = op.Walk(func(op *Op) error { + err = op.Walk(ctx, func(op *Op) error { n++ return nil }) @@ -29,6 +32,8 @@ func TestLocalMatch(t *testing.T) { } func TestCopyMatch(t *testing.T) { + ctx := context.TODO() + cc := &Compiler{} src := `do: "copy", from: [{do: "local", dir: "foo"}]` v, err := cc.Compile("", src) @@ -43,7 +48,7 @@ func TestCopyMatch(t *testing.T) { t.Fatal(err) } n := 0 - err = op.Walk(func(op *Op) error { + err = op.Walk(ctx, func(op *Op) error { n++ return nil }) diff --git a/dagger/script.go b/dagger/script.go index a76495bf..b15752f5 100644 --- a/dagger/script.go +++ b/dagger/script.go @@ -41,22 +41,22 @@ func (s *Script) Execute(ctx context.Context, fs FS, out Fillable) (FS, error) { return fs, err } -func (s *Script) Walk(fn func(op *Op) error) error { +func (s *Script) Walk(ctx context.Context, fn func(op *Op) error) error { return s.v.RangeList(func(idx int, v *Value) error { op, err := v.Op() if err != nil { return errors.Wrapf(err, "validate op %d/%d", idx+1, s.v.Len()) } - if err := op.Walk(fn); err != nil { + if err := op.Walk(ctx, fn); err != nil { return err } return nil }) } -func (s *Script) LocalDirs() ([]string, error) { +func (s *Script) LocalDirs(ctx context.Context) ([]string, error) { var dirs []string - err := s.Walk(func(op *Op) error { + err := s.Walk(ctx, func(op *Op) error { if err := op.Validate("#Local"); err != nil { return nil } diff --git a/dagger/script_test.go b/dagger/script_test.go index 1c24d1a0..b5a2eb65 100644 --- a/dagger/script_test.go +++ b/dagger/script_test.go @@ -1,6 +1,7 @@ package dagger import ( + "context" "strings" "testing" ) @@ -17,6 +18,8 @@ func TestValidateEmptyValue(t *testing.T) { } func TestLocalScript(t *testing.T) { + ctx := context.TODO() + cc := &Compiler{} src := `[{do: "local", dir: "foo"}]` v, err := cc.Compile("", src) @@ -28,7 +31,7 @@ func TestLocalScript(t *testing.T) { t.Fatal(err) } n := 0 - err = s.Walk(func(op *Op) error { + err = s.Walk(ctx, func(op *Op) error { n++ return nil }) @@ -41,6 +44,8 @@ func TestLocalScript(t *testing.T) { } func TestWalkBootScript(t *testing.T) { + ctx := context.TODO() + cc := &Compiler{} cfg, err := cc.Compile("clientconfig.cue", defaultBootScript) if err != nil { @@ -50,7 +55,7 @@ func TestWalkBootScript(t *testing.T) { if err != nil { t.Fatal(err) } - dirs, err := script.LocalDirs() + dirs, err := script.LocalDirs(ctx) if err != nil { t.Fatal(err) } @@ -64,6 +69,8 @@ func TestWalkBootScript(t *testing.T) { func TestWalkBiggerScript(t *testing.T) { t.Skip("FIXME") + + ctx := context.TODO() cc := &Compiler{} script, err := cc.CompileScript("boot.cue", ` [ @@ -102,7 +109,7 @@ func TestWalkBiggerScript(t *testing.T) { if err != nil { t.Fatal(err) } - dirs, err := script.LocalDirs() + dirs, err := script.LocalDirs(ctx) if err != nil { t.Fatal(err) } diff --git a/dagger/spec.go b/dagger/spec.go index e061ecac..295d89b4 100644 --- a/dagger/spec.go +++ b/dagger/spec.go @@ -1,10 +1,9 @@ package dagger import ( - "fmt" - "cuelang.org/go/cue" cueerrors "cuelang.org/go/cue/errors" + "github.com/pkg/errors" ) // Cue spec validator @@ -18,8 +17,7 @@ func (s Spec) Validate(v *Value, defpath string) (err error) { // FIXME: there is probably a cleaner way to do this. defer func() { if err != nil { - //debugf("ERROR while validating %v against %v err=%q", v, defpath, err) - err = fmt.Errorf("%s", cueerrors.Details(err, nil)) + err = errors.New(cueerrors.Details(err, nil)) } }() diff --git a/dagger/types.go b/dagger/types.go index 695e56f1..10a3b5d4 100644 --- a/dagger/types.go +++ b/dagger/types.go @@ -7,7 +7,7 @@ import ( // Implemented by Component, Script, Op type Executable interface { Execute(context.Context, FS, Fillable) (FS, error) - Walk(func(*Op) error) error + Walk(context.Context, func(*Op) error) error } // Something which can be filled in-place with a cue value diff --git a/dagger/utils.go b/dagger/utils.go index 678c3f97..a6e517b7 100644 --- a/dagger/utils.go +++ b/dagger/utils.go @@ -3,24 +3,14 @@ package dagger import ( "crypto/rand" "fmt" - "os" - "strings" "cuelang.org/go/cue" cueerrors "cuelang.org/go/cue/errors" + "github.com/pkg/errors" ) func cueErr(err error) error { - return fmt.Errorf("%s", cueerrors.Details(err, &cueerrors.Config{})) -} - -func debugf(msg string, args ...interface{}) { - if !strings.HasSuffix(msg, "\n") { - msg += "\n" - } - if os.Getenv("DEBUG") != "" { - fmt.Fprintf(os.Stderr, msg, args...) - } + return errors.New(cueerrors.Details(err, &cueerrors.Config{})) } func randomID(size int) (string, error) { diff --git a/dagger/value.go b/dagger/value.go index f82aad42..d4ee3d62 100644 --- a/dagger/value.go +++ b/dagger/value.go @@ -319,12 +319,6 @@ func (v *Value) Compiler() *Compiler { return v.cc } -func (v *Value) Debugf(msg string, args ...interface{}) { - prefix := v.Path().String() - args = append([]interface{}{prefix}, args...) - debugf("%s: "+msg, args...) -} - func (v *Value) Wrap(v2 cue.Value) *Value { return wrapValue(v2, v.inst, v.cc) } diff --git a/go.mod b/go.mod index 65ddf13c..efbedcac 100644 --- a/go.mod +++ b/go.mod @@ -10,9 +10,12 @@ require ( github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db github.com/moby/buildkit v0.8.1 github.com/pkg/errors v0.9.1 + github.com/rs/zerolog v1.20.0 github.com/spf13/cobra v1.0.0 + github.com/spf13/viper v1.7.0 github.com/tonistiigi/fsutil v0.0.0-20201103201449-0834f99b7b85 golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 + golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 gopkg.in/yaml.v3 v3.0.0-20200506231410-2ff61e1afc86 // indirect ) diff --git a/go.sum b/go.sum index 3015c2ac..70e9f97a 100644 --- a/go.sum +++ b/go.sum @@ -312,6 +312,7 @@ github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -461,6 +462,7 @@ github.com/googleapis/gnostic v0.2.2/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTV github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg= github.com/gookit/color v1.2.4/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg= github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/goreleaser/goreleaser v0.136.0/go.mod h1:wiKrPUeSNh6Wu8nUHxZydSOVQ/OZvOaO7DTtFqie904= github.com/goreleaser/nfpm v1.2.1/go.mod h1:TtWrABZozuLOttX2uDlYyECfQX7x5XYkVxhjYcR6G9w= @@ -514,6 +516,7 @@ github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= @@ -551,6 +554,7 @@ github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= @@ -583,6 +587,7 @@ github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= @@ -626,6 +631,7 @@ github.com/mitchellh/hashstructure v1.0.0/go.mod h1:QjSHrPWS+BGUVBYkbTZWEnOh3G1D github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.3.1 h1:cCBH2gTD2K0OtLlv/Y5H01VQCqmlDxz30kS5Y5bqfLA= github.com/mitchellh/mapstructure v1.3.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A= github.com/moby/buildkit v0.8.1 h1:zrGxLwffKM8nVxBvaJa7H404eQLfqlg1GB6YVIzXVQ0= @@ -711,6 +717,7 @@ github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pelletier/go-toml v1.8.0 h1:Keo9qb7iRJs2voHvunFtuuYFsbWeOBh8/P9v/kVMFtw= github.com/pelletier/go-toml v1.8.0/go.mod h1:D6yutnOGMveHEPV7VQOuvI/gXY61bv+9bAOTRnLElKs= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/phayes/checkstyle v0.0.0-20170904204023-bfd46e6a821d/go.mod h1:3OzsM7FXDQlpCiw2j81fOmAwQLnZnLGXVKUzeKQXIAw= @@ -764,6 +771,8 @@ github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369 h1:wdCVGtPadWC/ZuuLC7Hv58VQ5UF7V98ewE71n5mJfrM= github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs= +github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= github.com/rubiojr/go-vhd v0.0.0-20160810183302-0bfd3b39853c/go.mod h1:DM5xW0nvfNNm2uytzsvhI3OnX8uzaRAg8UX/CnDqbto= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -794,9 +803,11 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/assertions v1.0.0 h1:UVQPSSmc3qtTi+zPPkCXvZX9VvW/xT/NsRvKfwY81a8= github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM= github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -804,8 +815,10 @@ github.com/sourcegraph/go-diff v0.5.1/go.mod h1:j2dHj3m8aZgQO8lMTcTnBcXkRRRqi34c github.com/sourcegraph/go-diff v0.5.3/go.mod h1:v9JDtjCE4HHHCZGId75rg8gkKKa98RVjBcBGsVmMmak= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= @@ -813,6 +826,7 @@ github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tL github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.1-0.20171106142849-4c012f6dcd95/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= @@ -823,6 +837,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/spf13/viper v1.6.1/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k= +github.com/spf13/viper v1.7.0 h1:xVKxvI7ouOI5I+U9s2eeiUfMaWBVoXA3AWskkrqK0VM= github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -835,6 +850,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2 h1:b6uOv7YOFK0TYG7HtkIgExQo+2RdLuwRft63jn2HWj8= @@ -1148,6 +1164,7 @@ golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/tools v0.0.0-20190706070813-72ffa07ba3db/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= golang.org/x/tools v0.0.0-20190719005602-e377ae9d6386/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190910044552-dd2b5c81c578/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -1261,6 +1278,7 @@ gopkg.in/gcfg.v1 v1.2.0/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o= gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/ini.v1 v1.56.0 h1:DPMeDvGTM54DXbPkVIZsp19fp/I2K7zwA/itHYHKo8Y= gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= From f8a2109aba27644b5cfb16afbaf72ffac6f5f808 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Wed, 13 Jan 2021 18:11:46 -0800 Subject: [PATCH 2/3] env.walk: inject contextual logging information Signed-off-by: Andrea Luzzardi --- dagger/env.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/dagger/env.go b/dagger/env.go index 726b5707..55f4e268 100644 --- a/dagger/env.go +++ b/dagger/env.go @@ -82,14 +82,8 @@ func NewEnv(ctx context.Context, s Solver, bootsrc, inputsrc string) (*Env, erro // Compute missing values in env configuration, and write them to state. func (env *Env) Compute(ctx context.Context) error { - lg := log.Ctx(ctx) - - output, err := env.Walk(ctx, func(c *Component, out Fillable) error { - lg := lg. - With(). - Str("path", c.Value().Path().String()). - Logger() - ctx := lg.WithContext(ctx) + output, err := env.Walk(ctx, func(ctx context.Context, c *Component, out Fillable) error { + lg := log.Ctx(ctx) lg. Debug(). @@ -120,7 +114,7 @@ func (env *Env) Export(fs FS) (FS, error) { return fs, nil } -type EnvWalkFunc func(*Component, Fillable) error +type EnvWalkFunc func(context.Context, *Component, Fillable) error // Walk components and return any computed values func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { @@ -138,7 +132,7 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { Str("value", env.cc.Wrap(flowInst.Value(), flowInst).JSON().String()). Msg("walking") - // Initialize empty output + // Initialize empty output out, err := env.cc.EmptyStruct() if err != nil { return nil, err @@ -162,9 +156,7 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { Str("state", t.State().String()). Logger() - lg. - Debug(). - Msg("cueflow task") + lg.Debug().Msg("cueflow task") if t.State() != cueflow.Terminated { return nil } @@ -189,6 +181,7 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { With(). Str("path", v.Path().String()). Logger() + ctx := lg.WithContext(ctx) lg.Debug().Msg("Env.Walk: processing") val := env.cc.Wrap(v, flowInst) @@ -204,7 +197,7 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { l.Lock() defer l.Unlock() - return fn(c, t) + return fn(ctx, c, t) }), nil } // Orchestrate execution with cueflow From d15ec726cf589cfa77bea0b51686747de9da6093 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Wed, 13 Jan 2021 18:19:36 -0800 Subject: [PATCH 3/3] cmd: remove ui package Signed-off-by: Andrea Luzzardi --- cmd/dagger/ui/ui.go | 144 -------------------------------------------- 1 file changed, 144 deletions(-) delete mode 100644 cmd/dagger/ui/ui.go diff --git a/cmd/dagger/ui/ui.go b/cmd/dagger/ui/ui.go deleted file mode 100644 index 382b920a..00000000 --- a/cmd/dagger/ui/ui.go +++ /dev/null @@ -1,144 +0,0 @@ -package ui - -import ( - "fmt" - "hash/adler32" - "io" - "os" - "unicode/utf8" - - "github.com/mitchellh/colorstring" -) - -var ( - // Colorize is the main colorizer - Colorize = colorstring.Colorize{ - Colors: colorstring.DefaultColors, - Reset: true, - } -) - -// Finfo prints an info message to w. -func Finfo(w io.Writer, msg string, args ...interface{}) { - fmt.Fprintf(w, Colorize.Color("[bold][blue]info[reset] %s\n"), fmt.Sprintf(msg, args...)) -} - -// Info prints an info message. -func Info(msg string, args ...interface{}) { - Finfo(os.Stdout, msg, args...) -} - -// Fverbose prints a verbose message to w. -func Fverbose(w io.Writer, msg string, args ...interface{}) { - fmt.Fprintf(w, Colorize.Color("[dim]%s\n"), fmt.Sprintf(msg, args...)) -} - -// Verbose prints a verbose message. -func Verbose(msg string, args ...interface{}) { - Fverbose(os.Stdout, msg, args...) -} - -// Fsuccess prints a success message to w. -func Fsuccess(w io.Writer, msg string, args ...interface{}) { - fmt.Fprintf(w, Colorize.Color("[bold][green]success[reset] %s\n"), fmt.Sprintf(msg, args...)) -} - -// Success prints a success message. -func Success(msg string, args ...interface{}) { - Fsuccess(os.Stdout, msg, args...) -} - -// Fwrror prints an error message to w. -func Ferror(w io.Writer, msg string, args ...interface{}) { - fmt.Fprintf(w, Colorize.Color("[bold][red]error[reset] %s\n"), fmt.Sprintf(msg, args...)) -} - -// Error prints an error message. -func Error(msg string, args ...interface{}) { - Ferror(os.Stdout, msg, args...) -} - -// Fwarning prints a warning message to w. -func Fwarning(w io.Writer, msg string, args ...interface{}) { - fmt.Fprintf(os.Stderr, Colorize.Color("[bold][yellow]warning[reset] %s\n"), fmt.Sprintf(msg, args...)) -} - -// Warning prints a warning message. -func Warning(msg string, args ...interface{}) { - Fwarning(os.Stdout, msg, args...) -} - -// Ffatal prints an error message to w and exits. -func Ffatal(w io.Writer, msg string, args ...interface{}) { - fmt.Fprintf(w, Colorize.Color("[bold][red]fatal[reset] %s\n"), fmt.Sprintf(msg, args...)) - os.Exit(1) -} - -// Fatal prints an error message and exits. -func Fatal(msg string, args ...interface{}) { - Ffatal(os.Stderr, msg, args...) -} - -// FfatalErr prints an error object to w and exits. -func FfatalErr(w io.Writer, err error) { - Ffatal(w, "%v", err) -} - -// FatalErr prints an error object and exits. -func FatalErr(err error) { - FfatalErr(os.Stderr, err) -} - -// Small returns a `small` colored string. -func Small(msg string) string { - return Colorize.Color("[dim]" + msg) -} - -// Primary returns a `primary` colored string. -func Primary(msg string) string { - return Colorize.Color("[light_green]" + msg) -} - -// Highlight returns a `highlighted` colored string. -func Highlight(msg string) string { - return Colorize.Color("[cyan]" + msg) -} - -// Truncate truncates a string to the given length -func Truncate(msg string, length int) string { - for utf8.RuneCountInString(msg) > length { - msg = msg[0:len(msg)-4] + "…" - } - return msg -} - -// HashColor returns a consistent color for a given string -func HashColor(text string) string { - colors := []string{ - "green", - "light_green", - "light_blue", - "blue", - "magenta", - "light_magenta", - "light_yellow", - "cyan", - "light_cyan", - "red", - "light_red", - } - h := adler32.Checksum([]byte(text)) - return colors[int(h)%len(colors)] -} - -// PrintLegend prints a demo of the ui functions -func PrintLegend() { - Info("info message") - Success("success message") - Error("error message") - Warning("warning message") - Verbose("verbose message") - fmt.Printf("this is %s\n", Small("small")) - fmt.Printf("this is %s\n", Primary("primary")) - fmt.Printf("this is a %s\n", Highlight("highlight")) -}