aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-03-21 18:50:55 +0100
committerGibheer <gibheer@gmail.com>2015-03-21 18:50:55 +0100
commitd983dbae543f93bf1c0019af1f24c11d4a1840ff (patch)
treea23f12c9c95d4140e11e7e0ab0adaf5a005eadc8 /main.go
parentccaef440f4f5c2c89775d87460f70267edf1bbc4 (diff)
rework the parameter management
With this change it is now possible to provide help messages for all commands. This will help to further cleanup and minimize the code base.
Diffstat (limited to 'main.go')
-rw-r--r--main.go193
1 files changed, 81 insertions, 112 deletions
diff --git a/main.go b/main.go
index eb661e6..7764d69 100644
--- a/main.go
+++ b/main.go
@@ -8,107 +8,94 @@ import (
"io/ioutil"
"math/big"
"os"
- "path/filepath"
+// "path/filepath"
"github.com/gibheer/pki"
)
+const (
+ ErrorProgram int = iota
+ ErrorFlagInput
+ ErrorInput
+)
+
var (
EmptyByteArray = make([]byte, 0)
)
func main() {
- if len(os.Args) == 1 {
- crash_with_help(1, "No module selected!")
- }
- switch os.Args[1] {
- case "create-private": create_private_key()
- case "create-public": create_public_key()
- case "sign-input": sign_input()
- case "verify-signature": verify_input()
- case "create-cert-sign": create_sign_request()
- case "create-cert": create_cert()
- case "help": print_modules()
- case "--help": print_modules()
-// case "info": info_on_file()
- default: crash_with_help(1, "Command not supported!")
- }
+ InitFlags()
+ CmdRoot.Execute()
}
-// create a private key
-func create_private_key() {
- fs := NewFlags("create-private")
- fs.AddOutput()
- fs.AddPrivateKeyGenerationFlags()
- err := fs.Parse(program_args())
- if err != nil { os.Exit(2) }
+// create a new private key
+func create_private_key(cmd *Command, args []string) {
+ err := checkFlags(checkOutput, checkPrivateKeyGeneration)
+ if err != nil {
+ crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err)
+ }
var pk pki.Pemmer
- switch fs.Flags.PrivateKeyGenerationFlags.Type {
- case "ecdsa": pk, err = pki.NewPrivateKeyEcdsa(fs.Flags.PrivateKeyGenerationFlags.Curve)
- case "rsa": pk, err = pki.NewPrivateKeyRsa(fs.Flags.PrivateKeyGenerationFlags.Size)
+ switch FlagPrivateKeyGeneration.Type {
+ case "ecdsa": pk, err = pki.NewPrivateKeyEcdsa(FlagPrivateKeyGeneration.Curve)
+ case "rsa": pk, err = pki.NewPrivateKeyRsa(FlagPrivateKeyGeneration.Size)
+ default: crash_with_help(cmd, ErrorInput, "Unknown private key type '%s'", FlagPrivateKeyGeneration.Type)
}
- if err != nil { os.Exit(2) }
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error creating private key: %s", err) }
marsh_pem, err := pk.MarshalPem()
- if err != nil { os.Exit(2) }
- _, err = marsh_pem.WriteTo(fs.Flags.Output)
- if err != nil { os.Exit(2) }
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error when marshalling to pem: %s", err) }
+ _, err = marsh_pem.WriteTo(FlagOutput)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error when writing output: %s", err) }
}
// create a public key derived from a private key
-func create_public_key() {
- fs := NewFlags("create-public")
- fs.AddPrivateKey()
- fs.AddOutput()
- err := fs.Parse(program_args())
- if err != nil { os.Exit(2) }
+func create_public_key(cmd *Command, args []string) {
+ err := checkFlags(checkPrivateKey, checkOutput)
+ if err != nil {
+ crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err)
+ }
var pub_key pki.Pemmer
- pub_key = fs.Flags.PrivateKey.Public()
+ pub_key = FlagPrivateKey.Public()
marsh_pem, err := pub_key.MarshalPem()
- if err != nil { os.Exit(2) }
- _, err = marsh_pem.WriteTo(fs.Flags.Output)
- if err != nil { os.Exit(2) }
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error when marshalling to pem: %s", err) }
+ _, err = marsh_pem.WriteTo(FlagOutput)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error when writing output: %s", err) }
}
// sign a message using he private key
-func sign_input() {
- fs := NewFlags("sign-input")
- fs.AddPrivateKey()
- fs.AddInput()
- fs.AddOutput()
- err := fs.Parse(program_args())
- if err != nil { os.Exit(2) }
-
- message, err := ioutil.ReadAll(fs.Flags.Input)
- if err != nil { crash_with_help(2, "Error reading input: %s", err) }
- signature, err := fs.Flags.PrivateKey.Sign(message, crypto.SHA256)
- if err != nil { crash_with_help(2, "Could not compute signature: %s", err) }
- _, err = io.WriteString(fs.Flags.Output, base64.StdEncoding.EncodeToString(signature))
- if err != nil { crash_with_help(2, "Could not write to output: %s", err) }
+func sign_input(cmd *Command, args []string) {
+ err := checkFlags(checkPrivateKey, checkInput, checkOutput)
+ if err != nil {
+ crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err)
+ }
+
+ message, err := ioutil.ReadAll(FlagInput)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error reading input: %s", err) }
+ signature, err := FlagPrivateKey.Sign(message, crypto.SHA256)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Could not compute signature: %s", err) }
+ _, err = io.WriteString(FlagOutput, base64.StdEncoding.EncodeToString(signature))
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Could not write to output: %s", err) }
// if we print to stderr, send a final line break to make the output nice
- if fs.Flags.Output == os.Stdout {
+ if FlagOutput == os.Stdout {
// we can ignore the result, as either Stdout did work or not
- _, _ = io.WriteString(fs.Flags.Output, "\n")
+ _, _ = io.WriteString(FlagOutput, "\n")
}
}
// verify a message using a signature and a public key
-func verify_input() {
- fs := NewFlags("sign-input")
- fs.AddPublicKey()
- fs.AddInput()
- fs.AddOutput()
- fs.AddSignature()
- err := fs.Parse(program_args())
- if err != nil { os.Exit(2) }
-
- signature := fs.Flags.Signature
- message, err := ioutil.ReadAll(fs.Flags.Input)
- if err != nil { crash_with_help(2, "Error reading input: %s", err) }
- valid, err := fs.Flags.PublicKey.Verify(message, signature, crypto.SHA256)
- if err != nil { crash_with_help(2, "Could not verify message with signature: %s", err) }
+func verify_input(cmd *Command, args []string) {
+ err := checkFlags(checkPrivateKey, checkInput, checkOutput, checkSignature)
+ if err != nil {
+ crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err)
+ }
+
+ signature := FlagSignature
+ message, err := ioutil.ReadAll(FlagInput)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error reading input: %s", err) }
+ valid, err := FlagPublicKey.Verify(message, signature, crypto.SHA256)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Could not verify message using signature: %s", err) }
if valid {
fmt.Println("valid")
os.Exit(0)
@@ -118,63 +105,45 @@ func verify_input() {
}
// create a certificate sign request
-func create_sign_request() {
- fs := NewFlags("create-cert-sign")
- fs.AddPrivateKey()
- fs.AddOutput()
- fs.AddCertificateFields()
- fs.Parse(program_args())
-
- csr, err := fs.Flags.CertificateData.ToCertificateRequest(fs.Flags.PrivateKey)
- if err != nil { crash_with_help(2, "Could not create certificate sign request: %s", err) }
+func create_sign_request(cmd *Command, args []string) {
+ err := checkFlags(checkPrivateKey, checkOutput, checkCertificateFields)
+ if err != nil {
+ crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err)
+ }
+
+ csr, err := FlagCertificateData.ToCertificateRequest(FlagPrivateKey)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Could not create certificate sign request: %s", err) }
pem_block, err := csr.MarshalPem()
- if err != nil { crash_with_help(2, "Could not covnert to pem: %s", err) }
- _, err = pem_block.WriteTo(fs.Flags.Output)
- if err != nil { crash_with_help(2, "Encoding didn't work: %s", err) }
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error when marshalling to pem: %s", err) }
+ _, err = pem_block.WriteTo(FlagOutput)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Could not write to output: %s", err) }
}
-func create_cert() {
- fs := NewFlags("create-cert")
- fs.AddPrivateKey()
- fs.AddCSR()
- fs.AddOutput()
- fs.Parse(program_args())
+func create_cert(cmd *Command, args []string) {
+ err := checkFlags(checkPrivateKey, checkOutput, checkCSR)
+ if err != nil {
+ crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err)
+ }
// TODO implement flags for all certificate options
cert_opts := pki.CertificateOptions{}
cert_opts.SerialNumber = big.NewInt(1)
- cert, err := fs.Flags.CertificateSignRequest.ToCertificate(
- fs.Flags.PrivateKey,
+ cert, err := FlagCertificateSignRequest.ToCertificate(
+ FlagPrivateKey,
cert_opts,
nil,
)
- if err != nil { crash_with_help(2, "Error generating certificate: %s", err) }
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error generating certificate: %s", err) }
pem_block, err := cert.MarshalPem()
- if err != nil { crash_with_help(2, "Error converting to pem: %s", err) }
- _, err = pem_block.WriteTo(fs.Flags.Output)
- if err != nil { crash_with_help(2, "Output didn't work: %s", err) }
-}
-
-// print the module help
-func print_modules() {
- fmt.Printf(`Usage: %s command args
-where 'command' is one of:
- create-private create a new private key
- create-public create a public key from a private one
- sign-input sign a message with a private key
- verify-signature verify a signature
- create-cert-sign create a new certificate sign request
- create-cert sign a certificate request
- help show this help
- info get info on a file
-`, filepath.Base(os.Args[0]))
- fmt.Println()
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Error when marshalling to pem: %s", err) }
+ _, err = pem_block.WriteTo(FlagOutput)
+ if err != nil { crash_with_help(cmd, ErrorProgram, "Could not write to output: %s", err) }
}
// crash and provide a helpful message
-func crash_with_help(code int, message string, args ...interface{}) {
+func crash_with_help(cmd *Command, code int, message string, args ...interface{}) {
fmt.Fprintf(os.Stderr, message + "\n", args...)
- print_modules()
+ cmd.Usage()
os.Exit(code)
}