Skip to content

Commit

Permalink
run gofmt in cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
rickar committed Mar 19, 2022
1 parent a3ca1f2 commit f72b88c
Show file tree
Hide file tree
Showing 7 changed files with 498 additions and 498 deletions.
80 changes: 40 additions & 40 deletions cmd/decrypt.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/rickar/props"
)

var (
decryptFlags = flag.NewFlagSet("decrypt", flag.ExitOnError)
decryptValue = decryptFlags.String("value", "", "`encrypted` value to decrypt (including algorithm prefix)")
decryptPass = decryptFlags.String("password", "", "`password` to decrypt the value")
)

func init() {
decryptFlags.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "decrypt: decrypt an encrypted property value\n")
decryptFlags.PrintDefaults()
}
}

func decrypt() {
if *decryptValue == "" {
fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n")
decryptFlags.Usage()
os.Exit(100)
}
if *decryptPass == "" {
*decryptPass = readPassword("Password:", decryptFlags, 100)
}

dec, err := props.Decrypt(*decryptPass, *decryptValue)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "decrypt error: %v\n", err)
os.Exit(101)
}
fmt.Println(dec)
}
package main

import (
"flag"
"fmt"
"os"

"github.com/rickar/props"
)

var (
decryptFlags = flag.NewFlagSet("decrypt", flag.ExitOnError)
decryptValue = decryptFlags.String("value", "", "`encrypted` value to decrypt (including algorithm prefix)")
decryptPass = decryptFlags.String("password", "", "`password` to decrypt the value")
)

func init() {
decryptFlags.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "decrypt: decrypt an encrypted property value\n")
decryptFlags.PrintDefaults()
}
}

func decrypt() {
if *decryptValue == "" {
fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n")
decryptFlags.Usage()
os.Exit(100)
}
if *decryptPass == "" {
*decryptPass = readPassword("Password:", decryptFlags, 100)
}

dec, err := props.Decrypt(*decryptPass, *decryptValue)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "decrypt error: %v\n", err)
os.Exit(101)
}
fmt.Println(dec)
}
172 changes: 86 additions & 86 deletions cmd/decryptFile.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"strings"

"github.com/rickar/props"
)

var (
decryptFileFlags = flag.NewFlagSet("decryptFile", flag.ExitOnError)
decryptFilePath = decryptFileFlags.String("path", "", "properties `file` to decrypt")
decryptFilePass = decryptFileFlags.String("password", "", "`password` to decrypt the values")
decryptFileOutput = decryptFileFlags.String("output", "", "output `file` to write results (default is input file)")
)

func init() {
decryptFileFlags.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "decryptFile: decrypt encrypted values in a property file\n")
decryptFileFlags.PrintDefaults()
}
}

func decryptFile() {
if *decryptFilePath == "" {
fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n")
decryptFileFlags.Usage()
os.Exit(200)
} else {
stat, err := os.Stat(*decryptFilePath)
if err != nil || stat.IsDir() {
fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n")
decryptFileFlags.Usage()
os.Exit(201)
}
}
if *decryptFilePass == "" {
*decryptFilePass = readPassword("Password:", decryptFileFlags, 202)
}
if *decryptFileOutput == "" {
*decryptFileOutput = *decryptFilePath
}

f, err := os.Open(*decryptFilePath)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err)
os.Exit(203)
}
defer f.Close()

found := 0
var result bytes.Buffer
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, props.EncryptAESGCM) {
found++
i := strings.Index(line, props.EncryptAESGCM)
val := line[i:]
line := line[:i]
enc, err := props.Decrypt(*decryptFilePass, val)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "unable to decrypt property: %v\n", err)
os.Exit(204)
}
result.WriteString(line)
result.WriteString(props.EncryptNone)
result.WriteString(enc)
result.WriteRune('\n')
} else {
result.WriteString(line)
result.WriteRune('\n')
}
}
f.Close()
err = os.WriteFile(*decryptFileOutput, result.Bytes(), 0o644)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err)
os.Exit(205)
}
fmt.Printf("%d properties decrypted\n", found)
}
package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"strings"

"github.com/rickar/props"
)

var (
decryptFileFlags = flag.NewFlagSet("decryptFile", flag.ExitOnError)
decryptFilePath = decryptFileFlags.String("path", "", "properties `file` to decrypt")
decryptFilePass = decryptFileFlags.String("password", "", "`password` to decrypt the values")
decryptFileOutput = decryptFileFlags.String("output", "", "output `file` to write results (default is input file)")
)

