Skip to content

Commit

Permalink
Better command usage handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Jan 27, 2023
1 parent 05ea31e commit facbde5
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 14 deletions.
4 changes: 3 additions & 1 deletion internal/cmd/aes/cbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
Expand All @@ -42,13 +41,16 @@ Options:
}

if *fKey == "" && *fKeyFile == "" {
fset.Usage()
return errors.New("no key specified, use -k or --key-file to specify it")
}
if *fKey != "" && *fKeyFile != "" {
fset.Usage()
return errors.New("cannot use -k and --key-file at the same time")
}

if *fIV == "" {
fset.Usage()
return errors.New("no IV specified, use -iv to specify it")
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/aes/ecb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
Expand All @@ -40,10 +39,12 @@ Options:
}

if *fKey == "" && *fKeyFile == "" {
fset.Usage()
return errors.New("no key specified, use -key or -key-file to specify it")
}

if *fKey != "" && *fKeyFile != "" {
fset.Usage()
return errors.New("cannot use -key and -key-file at the same time")
}

Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/aes/gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
Expand All @@ -42,14 +41,17 @@ Options:
}

if *fKey == "" && *fKeyFile == "" {
fset.Usage()
return errors.New("no key specified, use -key or -key-file to specify it")
}

if *fKey != "" && *fKeyFile != "" {
fset.Usage()
return errors.New("cannot use -key and -key-file at the same time")
}

if *fIV == "" {
fset.Usage()
return errors.New("no IV specified, use -iv to specify it")
}

Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/aes/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func KeyGen(args []string) error {
fmt.Fprint(os.Stderr, `Usage: pocryp aes-keygen [-out OUTPUT] NUM_BITS
Generate AES key.
Valid NUM_BITS: 128, 192, 256.
If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
Expand All @@ -34,6 +34,7 @@ Options:
}

if fset.NArg() == 0 {
fset.Usage()
return errors.New("number of bits not specified")
}

Expand All @@ -43,6 +44,7 @@ Options:
}

if !(numBits == 128 || numBits == 192 || numBits == 256) {
fset.Usage()
return errors.New("invalid num bits requested")
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/aes/keywrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fWrap := fset.Bool("w", false, "Wrap the input to the output. Default if omitted.")
Expand All @@ -40,9 +39,11 @@ Options:
}

if *fKey == "" && *fKeyFile == "" {
fset.Usage()
return errors.New("no key specified, use -k or --key-file to specify it")
}
if *fKey != "" && *fKeyFile != "" {
fset.Usage()
return errors.New("cannot use -k and --key-file at the same time")
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/hash/sha.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
Expand All @@ -37,11 +36,13 @@ Options:
}

if *fAlg == "" {
fset.Usage()
return errors.New("hash alg not specified, use -alg")
}

hashFunc, err := common.HashFuncFrom(*fAlg)
if err != nil {
fset.Usage()
return err
}

Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/kdf/pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
Expand All @@ -48,21 +47,26 @@ Options:
}

if *fKey == "" && *fKeyFile == "" {
fset.Usage()
return errors.New("no key specified, use -key or -key-file to specify it")
}
if *fKey != "" && *fKeyFile != "" {
fset.Usage()
return errors.New("cannot use -key and -key-file at the same time")
}

if *fSalt == "" && *fSaltFile == "" {
fset.Usage()
return errors.New("no salt specified, use -salt or -salt-file to specify it")
}
if *fSalt != "" && *fSaltFile != "" {
fset.Usage()
return errors.New("cannot use -salt and -salt-file at the same time")
}

hashFunc, err := common.HashFuncFrom(*fHashFunc)
if err != nil {
fset.Usage()
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/rsa/der_pem.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ DER input must be specified in binary form.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fPriv := fset.Bool("priv", false, "Encode PrivateKey from given input.")
Expand Down Expand Up @@ -73,6 +72,7 @@ Options:
case *fPub:
blockType = "RSA PUBLIC KEY"
default:
fset.Usage()
return errors.New("need to specify one of -priv or -pub")
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/rsa/der_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ DER must be specified in hex form.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fPriv := fset.Bool("priv", false, "Encode PrivateKey from given input.")
Expand All @@ -32,6 +31,7 @@ Options:
}

if fset.NArg() != 1 {
fset.Usage()
return errors.New("DER hex string not specified")
}

Expand Down Expand Up @@ -59,6 +59,7 @@ Options:
fmt.Printf("n=%s\n", hex.EncodeToString(key.N.Bytes()))
fmt.Printf("e=%x\n", key.E)
default:
fset.Usage()
return errors.New("need to specify one of -priv or -pub")
}

Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/rsa/kem.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fEncapsulate := fset.Bool("e", false, "Encapsulate the input to the output. Default if omitted.")
Expand All @@ -49,6 +48,7 @@ Options:
}

if *fKey == "" {
fset.Usage()
return errors.New("no key specified, use -key to specify it")
}

Expand All @@ -71,6 +71,7 @@ Options:
}

if *fKdfSalt == "" {
fset.Usage()
return errors.New("KDF salt cannot be empty")
}
kdfSalt, err := hex.DecodeString(*fKdfSalt)
Expand All @@ -80,6 +81,7 @@ Options:

kdfHashFunc, err := common.HashFuncFrom(*fKdfHashFunc)
if err != nil {
fset.Usage()
return err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/rsa/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func KeyGen(args []string) error {
fmt.Fprint(os.Stderr, `Usage: pocryp rsa-keygen [-out OUTPUT] NUM_BITS
Generate RSA key.
Valid NUM_BITS: 2048, 3072, 4096.
If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
Expand All @@ -35,6 +35,7 @@ Options:
}

if fset.NArg() == 0 {
fset.Usage()
return errors.New("number of bits not specified")
}

Expand All @@ -44,6 +45,7 @@ Options:
}

