Skip to content

Commit

Permalink
feat: add the new logger through Zarf wherever it is missing (#3265)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 authored Nov 21, 2024
1 parent 837f2ca commit d946546
Show file tree
Hide file tree
Showing 29 changed files with 111 additions and 62 deletions.
5 changes: 4 additions & 1 deletion src/cmd/common/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
package common

import (
"context"
"fmt"
"path/filepath"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/fatih/color"

"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
)

// PrintFindings prints the findings in the LintError as a table.
func PrintFindings(lintErr *lint.LintError) {
func PrintFindings(ctx context.Context, lintErr *lint.LintError) {
mapOfFindingsByPath := lint.GroupFindingsByPath(lintErr.Findings, lintErr.PackageName)
for _, findings := range mapOfFindingsByPath {
lintData := [][]string{}
Expand All @@ -41,6 +43,7 @@ func PrintFindings(lintErr *lint.LintError) {
packagePathFromUser = filepath.Join(lintErr.BaseDir, findings[0].PackagePathOverride)
}
message.Notef("Linting package %q at %s", findings[0].PackageNameOverride, packagePathFromUser)
logger.From(ctx).Info("linting package", "name", findings[0].PackageNameOverride, "path", packagePathFromUser)
message.Table([]string{"Type", "Path", "Message"}, lintData)
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var devDeployCmd = &cobra.Command{
Short: lang.CmdDevDeployShort,
Long: lang.CmdDevDeployLong,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)

v := common.GetViper()
Expand All @@ -62,10 +63,10 @@ var devDeployCmd = &cobra.Command{
}
defer pkgClient.ClearTempPaths()

err = pkgClient.DevDeploy(cmd.Context())
err = pkgClient.DevDeploy(ctx)
var lintErr *lint.LintError
if errors.As(err, &lintErr) {
common.PrintFindings(lintErr)
common.PrintFindings(ctx, lintErr)
}
if err != nil {
return fmt.Errorf("failed to dev deploy: %w", err)
Expand Down Expand Up @@ -242,6 +243,7 @@ var devFindImagesCmd = &cobra.Command{
Short: lang.CmdDevFindImagesShort,
Long: lang.CmdDevFindImagesLong,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)

v := common.GetViper()
Expand All @@ -256,16 +258,16 @@ var devFindImagesCmd = &cobra.Command{
}
defer pkgClient.ClearTempPaths()

_, err = pkgClient.FindImages(cmd.Context())
_, err = pkgClient.FindImages(ctx)

var lintErr *lint.LintError
if errors.As(err, &lintErr) {
// HACK(mkcp): Re-initializing PTerm with a stderr writer isn't great, but it lets us render these lint
// tables below for backwards compatibility
if logger.Enabled(cmd.Context()) {
if logger.Enabled(ctx) {
message.InitializePTerm(logger.DestinationDefault)
}
common.PrintFindings(lintErr)
common.PrintFindings(ctx, lintErr)
}
if err != nil {
return fmt.Errorf("unable to find images: %w", err)
Expand Down Expand Up @@ -302,21 +304,22 @@ var devLintCmd = &cobra.Command{
Short: lang.CmdDevLintShort,
Long: lang.CmdDevLintLong,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
config.CommonOptions.Confirm = true
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)
v := common.GetViper()
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

err := lint.Validate(cmd.Context(), pkgConfig.CreateOpts.BaseDir, pkgConfig.CreateOpts.Flavor, pkgConfig.CreateOpts.SetVariables)
err := lint.Validate(ctx, pkgConfig.CreateOpts.BaseDir, pkgConfig.CreateOpts.Flavor, pkgConfig.CreateOpts.SetVariables)
var lintErr *lint.LintError
if errors.As(err, &lintErr) {
// HACK(mkcp): Re-initializing PTerm with a stderr writer isn't great, but it lets us render these lint
// tables below for backwards compatibility
if logger.Enabled(cmd.Context()) {
if logger.Enabled(ctx) {
message.InitializePTerm(logger.DestinationDefault)
}
common.PrintFindings(lintErr)
common.PrintFindings(ctx, lintErr)
// Do not return an error if the findings are all warnings.
if lintErr.OnlyWarnings() {
return nil
Expand Down Expand Up @@ -352,11 +355,11 @@ func init() {

err := devFindImagesCmd.Flags().MarkDeprecated("set", "this field is replaced by create-set")
if err != nil {
message.Debug("Unable to mark dev-find-images flag as set", "error", err)
logger.Default().Debug("unable to mark dev-find-images flag as set", "error", err)
}
err = devFindImagesCmd.Flags().MarkHidden("set")
if err != nil {
message.Debug("Unable to mark dev-find-images flag as hidden", "error", err)
logger.Default().Debug("unable to mark dev-find-images flag as hidden", "error", err)
}
devFindImagesCmd.Flags().StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor)
devFindImagesCmd.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "create-set", v.GetStringMapString(common.VPkgCreateSet), lang.CmdDevFlagSet)
Expand Down Expand Up @@ -385,7 +388,7 @@ func bindDevDeployFlags(v *viper.Viper) {
devDeployFlags.StringVar(&pkgConfig.DeployOpts.RegistryURL, "registry-url", defaultRegistry, lang.CmdDevFlagRegistry)
err := devDeployFlags.MarkHidden("registry-url")
if err != nil {
message.Debug("Unable to mark dev-deploy flag as hidden", "error", err)
logger.Default().Debug("unable to mark dev-deploy flag as hidden", "error", err)
}

devDeployFlags.StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "deploy-set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)
Expand Down
10 changes: 4 additions & 6 deletions src/cmd/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/zarf-dev/zarf/src/internal/agent"
"github.com/zarf-dev/zarf/src/internal/gitea"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
)

Expand Down Expand Up @@ -154,11 +155,7 @@ tableOfContents: false
return "/commands/" + link[:len(link)-3] + "/"
}

if err := doc.GenMarkdownTreeCustom(rootCmd, "./site/src/content/docs/commands", prependTitle, linkHandler); err != nil {
return err
}
message.Success(lang.CmdInternalGenerateCliDocsSuccess)
return nil
return doc.GenMarkdownTreeCustom(rootCmd, "./site/src/content/docs/commands", prependTitle, linkHandler)
},
}

Expand Down Expand Up @@ -273,6 +270,7 @@ var updateGiteaPVC = &cobra.Command{
helmShouldCreate, err := c.UpdateGiteaPVC(ctx, pvcName, rollback)
if err != nil {
message.WarnErr(err, lang.CmdInternalUpdateGiteaPVCErr)
logger.From(ctx).Warn("Unable to update the existing Gitea persistent volume claim", "error", err.Error())
}
fmt.Print(helmShouldCreate)
return nil
Expand Down Expand Up @@ -324,7 +322,7 @@ func addHiddenDummyFlag(cmd *cobra.Command, flagDummy string) {
cmd.PersistentFlags().StringVar(&dummyStr, flagDummy, "", "")
err := cmd.PersistentFlags().MarkHidden(flagDummy)
if err != nil {
message.Debug("Unable to add hidden dummy flag", "error", err)
logger.From(cmd.Context()).Debug("Unable to add hidden dummy flag", "error", err)
}
}
}
49 changes: 27 additions & 22 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var packageCreateCmd = &cobra.Command{
Short: lang.CmdPackageCreateShort,
Long: lang.CmdPackageCreateLong,
RunE: func(cmd *cobra.Command, args []string) error {
l := logger.From(cmd.Context())
ctx := cmd.Context()
l := logger.From(ctx)
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)

var isCleanPathRegex = regexp.MustCompile(`^[a-zA-Z0-9\_\-\/\.\~\\:]+$`)
Expand All @@ -65,19 +66,19 @@ var packageCreateCmd = &cobra.Command{
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

pkgClient, err := packager.New(&pkgConfig,
packager.WithContext(cmd.Context()),
packager.WithContext(ctx),
)
if err != nil {
return err
}
defer pkgClient.ClearTempPaths()

err = pkgClient.Create(cmd.Context())
err = pkgClient.Create(ctx)

// NOTE(mkcp): LintErrors are rendered with a table
var lintErr *lint.LintError
if errors.As(err, &lintErr) {
common.PrintFindings(lintErr)
common.PrintFindings(ctx, lintErr)
}
if err != nil {
return fmt.Errorf("failed to create package: %w", err)
Expand All @@ -99,7 +100,8 @@ var packageDeployCmd = &cobra.Command{
}
},
RunE: func(cmd *cobra.Command, args []string) error {
packageSource, err := choosePackage(args)
ctx := cmd.Context()
packageSource, err := choosePackage(ctx, args)
if err != nil {
return err
}
Expand All @@ -115,8 +117,6 @@ var packageDeployCmd = &cobra.Command{
}
defer pkgClient.ClearTempPaths()

ctx := cmd.Context()

if err := pkgClient.Deploy(ctx); err != nil {
return fmt.Errorf("failed to deploy package: %w", err)
}
Expand All @@ -138,6 +138,7 @@ var packageMirrorCmd = &cobra.Command{
}
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
var c *cluster.Cluster
if dns.IsServiceURL(pkgConfig.InitOpts.RegistryInfo.Address) || dns.IsServiceURL(pkgConfig.InitOpts.GitServer.Address) {
var err error
Expand All @@ -146,7 +147,7 @@ var packageMirrorCmd = &cobra.Command{
return err
}
}
src, err := choosePackage(args)
src, err := choosePackage(ctx, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -180,7 +181,7 @@ var packageMirrorCmd = &cobra.Command{
NoImageChecksum: pkgConfig.MirrorOpts.NoImgChecksum,
Retries: pkgConfig.PkgOpts.Retries,
}
err = packager2.Mirror(cmd.Context(), mirrorOpt)
err = packager2.Mirror(ctx, mirrorOpt)
if err != nil {
return err
}
Expand All @@ -201,8 +202,9 @@ var packageInspectCmd = &cobra.Command{
}
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// NOTE(mkcp): Gets user input with message
src, err := choosePackage(args)
src, err := choosePackage(ctx, args)
if err != nil {
return err
}
Expand All @@ -219,7 +221,7 @@ var packageInspectCmd = &cobra.Command{
}

if pkgConfig.InspectOpts.ListImages {
output, err := packager2.InspectList(cmd.Context(), inspectOpt)
output, err := packager2.InspectList(ctx, inspectOpt)
if err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
}
Expand All @@ -231,7 +233,7 @@ var packageInspectCmd = &cobra.Command{
}
}

output, err := packager2.Inspect(cmd.Context(), inspectOpt)
output, err := packager2.Inspect(ctx, inspectOpt)
if err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
}
Expand Down Expand Up @@ -306,7 +308,8 @@ var packageRemoveCmd = &cobra.Command{
}
},
RunE: func(cmd *cobra.Command, args []string) error {
packageSource, err := choosePackage(args)
ctx := cmd.Context()
packageSource, err := choosePackage(ctx, args)
if err != nil {
return err
}
Expand All @@ -322,7 +325,7 @@ var packageRemoveCmd = &cobra.Command{
SkipSignatureValidation: pkgConfig.PkgOpts.SkipSignatureValidation,
PublicKeyPath: pkgConfig.PkgOpts.PublicKeyPath,
}
err = packager2.Remove(cmd.Context(), removeOpt)
err = packager2.Remove(ctx, removeOpt)
if err != nil {
return err
}
Expand Down Expand Up @@ -400,30 +403,31 @@ var packagePullCmd = &cobra.Command{
},
}

func choosePackage(args []string) (string, error) {
func choosePackage(ctx context.Context, args []string) (string, error) {
if len(args) > 0 {
return args[0], nil
}
l := logger.From(ctx)
var path string
prompt := &survey.Input{
Message: lang.CmdPackageChoose,
Suggest: func(toComplete string) []string {
tarPath := config.ZarfPackagePrefix + toComplete + "*.tar"
files, err := filepath.Glob(tarPath)
if err != nil {
message.Debug("Unable to glob", "tarPath", tarPath, "error", err)
l.Debug("unable to glob", "tarPath", tarPath, "error", err)
}

zstPath := config.ZarfPackagePrefix + toComplete + "*.tar.zst"
zstFiles, err := filepath.Glob(zstPath)
if err != nil {
message.Debug("Unable to glob", "zstPath", zstPath, "error", err)
l.Debug("unable to glob", "zstPath", zstPath, "error", err)
}

splitPath := config.ZarfPackagePrefix + toComplete + "*.part000"
splitFiles, err := filepath.Glob(splitPath)
if err != nil {
message.Debug("Unable to glob", "splitPath", splitPath, "error", err)
l.Debug("unable to glob", "splitPath", splitPath, "error", err)
}

files = append(files, zstFiles...)
Expand Down Expand Up @@ -452,6 +456,7 @@ func getPackageCompletionArgs(cmd *cobra.Command, _ []string, _ string) ([]strin
deployedZarfPackages, err := c.GetDeployedZarfPackages(ctx)
if err != nil {
message.Debug("Unable to get deployed zarf packages for package completion args", "error", err)
logger.From(cmd.Context()).Debug("unable to get deployed zarf packages for package completion args", "error", err)
}
// Populate list of package names
for _, pkg := range deployedZarfPackages {
Expand Down Expand Up @@ -523,15 +528,15 @@ func bindCreateFlags(v *viper.Viper) {

errOD := createFlags.MarkHidden("output-directory")
if errOD != nil {
message.Debug("Unable to mark flag output-directory", "error", errOD)
logger.Default().Debug("unable to mark flag output-directory", "error", errOD)
}
errKey := createFlags.MarkHidden("key")
if errKey != nil {
message.Debug("Unable to mark flag key", "error", errKey)
logger.Default().Debug("unable to mark flag key", "error", errKey)
}
errKP := createFlags.MarkHidden("key-pass")
if errKP != nil {
message.Debug("Unable to mark flag key-pass", "error", errKP)
logger.Default().Debug("unable to mark flag key-pass", "error", errKP)
}
}

Expand All @@ -554,7 +559,7 @@ func bindDeployFlags(v *viper.Viper) {

err := deployFlags.MarkHidden("sget")
if err != nil {
message.Debug("Unable to mark flag sget", "error", err)
logger.Default().Debug("unable to mark flag sget", "error", err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func Open(rootPath, address string) (*Repository, error) {

// Clone clones a git repository to the given local path.
func Clone(ctx context.Context, rootPath, address string, shallow bool) (*Repository, error) {
l := logger.From(ctx)
// Split the remote url and the zarf reference
gitURLNoRef, refPlain, err := transform.GitURLSplitRef(address)
if err != nil {
Expand Down Expand Up @@ -97,6 +98,7 @@ func Clone(ctx context.Context, rootPath, address string, shallow bool) (*Reposi
repo, err := git.PlainCloneContext(ctx, r.path, false, cloneOpts)
if err != nil {
message.Notef("Falling back to host 'git', failed to clone the repo %q with Zarf: %s", gitURLNoRef, err.Error())
l.Info("falling back to host 'git', failed to clone the repo with Zarf", "url", gitURLNoRef, "error", err)
err := r.gitCloneFallback(ctx, gitURLNoRef, ref, shallow)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit d946546

Please sign in to comment.