Skip to content

Commit 51c7269

Browse files
committed
Adding force flag.
1 parent 84ff290 commit 51c7269

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ CfgCrypt or config crypt is a cli tool to encrypt values in a text configuration
3333
Post string denoting end of value to be encrypted (default "}}#")
3434
-prefix string
3535
Prefix string denoting start of value to be encrypted (default "#{{")
36+
-force bool
37+
Overwrites key file if it exists
3638
```
3739

3840
## Example

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func main() {
1919
encodedKey := cli.String("key", "", "Base64 encoded encryption key, if not specified one will be generated")
2020
prefix := cli.String("prefix", "#{{", "Prefix string denoting start of value to be encrypted")
2121
postfix := cli.String("postfix", "}}#", "Post string denoting end of value to be encrypted")
22+
force := cli.Bool("force", false, "Overwrite key file if found")
2223
if len(os.Args) < 2 {
2324
os.Stderr.WriteString("Not enough arguments\n")
2425
explainUsage(cli)
@@ -33,7 +34,7 @@ func main() {
3334
explainUsage(cli)
3435
os.Exit(1)
3536
}
36-
err := textValueEncryptor.EncryptTextFile(textfile, *prefix, *postfix, *encodedKey)
37+
err := textValueEncryptor.EncryptTextFile(textfile, *prefix, *postfix, *encodedKey, *force)
3738
if err != nil {
3839
msg := fmt.Sprintf("Error encrypting file \"%s\":\n%s\n", textfile, err.Error())
3940
os.Stderr.WriteString(msg)

textValueEncryptor/textValueEncryptor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const DEFAULT_KEY_SIZE = 16
1919

2020
const MAX_FILE_SIZE = 1E7
2121

22-
func generateKey(textFilePath string) (key []byte, err error) {
22+
func generateKey(textFilePath string, force bool) (key []byte, err error) {
2323
keyPath := textFilePath + KEY_FILE_EXTENSION
24-
if _, statErr := os.Stat(keyPath); !os.IsNotExist(statErr) {
24+
if _, statErr := os.Stat(keyPath); !os.IsNotExist(statErr) && !force {
2525
msg := fmt.Sprintf("Key file \"%s\" already exists", keyPath)
2626
err = errors.New(msg)
2727
return
@@ -38,10 +38,10 @@ func isPowerOf2(n int) bool {
3838
return (n&(n-1)) == 0 && n > 0
3939
}
4040

41-
func EncryptTextFile(textFilePath, prefix, postfix, encodedKey string) (err error) {
41+
func EncryptTextFile(textFilePath, prefix, postfix, encodedKey string, force bool) (err error) {
4242
var key []byte
4343
if encodedKey == "" {
44-
key, err = generateKey(textFilePath)
44+
key, err = generateKey(textFilePath, force)
4545
} else {
4646
key, err = base64.StdEncoding.DecodeString(encodedKey)
4747
}

0 commit comments

Comments
 (0)