if !(numBits == 2048 || numBits == 3072 || numBits == 4096) {
fset.Usage()
return errors.New("invalid num bits requested")
}

Expand Down
1 change: 0 additions & 1 deletion internal/cmd/rsa/pem_der.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
Expand Down
1 change: 0 additions & 1 deletion internal/cmd/rsa/pub_from_priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
Expand Down
9 changes: 8 additions & 1 deletion internal/cmd/rsa/raw_der.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Convert RSA key from raw values(n, e, d, p, q) to PKCS#1 ASN.1 DER.
Options:
`)
fset.PrintDefaults()
os.Exit(1)
}

fPriv := fset.Bool("priv", false, "Encode PrivateKey from given inputs.")
Expand All @@ -37,10 +36,12 @@ Options:
}

if *fMod == "" {
fset.Usage()
return errors.New("modulus not specified, use -n to specify it")
}

if *fPub && *fPriv {
fset.Usage()
return errors.New("cannot specify -priv and -pub at the same time, choose one")
}

Expand All @@ -55,15 +56,19 @@ Options:
switch {
case *fPriv:
if *fPubExp == 0 {
fset.Usage()
return errors.New("-e is needed")
}
if *fPrivExp == "" {
fset.Usage()
return errors.New("-d is needed")
}
if *fPrime1 == "" {
fset.Usage()
return errors.New("-p is needed")
}
if *fPrime2 == "" {
fset.Usage()
return errors.New("-q is needed")
}
dBytes, err := hex.DecodeString(*fPrivExp)
Expand Down Expand Up @@ -95,6 +100,7 @@ Options:
result = x509.MarshalPKCS1PrivateKey(key)
case *fPub:
if *fPubExp == 0 {
fset.Usage()
return errors.New("-e is needed")
}
key := &rsa.PublicKey{
Expand All @@ -103,6 +109,7 @@ Options:
}
result = x509.MarshalPKCS1PublicKey(key)
default:
fset.Usage()
return errors.New("need to specify one of -priv or -pub")
}

Expand Down

0 comments on commit facbde5

Please sign in to comment.