From 779dda1aca8cf2c233bc2d075889a59924817d34 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 22 Oct 2021 11:02:34 -0700 Subject: [PATCH] mod: added basic logging + context in sub libs Signed-off-by: Sam Alba --- cmd/dagger/cmd/mod/get.go | 6 +++--- mod/mod.go | 24 ++++++++++++++++-------- state/project.go | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cmd/dagger/cmd/mod/get.go b/cmd/dagger/cmd/mod/get.go index c4cca65b..20df6180 100644 --- a/cmd/dagger/cmd/mod/get.go +++ b/cmd/dagger/cmd/mod/get.go @@ -38,13 +38,13 @@ var getCmd = &cobra.Command{ var err error if update && len(args) == 0 { lg.Info().Msg("updating all installed packages...") - processedRequires, err = mod.UpdateInstalled(project.Path) + processedRequires, err = mod.UpdateInstalled(ctx, project.Path) } else if update && len(args) > 0 { lg.Info().Msg("updating specified packages...") - processedRequires, err = mod.UpdateAll(project.Path, args) + processedRequires, err = mod.UpdateAll(ctx, project.Path, args) } else if !update && len(args) > 0 { lg.Info().Msg("installing specified packages...") - processedRequires, err = mod.InstallAll(project.Path, args) + processedRequires, err = mod.InstallAll(ctx, project.Path, args) } else { lg.Fatal().Msg("unrecognized update/install operation") } diff --git a/mod/mod.go b/mod/mod.go index 85aec33e..57892b7e 100644 --- a/mod/mod.go +++ b/mod/mod.go @@ -1,12 +1,17 @@ package mod import ( + "context" "path" "github.com/gofrs/flock" + "github.com/rs/zerolog/log" ) -func Install(workspace, repoName, versionConstraint string) (*Require, error) { +func Install(ctx context.Context, workspace, repoName, versionConstraint string) (*Require, error) { + lg := log.Ctx(ctx) + + lg.Info().Str("name", repoName).Msg("installing module") require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err @@ -38,14 +43,14 @@ func Install(workspace, repoName, versionConstraint string) (*Require, error) { return require, nil } -func InstallAll(workspace string, repoNames []string) ([]*Require, error) { +func InstallAll(ctx context.Context, workspace string, repoNames []string) ([]*Require, error) { installedRequires := make([]*Require, 0, len(repoNames)) var err error for _, repoName := range repoNames { var require *Require - if require, err = Install(workspace, repoName, ""); err != nil { + if require, err = Install(ctx, workspace, repoName, ""); err != nil { continue } @@ -55,7 +60,10 @@ func InstallAll(workspace string, repoNames []string) ([]*Require, error) { return installedRequires, err } -func Update(workspace, repoName, versionConstraint string) (*Require, error) { +func Update(ctx context.Context, workspace, repoName, versionConstraint string) (*Require, error) { + lg := log.Ctx(ctx) + + lg.Info().Str("name", repoName).Msg("updating module") require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err @@ -87,14 +95,14 @@ func Update(workspace, repoName, versionConstraint string) (*Require, error) { return updatedRequire, nil } -func UpdateAll(workspace string, repoNames []string) ([]*Require, error) { +func UpdateAll(ctx context.Context, workspace string, repoNames []string) ([]*Require, error) { updatedRequires := make([]*Require, 0, len(repoNames)) var err error for _, repoName := range repoNames { var require *Require - if require, err = Update(workspace, repoName, ""); err != nil { + if require, err = Update(ctx, workspace, repoName, ""); err != nil { continue } @@ -104,7 +112,7 @@ func UpdateAll(workspace string, repoNames []string) ([]*Require, error) { return updatedRequires, err } -func UpdateInstalled(workspace string) ([]*Require, error) { +func UpdateInstalled(ctx context.Context, workspace string) ([]*Require, error) { modfile, err := readPath(workspace) if err != nil { return nil, err @@ -116,7 +124,7 @@ func UpdateInstalled(workspace string) ([]*Require, error) { repoNames = append(repoNames, require.String()) } - return UpdateAll(workspace, repoNames) + return UpdateAll(ctx, workspace, repoNames) } func Ensure(workspace string) error { diff --git a/state/project.go b/state/project.go index 5f61d8be..adc72da3 100644 --- a/state/project.go +++ b/state/project.go @@ -398,7 +398,7 @@ func vendorUniverse(ctx context.Context, p string) error { } log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe") - if _, err := mod.Install(p, "alpha.dagger.io", universeVersionConstraint); err != nil { + if _, err := mod.Install(ctx, p, "alpha.dagger.io", universeVersionConstraint); err != nil { return err }