Skip to content

Commit 8740120

Browse files
Serge Bazanskiq3k
authored andcommitted
cmd/sops: use --config in subcommands
1 parent 4bc27f6 commit 8740120

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

cmd/sops/main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func main() {
227227
if c.Bool("verbose") || c.GlobalBool("verbose") {
228228
logging.SetLevel(logrus.DebugLevel)
229229
}
230-
configPath, err := config.FindConfigFile(".")
230+
configPath, err := getConfigPath(c, ".")
231231
if err != nil {
232232
return common.NewExitError(err, codes.ErrorGeneric)
233233
}
@@ -418,7 +418,7 @@ func main() {
418418
},
419419
}, keyserviceFlags...),
420420
Action: func(c *cli.Context) error {
421-
configPath, err := config.FindConfigFile(".")
421+
configPath, err := getConfigPath(c, ".")
422422
if err != nil {
423423
return common.NewExitError(err, codes.ErrorGeneric)
424424
}
@@ -958,13 +958,22 @@ func keyGroups(c *cli.Context, file string) ([]sops.KeyGroup, error) {
958958
return []sops.KeyGroup{group}, nil
959959
}
960960

961+
// getConfigPath returns the config path that should be used: either one explicitly set by the 'config' command line flag, or
962+
// by travering up the filesystem.
963+
func getConfigPath(c *cli.Context, root string) (string, error) {
964+
if c.GlobalString("config") != "" {
965+
return c.GlobalString("config"), nil
966+
}
967+
return config.FindConfigFile(root)
968+
}
969+
961970
// loadConfig will look for an existing config file, either provided through the command line, or using config.FindConfigFile.
962971
// Since a config file is not required, this function does not error when one is not found, and instead returns a nil config pointer
963972
func loadConfig(c *cli.Context, file string, kmsEncryptionContext map[string]*string) (*config.Config, error) {
964973
var err error
965974
var configPath string
966-
if c.String("config") != "" {
967-
configPath = c.String("config")
975+
if c.GlobalString("config") != "" {
976+
configPath = c.GlobalString("config")
968977
} else {
969978
// Ignore config not found errors returned from FindConfigFile since the config file is not mandatory
970979
configPath, err = config.FindConfigFile(".")

0 commit comments

Comments
 (0)