Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace message.Info, message.Note and message.Success with context logger #2939

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var devDeployCmd = &cobra.Command{
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)

pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand All @@ -78,7 +78,7 @@ var devGenerateCmd = &cobra.Command{
pkgConfig.CreateOpts.BaseDir = "."
pkgConfig.FindImagesOpts.RepoHelmChartPath = pkgConfig.GenerateOpts.GitPath

pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -229,7 +229,7 @@ var devFindImagesCmd = &cobra.Command{
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)
pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -276,7 +276,7 @@ var devLintCmd = &cobra.Command{
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/zarf-dev/zarf/src/cmd/common"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/logging"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
Expand Down Expand Up @@ -63,7 +64,7 @@ var initCmd = &cobra.Command{
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)

pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))
pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src))
if err != nil {
return err
}
Expand Down Expand Up @@ -127,7 +128,7 @@ func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, er
// Give the user the choice to download the init-package and note that this does require an internet connection
message.Question(fmt.Sprintf(lang.CmdInitPullAsk, url))

message.Note(lang.CmdInitPullNote)
logging.FromContextOrDiscard(ctx).Info("Downloading init package will require an internet connection")

// Prompt the user if --confirm not specified
if !confirmDownload {
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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/logging"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/types"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ var httpProxyCmd = &cobra.Command{
var genCLIDocs = &cobra.Command{
Use: "gen-cli-docs",
Short: lang.CmdInternalGenerateCliDocsShort,
RunE: func(_ *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
// Don't include the datestamp in the output
rootCmd.DisableAutoGenTag = true

Expand Down Expand Up @@ -161,7 +162,7 @@ tableOfContents: false
if err := doc.GenMarkdownTreeCustom(rootCmd, "./site/src/content/docs/commands", prependTitle, linkHandler); err != nil {
return err
}
message.Success(lang.CmdInternalGenerateCliDocsSuccess)
logging.FromContextOrDiscard(cmd.Context()).Info("Successfully createed the CLI documentation")
return nil
},
}
Expand Down
23 changes: 12 additions & 11 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/zarf-dev/zarf/src/cmd/common"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/logging"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
"github.com/zarf-dev/zarf/src/types"
Expand Down Expand Up @@ -54,7 +55,7 @@ var packageCreateCmd = &cobra.Command{
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -84,7 +85,7 @@ var packageDeployCmd = &cobra.Command{
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)

pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,7 +113,7 @@ var packageMirrorCmd = &cobra.Command{
return err
}
pkgConfig.PkgOpts.PackageSource = packageSource
pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand All @@ -136,11 +137,11 @@ var packageInspectCmd = &cobra.Command{
return err
}
pkgConfig.PkgOpts.PackageSource = packageSource
src, err := identifyAndFallbackToClusterSource()
src, err := identifyAndFallbackToClusterSource(cmd.Context())
if err != nil {
return err
}
pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))
pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src))
if err != nil {
return err
}
Expand Down Expand Up @@ -208,11 +209,11 @@ var packageRemoveCmd = &cobra.Command{
return err
}
pkgConfig.PkgOpts.PackageSource = packageSource
src, err := identifyAndFallbackToClusterSource()
src, err := identifyAndFallbackToClusterSource(cmd.Context())
if err != nil {
return err
}
pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))
pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src))
if err != nil {
return err
}
Expand Down Expand Up @@ -253,7 +254,7 @@ var packagePublishCmd = &cobra.Command{

pkgConfig.PublishOpts.PackageDestination = ref.String()

pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand All @@ -273,7 +274,7 @@ var packagePullCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pkgConfig.PkgOpts.PackageSource = args[0]
pkgClient, err := packager.New(&pkgConfig)
pkgClient, err := packager.New(cmd.Context(), &pkgConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -311,10 +312,10 @@ func choosePackage(args []string) (string, error) {
}

// TODO: This code does not seem to do what it was intended.
func identifyAndFallbackToClusterSource() (sources.PackageSource, error) {
func identifyAndFallbackToClusterSource(ctx context.Context) (sources.PackageSource, error) {
identifiedSrc := sources.Identify(pkgConfig.PkgOpts.PackageSource)
if identifiedSrc == "" {
message.Debugf(lang.CmdPackageClusterSourceFallback, pkgConfig.PkgOpts.PackageSource)
logging.FromContextOrDiscard(ctx).Debug("package source does not satisfy any current sources, assuming it is a package deployed to a cluster", "source", pkgConfig.PkgOpts.PackageSource)
src, err := sources.NewClusterSource(&pkgConfig.PkgOpts)
if err != nil {
return nil, fmt.Errorf("unable to identify source from %s: %w", pkgConfig.PkgOpts.PackageSource, err)
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"slices"
"strings"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/logging"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/types"
)
Expand Down Expand Up @@ -87,6 +89,10 @@ var rootCmd = &cobra.Command{

// Execute is the entrypoint for the CLI.
func Execute(ctx context.Context) {
handler := logging.NewPtermHandler()
log := slog.New(handler)
ctx = logging.NewContext(ctx, log)

cmd, err := rootCmd.ExecuteContextC(ctx)
if err == nil {
return
Expand Down
18 changes: 9 additions & 9 deletions src/cmd/tools/crane.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package tools

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/internal/packager/images"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/logging"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/types"
Expand Down Expand Up @@ -239,13 +241,15 @@ func pruneImages(cmd *cobra.Command, _ []string) error {
if tunnel != nil {
message.Notef(lang.CmdToolsRegistryTunnel, registryEndpoint, zarfState.RegistryInfo.Address)
defer tunnel.Close()
return tunnel.Wrap(func() error { return doPruneImagesForPackages(zarfState, zarfPackages, registryEndpoint) })
return tunnel.Wrap(func() error { return doPruneImagesForPackages(ctx, zarfState, zarfPackages, registryEndpoint) })
}

return doPruneImagesForPackages(zarfState, zarfPackages, registryEndpoint)
return doPruneImagesForPackages(ctx, zarfState, zarfPackages, registryEndpoint)
}

func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.DeployedPackage, registryEndpoint string) error {
func doPruneImagesForPackages(ctx context.Context, zarfState *types.ZarfState, zarfPackages []types.DeployedPackage, registryEndpoint string) error {
log := logging.FromContextOrDiscard(ctx)

authOption := images.WithPushAuth(zarfState.RegistryInfo)

spinner := message.NewProgressSpinner(lang.CmdToolsRegistryPruneLookup)
Expand Down Expand Up @@ -320,11 +324,7 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D
spinner.Success()

if len(imageDigestsToPrune) > 0 {
message.Note(lang.CmdToolsRegistryPruneImageList)

for digestRef := range imageDigestsToPrune {
message.Info(digestRef)
}
log.Info("Pruning images with digests from the registry", "digests", imageDigestsToPrune)

confirm := config.CommonOptions.Confirm

Expand Down Expand Up @@ -353,7 +353,7 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D
spinner.Success()
}
} else {
message.Note(lang.CmdToolsRegistryPruneNoImages)
log.Info("Tehere are no images to prune")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/tools/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var waitForCmd = &cobra.Command{
Long: lang.CmdToolsWaitForLong,
Example: lang.CmdToolsWaitForExample,
Args: cobra.MinimumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
// Parse the timeout string
timeout, err := time.ParseDuration(waitTimeout)
if err != nil {
Expand All @@ -51,7 +51,7 @@ var waitForCmd = &cobra.Command{
}

// Execute the wait command.
if err := utils.ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil {
if err := utils.ExecuteWait(cmd.Context(), waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil {
return err
}
return err
Expand Down
18 changes: 10 additions & 8 deletions src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/zarf-dev/zarf/src/internal/packager/helm"
"github.com/zarf-dev/zarf/src/internal/packager/template"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/logging"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
"github.com/zarf-dev/zarf/src/pkg/pki"
Expand Down Expand Up @@ -127,7 +128,7 @@ var updateCredsCmd = &cobra.Command{
confirm := config.CommonOptions.Confirm

if confirm {
message.Note(lang.CmdToolsUpdateCredsConfirmProvided)
logging.FromContextOrDiscard(ctx).Info("Confirm flag specified, continuing without prompting")
} else {
prompt := &survey.Confirm{
Message: lang.CmdToolsUpdateCredsConfirmContinue,
Expand Down Expand Up @@ -204,12 +205,13 @@ var clearCacheCmd = &cobra.Command{
Use: "clear-cache",
Aliases: []string{"c"},
Short: lang.CmdToolsClearCacheShort,
RunE: func(_ *cobra.Command, _ []string) error {
message.Notef(lang.CmdToolsClearCacheDir, config.GetAbsCachePath())
RunE: func(cmd *cobra.Command, _ []string) error {
log := logging.FromContextOrDiscard(cmd.Context())
log.Info("Using cache directory", "path", config.GetAbsCachePath())
if err := os.RemoveAll(config.GetAbsCachePath()); err != nil {
return fmt.Errorf("unable to clear the cache directory %s: %w", config.GetAbsCachePath(), err)
}
message.Successf(lang.CmdToolsClearCacheSuccess, config.GetAbsCachePath())
log.Info("Successfully cleared the cache", "path", config.GetAbsCachePath())
return nil
},
}
Expand Down Expand Up @@ -237,7 +239,7 @@ var generatePKICmd = &cobra.Command{
Aliases: []string{"pki"},
Short: lang.CmdToolsGenPkiShort,
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
pki, err := pki.GeneratePKI(args[0], subAltNames...)
if err != nil {
return err
Expand All @@ -251,7 +253,7 @@ var generatePKICmd = &cobra.Command{
if err := os.WriteFile("tls.key", pki.Key, helpers.ReadWriteUser); err != nil {
return err
}
message.Successf(lang.CmdToolsGenPkiSuccess, args[0])
logging.FromContextOrDiscard(cmd.Context()).Info("Successfully created a chain of trust", "host", args[0])
return nil
},
}
Expand All @@ -260,7 +262,7 @@ var generateKeyCmd = &cobra.Command{
Use: "gen-key",
Aliases: []string{"key"},
Short: lang.CmdToolsGenKeyShort,
RunE: func(_ *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
// Utility function to prompt the user for the password to the private key
passwordFunc := func(bool) ([]byte, error) {
// perform the first prompt
Expand Down Expand Up @@ -323,7 +325,7 @@ var generateKeyCmd = &cobra.Command{
return err
}

message.Successf(lang.CmdToolsGenKeySuccess, prvKeyFileName, pubKeyFileName)
logging.FromContextOrDiscard(cmd.Context()).Info("Generated key pair", "private key", prvKeyFileName, "public key", pubKeyFileName)
return nil
},
}
Expand Down
Loading
Loading