From 974b07f3527eb4e795af653f96ddd1e3119ac6da Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Mon, 5 Aug 2019 22:10:44 +0200 Subject: [PATCH] feat: cli now accepts file as argument --- main.go | 38 ++++++++++++++++++++++++++++---------- test/pem-rsa-pubkey.txt | 14 ++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 test/pem-rsa-pubkey.txt diff --git a/main.go b/main.go index 2bb1cd6..14e2420 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,42 @@ package main // import "moul.io/cryptoguess" import ( - "bufio" + "flag" "fmt" - "io" + "io/ioutil" + "log" "os" "moul.io/cryptoguess/cryptoguess" ) func main() { - in := bufio.NewReader(os.Stdin) - input := []byte{} - for { - chunk, err := in.ReadByte() - if err == io.EOF { - break + if err := guess(); err != nil { + log.Printf("Error: %v", err) + os.Exit(1) + } +} + +func guess() error { + flag.Parse() + var data []byte + var err error + switch flag.NArg() { + case 0: + data, err = ioutil.ReadAll(os.Stdin) + if err != nil { + return err } - input = append(input, chunk) + case 1: + data, err = ioutil.ReadFile(flag.Arg(0)) + if err != nil { + return err + } + default: + return fmt.Errorf("input must be from stdin or file") } - question := cryptoguess.New(input) + + question := cryptoguess.New(data) fmt.Println(question) + return nil } diff --git a/test/pem-rsa-pubkey.txt b/test/pem-rsa-pubkey.txt new file mode 100644 index 0000000..437dc95 --- /dev/null +++ b/test/pem-rsa-pubkey.txt @@ -0,0 +1,14 @@ +-----BEGIN PUBLIC KEY----- +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAlRuRnThUjU8/prwYxbty +WPT9pURI3lbsKMiB6Fn/VHOKE13p4D8xgOCADpdRagdT6n4etr9atzDKUSvpMtR3 +CP5noNc97WiNCggBjVWhs7szEe8ugyqF23XwpHQ6uV1LKH50m92MbOWfCtjU9p/x +qhNpQQ1AZhqNy5Gevap5k8XzRmjSldNAFZMY7Yv3Gi+nyCwGwpVtBUwhuLzgNFK/ +yDtw2WcWmUU7NuC8Q6MWvPebxVtCfVp/iQU6q60yyt6aGOBkhAX0LpKAEhKidixY +nP9PNVBvxgu3XZ4P36gZV6+ummKdBVnc3NqwBLu5+CcdRdusmHPHd5pHf4/38Z3/ +6qU2a/fPvWzceVTEgZ47QjFMTCTmCwNt29cvi7zZeQzjtwQgn4ipN9NibRH/Ax/q +TbIzHfrJ1xa2RteWSdFjwtxi9C20HUkjXSeI4YlzQMH0fPX6KCE7aVePTOnB69I/ +a9/q96DiXZajwlpq3wFctrs1oXqBp5DVrCIj8hU2wNgB7LtQ1mCtsYz//heai0K9 +PhE4X6hiE0YmeAZjR0uHl8M/5aW9xCoJ72+12kKpWAa0SFRWLy6FejNYCYpkupVJ +yecLk/4L1W0l6jQQZnWErXZYe0PNFcmwGXy1Rep83kfBRNKRy5tvocalLlwXLdUk +AIU+2GKjyT3iMuzZxxFxPFMCAwEAAQ== +-----END PUBLIC KEY-----