Skip to content

Commit

Permalink
feat: introduce logger to ctx, refactor cmd, and migrate logo to Zarf…
Browse files Browse the repository at this point in the history
… Say (#3120)

Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp authored Oct 22, 2024
1 parent 8f61a5b commit 72769a9
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 149 deletions.
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 @@ -46,7 +46,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 @@ -236,7 +236,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 @@ -291,7 +291,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

0 comments on commit 72769a9

Please sign in to comment.