-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
linters: | ||
enable-all: true | ||
|
||
disable: | ||
- maligned | ||
- scopelint | ||
- interfacer | ||
- ifshort | ||
- exhaustivestruct | ||
- golint | ||
- forbidigo | ||
- testpackage | ||
- goerr113 | ||
- paralleltest | ||
- gomnd | ||
- varnamelen | ||
- wrapcheck | ||
- wsl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/blaskovicz/go-cryptkeeper" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
var ( | ||
operation string | ||
key string | ||
value string | ||
) | ||
|
||
func init() { | ||
flag.StringVar(&operation, "o", "", "operation: encrypt or decrypt") | ||
flag.StringVar(&key, "k", "", "encryption key") | ||
flag.StringVar(&value, "v", "", "target value of the operation") | ||
|
||
flag.Parse() | ||
} | ||
|
||
func main() { | ||
if operation == "" || key == "" || value == "" { | ||
flag.PrintDefaults() | ||
if err := run(); err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
if err := cryptkeeper.SetCryptKey([]byte(key)); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
func run() error { | ||
op, k, v, err := getFlags() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cryptkeeper.SetCryptKey([]byte(k)); err != nil { | ||
return err | ||
} | ||
|
||
var result string | ||
var err error | ||
switch operation { | ||
|
||
switch op { | ||
case "encrypt": | ||
result, err = cryptkeeper.Encrypt(value) | ||
result, err = cryptkeeper.Encrypt(v) | ||
case "decrypt": | ||
result, err = cryptkeeper.Decrypt(value) | ||
result, err = cryptkeeper.Decrypt(v) | ||
default: | ||
fmt.Println("Invalid operation: ", operation) | ||
os.Exit(1) | ||
return usageError(fmt.Sprintf("Invalid operation: %s", op)) | ||
} | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
return err | ||
} | ||
|
||
fmt.Println(result) | ||
|
||
return nil | ||
} | ||
|
||
func getFlags() (string, string, string, error) { | ||
o := pflag.String("o", "", "operation: encrypt or decrypt") | ||
k := pflag.String("k", "", "encryption key") | ||
v := pflag.String("v", "", "target value of the operation") | ||
|
||
var errs []string | ||
if empty(o) { | ||
errs = append(errs, "operation flag missing") | ||
} | ||
if empty(k) { | ||
errs = append(errs, "key flag missing") | ||
} | ||
if empty(v) { | ||
errs = append(errs, "value flag missing") | ||
} | ||
if len(errs) > 0 { | ||
return "", "", "", usageError(strings.Join(errs, "\n")) | ||
} | ||
|
||
return *o, *k, *v, nil | ||
} | ||
|
||
func empty(s *string) bool { | ||
if s == nil || *s == "" { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
type usageError string | ||
|
||
func (ue usageError) Error() string { | ||
return fmt.Sprintf("%s\n\nUsage of %s:\n%s", string(ue), os.Args[0], pflag.CommandLine.FlagUsages()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/blaskovicz/go-cryptkeeper v0.0.0-20190930202326-736b85609f91 h1:htfkBBjhD5gi6WkDbU1Eq0qE67h0vAmeb3HWtn/Qdlc= | ||
github.com/blaskovicz/go-cryptkeeper v0.0.0-20190930202326-736b85609f91/go.mod h1:aLh4O6/WQZYvQ/LKF4hPidOh/oofXPjIShojoLvbrjg= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= |