Skip to content

Commit 3e66429

Browse files
committed
breaking!: envfile flags changed and escaped flags support
1 parent 9177583 commit 3e66429

File tree

8 files changed

+43
-10
lines changed

8 files changed

+43
-10
lines changed

cmd/list.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var listCmd = &cobra.Command{
1414
Use: "list",
1515
Short: "List all key-value pairs in .env file",
1616
Example: heredoc.Doc(`
17-
$ ennbu list
18-
$ ennbu list -e .env.development
19-
$ ennbu list --json
17+
$ ennbu list
18+
$ ennbu list -e .env.development
19+
$ ennbu list --json
2020
`),
2121
RunE: list.Run,
2222
}

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ func Execute() {
2424
}
2525

2626
func init() {
27-
rootCmd.PersistentFlags().StringP(flags.FlagEnvFile, "e", ".env", "Path to the .env file")
27+
rootCmd.PersistentFlags().StringP(flags.FlagEnvFile, "f", ".env", "Path to the .env file")
2828
rootCmd.PersistentFlags().StringP(flags.FlagKey, "k", "", "Key to the .env file")
2929
}

cmd/set.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ ennbu set -e .env.development -k KEY VALUE
2222
}
2323

2424
func init() {
25-
25+
setCmd.Flags().BoolP(set.FlagEscape, "e", false, "value is escaped")
2626
_ = setCmd.MarkFlagRequired(flags.FlagKey)
2727
rootCmd.AddCommand(setCmd)
2828
}

flags/global.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package flags
22

33
const (
4-
FlagEnvFile string = "envFile"
4+
FlagEnvFile string = "file"
55
FlagKey string = "key"
66
)

replace/flags.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package replace
2+
3+
const (
4+
FlagEscape = "escape"
5+
)

replace/run.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ import (
77
"strings"
88

99
"github.com/spf13/cobra"
10+
"github.com/u-yas/ennbu/env"
11+
"github.com/u-yas/ennbu/flags"
1012
)
1113

1214
func Run(cmd *cobra.Command, args []string) error {
13-
key, _ := cmd.Flags().GetString("key")
14-
envFilePath, _ := cmd.Flags().GetString("envFile")
15+
key, _ := cmd.Flags().GetString(flags.FlagKey)
16+
envFilePath, _ := cmd.Flags().GetString(flags.FlagEnvFile)
17+
escapeFlag, _ := cmd.Flags().GetBool(FlagEscape)
18+
1519
value := args[0]
20+
if escapeFlag {
21+
value = env.EscapeSpecialChars(value)
22+
}
23+
24+
// Check if the value contains newline characters and wrap it with double quotes if necessary
25+
if strings.Contains(value, "\n") {
26+
value = fmt.Sprintf("\"%s\"", value)
27+
}
1628

1729
fileName := filepath.Base(envFilePath)
1830
envContent, err := os.ReadFile(envFilePath)

set/flags.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package set
2+
3+
const (
4+
FlagEscape = "escape"
5+
)

set/run.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ import (
66
"strings"
77

88
"github.com/spf13/cobra"
9+
"github.com/u-yas/ennbu/env"
10+
"github.com/u-yas/ennbu/flags"
911
)
1012

1113
func Run(cmd *cobra.Command, args []string) error {
12-
key, _ := cmd.Flags().GetString("key")
13-
envFilePath, _ := cmd.Flags().GetString("envFile")
14+
key, _ := cmd.Flags().GetString(flags.FlagKey)
15+
envFilePath, _ := cmd.Flags().GetString(flags.FlagEnvFile)
16+
escapeFlag, _ := cmd.Flags().GetBool(FlagEscape)
17+
1418
value := args[0]
19+
if escapeFlag {
20+
value = env.EscapeSpecialChars(value)
21+
}
22+
// Check if the value contains newline characters and wrap it with double quotes if necessary
23+
if strings.Contains(value, "\n") {
24+
value = fmt.Sprintf("\"%s\"", value)
25+
}
1526

1627
envContent, err := os.ReadFile(envFilePath)
1728
if err != nil {

0 commit comments

Comments
 (0)