mod: added basic logging + context in sub libs

Signed-off-by: Sam Alba <samalba@users.noreply.github.com>
This commit is contained in:
Sam Alba 2021-10-22 11:02:34 -07:00
parent d34bd86d99
commit 779dda1aca
3 changed files with 20 additions and 12 deletions

View File

@ -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")
}

View File

@ -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 {

View File

@ -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
}