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

feat: introduce logger to ctx, refactor cmd, and migrate logo to Zarf Say #3120

Merged
merged 10 commits into from
Oct 22, 2024
13 changes: 13 additions & 0 deletions src/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package cmd contains the CLI commands for Zarf.
package cmd

// setBaseDirectory sets the base directory. This is a directory with a zarf.yaml.
func setBaseDirectory(args []string) string {
if len(args) > 0 {
return args[0]
}
return "."
}
62 changes: 0 additions & 62 deletions src/cmd/common/setup.go

This file was deleted.

13 changes: 0 additions & 13 deletions src/cmd/common/utils.go

This file was deleted.

26 changes: 17 additions & 9 deletions src/cmd/common/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,37 @@
package common

import (
"context"
"errors"
"os"
"strings"

"github.com/zarf-dev/zarf/src/pkg/logger"

"github.com/spf13/viper"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/message"
)

// Constants for use when loading configurations from viper config files
const (

// Root config keys

VLogLevel = "log_level"
VArchitecture = "architecture"
VNoLogFile = "no_log_file"
VNoProgress = "no_progress"
VNoColor = "no_color"
VZarfCache = "zarf_cache"
VTmpDir = "tmp_dir"
VInsecure = "insecure"
VPlainHTTP = "plain_http"
VInsecureSkipTLSVerify = "insecure_skip_tls_verify"

// Root config, Logging

VLogLevel = "log_level"
VLogFormat = "log_format"
VNoLogFile = "no_log_file"
VNoProgress = "no_progress"
VNoColor = "no_color"

// Init config keys

VInitComponents = "init.components"
Expand Down Expand Up @@ -162,7 +167,10 @@ func isVersionCmd() bool {
return len(args) > 1 && (args[1] == "version" || args[1] == "v")
}

func printViperConfigUsed() {
// PrintViperConfigUsed informs users when Zarf has detected a config file.
func PrintViperConfigUsed(ctx context.Context) {
l := logger.From(ctx)

// Only print config info if viper is initialized.
vInitialized := v != nil
if !vInitialized {
Expand All @@ -173,11 +181,11 @@ func printViperConfigUsed() {
return
}
if vConfigError != nil {
message.WarnErrf(vConfigError, lang.CmdViperErrLoadingConfigFile, vConfigError.Error())
l.Error("unable to load config file", "error", vConfigError)
return
}
if cfgFile := v.ConfigFileUsed(); cfgFile != "" {
message.Notef(lang.CmdViperInfoUsingConfigFile, cfgFile)
l.Info("Using config file", "location", cfgFile)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var devDeployCmd = &cobra.Command{
Short: lang.CmdDevDeployShort,
Long: lang.CmdDevDeployLong,
RunE: func(cmd *cobra.Command, args []string) error {
pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args)
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)

v := common.GetViper()
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
Expand Down Expand Up @@ -234,7 +234,7 @@ var devFindImagesCmd = &cobra.Command{
Short: lang.CmdDevFindImagesShort,
Long: lang.CmdDevFindImagesLong,
RunE: func(cmd *cobra.Command, args []string) error {
pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args)
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)

v := common.GetViper()

Expand Down Expand Up @@ -289,7 +289,7 @@ var devLintCmd = &cobra.Command{
Long: lang.CmdDevLintLong,
RunE: func(cmd *cobra.Command, args []string) error {
config.CommonOptions.Confirm = true
pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args)
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)
v := common.GetViper()
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)
Expand Down
4 changes: 0 additions & 4 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -37,9 +36,6 @@ var initCmd = &cobra.Command{
Long: lang.CmdInitLong,
Example: lang.CmdInitExample,
RunE: func(cmd *cobra.Command, _ []string) error {
zarfLogo := message.GetLogo()
_, _ = fmt.Fprintln(os.Stderr, zarfLogo)

if err := validateInitFlags(); err != nil {
return fmt.Errorf("invalid command flags were provided: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var packageCreateCmd = &cobra.Command{
Short: lang.CmdPackageCreateShort,
Long: lang.CmdPackageCreateLong,
RunE: func(cmd *cobra.Command, args []string) error {
pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args)
pkgConfig.CreateOpts.BaseDir = setBaseDirectory(args)

var isCleanPathRegex = regexp.MustCompile(`^[a-zA-Z0-9\_\-\/\.\~\\:]+$`)
if !isCleanPathRegex.MatchString(config.CommonOptions.CachePath) {
Expand Down
Loading