From 5a51db3008992948b16bf9b6f2a908c370a97673 Mon Sep 17 00:00:00 2001 From: David CLEMENT Date: Tue, 23 Sep 2025 18:30:54 +0200 Subject: [PATCH] feat: preserve key on write --- README.md | 9 +++++---- cmd/write.go | 15 +++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 09b8fc8b..277e49c0 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ will use your account's default SSM alias: ### Writing Secrets ```bash -$ chamber write +$ chamber write [--preserve-case] ``` This operation will write a secret into the secret store. If a secret with that @@ -142,9 +142,10 @@ key already exists, it will increment the version and store a new value. If `-` is provided as the value argument, the value will be read from standard input. -Secret keys are normalized automatically. The `-` will be `_` and the letters will -be converted to upper case (for example a secret with key `secret_key` and -`secret-key` will become `SECRET_KEY`). +Secret keys are normalized automatically unless `--preserve-case` is specified. +The letters will be converted to lower case (for example a secret with key +`SECRET_KEY` will become `secret_key`, and `Secret-Key` will become +`secret-key`). #### Reserved Service Names diff --git a/cmd/write.go b/cmd/write.go index 898538db..878f33b8 100644 --- a/cmd/write.go +++ b/cmd/write.go @@ -14,9 +14,10 @@ import ( ) var ( - singleline bool - skipUnchanged bool - tags map[string]string + singleline bool + skipUnchanged bool + tags map[string]string + preserveWriteCase bool // writeCmd represents the write command writeCmd = &cobra.Command{ @@ -31,6 +32,7 @@ func init() { writeCmd.Flags().BoolVarP(&singleline, "singleline", "s", false, "Insert single line parameter (end with \\n)") writeCmd.Flags().BoolVarP(&skipUnchanged, "skip-unchanged", "", false, "Skip writing secret if value is unchanged") writeCmd.Flags().StringToStringVarP(&tags, "tags", "t", map[string]string{}, "Add tags to the secret; new secrets only") + writeCmd.Flags().BoolVarP(&preserveWriteCase, "preserve-case", "p", false, "preserve variable name case") RootCmd.AddCommand(writeCmd) } @@ -40,7 +42,12 @@ func write(cmd *cobra.Command, args []string) error { return fmt.Errorf("Failed to validate service: %w", err) } - key := utils.NormalizeKey(args[1]) + key := args[1] + + if !preserveWriteCase { + key = utils.NormalizeKey(key) + } + if err := validateKey(key); err != nil { return fmt.Errorf("Failed to validate key: %w", err) }