func init() {
decryptFileFlags.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "decryptFile: decrypt encrypted values in a property file\n")
decryptFileFlags.PrintDefaults()
}
}

func decryptFile() {
if *decryptFilePath == "" {
fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n")
decryptFileFlags.Usage()
os.Exit(200)
} else {
stat, err := os.Stat(*decryptFilePath)
if err != nil || stat.IsDir() {
fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n")
decryptFileFlags.Usage()
os.Exit(201)
}
}
if *decryptFilePass == "" {
*decryptFilePass = readPassword("Password:", decryptFileFlags, 202)
}
if *decryptFileOutput == "" {
*decryptFileOutput = *decryptFilePath
}

f, err := os.Open(*decryptFilePath)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err)
os.Exit(203)
}
defer f.Close()

found := 0
var result bytes.Buffer
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, props.EncryptAESGCM) {
found++
i := strings.Index(line, props.EncryptAESGCM)
val := line[i:]
line := line[:i]
enc, err := props.Decrypt(*decryptFilePass, val)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "unable to decrypt property: %v\n", err)
os.Exit(204)
}
result.WriteString(line)
result.WriteString(props.EncryptNone)
result.WriteString(enc)
result.WriteRune('\n')
} else {
result.WriteString(line)
result.WriteRune('\n')
}
}
f.Close()
err = os.WriteFile(*decryptFileOutput, result.Bytes(), 0o644)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err)
os.Exit(205)
}
fmt.Printf("%d properties decrypted\n", found)
}
108 changes: 54 additions & 54 deletions cmd/encrypt.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/rickar/props"
)

var (
encryptFlags = flag.NewFlagSet("encrypt", flag.ExitOnError)
encryptValue = encryptFlags.String("value", "", "`plaintext` value to encrypt")
encryptPass = encryptFlags.String("password", "", "`password` to encrypt the value")
encryptAlg = encryptFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)")
)

func init() {
encryptFlags.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "encrypt: encrypt a value for use in a property file\n")
encryptFlags.PrintDefaults()
}
}

func encrypt() {
if *encryptValue == "" {
fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n")
encryptFlags.Usage()
os.Exit(300)
}
if *encryptPass == "" {
*encryptPass = readPassword("Password:", encryptFlags, 301)
}
if *encryptAlg != props.EncryptAESGCM {
fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n")
encryptFlags.Usage()
os.Exit(302)
}

switch len(*encryptPass) {
case 16, 24, 32:
default:
fmt.Fprintf(flag.CommandLine.Output(), "the password parameter must be 16, 24, or 32 bytes\n")
encryptFlags.Usage()
os.Exit(303)
}

enc, err := props.Encrypt(*encryptAlg, *encryptPass, *encryptValue)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "encrypt error: %v\n", err)
os.Exit(304)
}
fmt.Println(enc)
}
package main

import (
"flag"
"fmt"
"os"

"github.com/rickar/props"
)

var (
encryptFlags = flag.NewFlagSet("encrypt", flag.ExitOnError)
encryptValue = encryptFlags.String("value", "", "`plaintext` value to encrypt")
encryptPass = encryptFlags.String("password", "", "`password` to encrypt the value")
encryptAlg = encryptFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)")
)

func init() {
encryptFlags.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "encrypt: encrypt a value for use in a property file\n")
encryptFlags.PrintDefaults()
}
}

func encrypt() {
if *encryptValue == "" {
fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n")
encryptFlags.Usage()
os.Exit(300)
}
if *encryptPass == "" {
*encryptPass = readPassword("Password:", encryptFlags, 301)
}
if *encryptAlg != props.EncryptAESGCM {
fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n")
encryptFlags.Usage()
os.Exit(302)
}

switch len(*encryptPass) {
case 16, 24, 32:
default:
fmt.Fprintf(flag.CommandLine.Output(), "the password parameter must be 16, 24, or 32 bytes\n")
encryptFlags.Usage()
os.Exit(303)
}

enc, err := props.Encrypt(*encryptAlg, *encryptPass, *encryptValue)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "encrypt error: %v\n", err)
os.Exit(304)
}
fmt.Println(enc)
}
Loading

0 comments on commit f72b88c

Please sign in to comment.