Skip to content

Commit

Permalink
feat: add --config flag as an alternative to ZARF_CONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
waveywaves committed Jul 2, 2024
1 parent 43e50bb commit 8fcd950
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/cmd/common/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func SetupCLI() {
message.DisableColor()
}

SetViperConfigFilePath(config.CommonOptions.ConfigPath)

printViperConfigUsed()

// No log level set, so use the default
Expand Down
29 changes: 29 additions & 0 deletions src/cmd/common/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ func InitViper() *viper.Viper {

// Specify an alternate config file
cfgFile := os.Getenv("ZARF_CONFIG")
if !argsConfigPathIsPresent() {
if cfgFile != "" {
// Use config file from the flag.
v.SetConfigFile(cfgFile)
} else {
// Search config paths in the current directory and $HOME/.zarf.
v.AddConfigPath(".")
v.AddConfigPath("$HOME/.zarf")
v.SetConfigName("zarf-config")
}
}

// Don't forget to read config either from cfgFile or from home directory!
if cfgFile != "" {
Expand All @@ -150,6 +161,24 @@ func InitViper() *viper.Viper {
return v
}

func argsConfigPathIsPresent() bool {
args := os.Args
//check if the config path is set in the args
for _, arg := range args {
if arg == "--config" {
return true
}
}
return false
}

func SetViperConfigFilePath(vcfgFilePath string) {
if vcfgFilePath != "" {
v.SetConfigFile(config.CommonOptions.ConfigPath)
vConfigError = v.ReadInConfig()
}
}

// GetViper returns the viper singleton
func GetViper() *viper.Viper {
return v
Expand Down
1 change: 1 addition & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ func init() {
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.CachePath, "zarf-cache", v.GetString(common.VZarfCache), lang.RootCmdFlagCachePath)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.TempDirectory, "tmpdir", v.GetString(common.VTmpDir), lang.RootCmdFlagTempDir)
rootCmd.PersistentFlags().BoolVar(&config.CommonOptions.Insecure, "insecure", v.GetBool(common.VInsecure), lang.RootCmdFlagInsecure)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.ConfigPath, "config-path", "", lang.RootCmdFlagConfigPath)
}
4 changes: 3 additions & 1 deletion src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
RootCmdFlagCachePath = "Specify the location of the Zarf cache directory"
RootCmdFlagTempDir = "Specify the temporary directory to use for intermediate files"
RootCmdFlagInsecure = "Allow access to insecure registries and disable other recommended security enforcements such as package checksum and signature validation. This flag should only be used if you have a specific reason and accept the reduced security posture."
RootCmdFlagConfigPath = "Specify the path of the configuration file to use"

RootCmdDeprecatedDeploy = "Deprecated: Please use \"zarf package deploy %s\" to deploy this package. This warning will be removed in Zarf v1.0.0."
RootCmdDeprecatedCreate = "Deprecated: Please use \"zarf package create\" to create this package. This warning will be removed in Zarf v1.0.0."
Expand Down Expand Up @@ -570,7 +571,7 @@ $ zarf tools update-creds \
--artifact-push-username={USERNAME} --artifact-push-token={PASSWORD}
# NOTE: Any credentials omitted from flags without a service key specified will be autogenerated - URLs will only change if specified.
# Config options can also be set with the 'init' section of a Zarf config file.
# ConfigPath options can also be set with the 'init' section of a Zarf config file.
# Update specific Zarf credentials w/external services:
$ zarf tools update-creds registry --registry-push-username={USERNAME} --registry-push-password={PASSWORD}
Expand Down Expand Up @@ -598,6 +599,7 @@ $ zarf tools update-creds artifact --artifact-push-username={USERNAME} --artifac
// cmd viper setup
CmdViperErrLoadingConfigFile = "failed to load config file: %s"
CmdViperInfoUsingConfigFile = "Using config file %s"
CmdViperNoConfigFileProvided = "No config file found"
)

// Zarf Agent messages
Expand Down
33 changes: 24 additions & 9 deletions src/test/e2e/29_config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ func TestConfigFile(t *testing.T) {
e2e.SetupWithCluster(t)

var (
path = fmt.Sprintf("zarf-package-config-file-%s.tar.zst", e2e.Arch)
dir = "examples/config-file"
config = "zarf-config.toml"
path = fmt.Sprintf("zarf-package-config-file-%s.tar.zst", e2e.Arch)
dir = "examples/config-file"
config = "zarf-config.toml"
configPath = filepath.Join(dir, config)
)
// defer unsetting the ZARF_CONFIG so that we can test w/ the env var and w/ the flag both
defer os.Unsetenv("ZARF_CONFIG")

e2e.CleanFiles(path)

// Test the config file flag
os.Unsetenv("ZARF_CONFIG")
configFileTests(t, dir, path, configPath)

// Test the config file environment variable
t.Setenv("ZARF_CONFIG", filepath.Join(dir, config))
defer os.Unsetenv("ZARF_CONFIG")
configFileTests(t, dir, path)
t.Setenv("ZARF_CONFIG", configPath)
configFileTests(t, dir, path, "")

configFileDefaultTests(t)

Expand All @@ -38,15 +44,17 @@ func TestConfigFile(t *testing.T) {
e2e.CleanFiles(path)
}

func configFileTests(t *testing.T, dir, path string) {
func configFileTests(t *testing.T, dir, path string, configPath string) {
t.Helper()

_, stdErr, err := e2e.Zarf("package", "create", dir, "--confirm")
args := addConfigIfPresent([]string{"package", "create", dir, "--confirm"}, configPath)
_, stdErr, err := e2e.Zarf(args...)
require.NoError(t, err)
require.Contains(t, string(stdErr), "This is a zebra and they have stripes")
require.Contains(t, string(stdErr), "This is a leopard and they have spots")

_, stdErr, err = e2e.Zarf("package", "deploy", path, "--confirm")
args = addConfigIfPresent([]string{"package", "deploy", path, "--confirm"}, configPath)
_, stdErr, err = e2e.Zarf(args...)
require.NoError(t, err)
require.Contains(t, string(stdErr), "📦 LION COMPONENT")
require.NotContains(t, string(stdErr), "📦 LEOPARD COMPONENT")
Expand Down Expand Up @@ -166,3 +174,10 @@ func configFileDefaultTests(t *testing.T) {
require.Contains(t, string(stdOut), test)
}
}

func addConfigIfPresent(args []string, value string) []string {
if value != "" {
return append(args, fmt.Sprintf("--config-path=%s", value))
}
return args
}
1 change: 1 addition & 0 deletions src/types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ZarfCommonOptions struct {
CachePath string `json:"cachePath" jsonschema:"description=Path to use to cache images and git repos on package create"`
TempDirectory string `json:"tempDirectory" jsonschema:"description=Location Zarf should use as a staging ground when managing files and images for package creation and deployment"`
OCIConcurrency int `jsonschema:"description=Number of concurrent layer operations to perform when interacting with a remote package"`
ConfigPath string `json:"configPath" jsonschema:"description=Location of the Zarf configuration file"`
}

// ZarfPackageOptions tracks the user-defined preferences during common package operations.
Expand Down

0 comments on commit 8fcd950

Please sign in to comment.