implemented sub subcommand

implemented the sub subcommand and did some clean up for the function
This commit is contained in:
Derek McQuay 2016-03-11 21:50:32 -08:00
parent 60d320088d
commit ca2c861387
2 changed files with 17 additions and 16 deletions

25
main.go
View File

@ -16,10 +16,7 @@ func init() {
} }
func main() { func main() {
//psyfer.ReadConfig() var key string
//psyfer.Substitution("hello")
var input string
//var key string
var decrypt bool var decrypt bool
var trans = &cobra.Command{ var trans = &cobra.Command{
@ -32,11 +29,18 @@ func main() {
} }
var sub = &cobra.Command{ var sub = &cobra.Command{
Use: "sub mode -c [cipher] -k [key] -i [input]", Use: "sub",
Short: "substitution cipher", Short: "substitution cipher",
Long: `perform substitution cipher`, Long: `perform substitution cipher`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("missing input, see -h (--help) for more info") if key == "" || len(args) < 1 {
fmt.Println("missing input, see -h (--help) for more info")
os.Exit(1)
}
psyfer.ReadConfig(key)
for _, arg := range args {
fmt.Println(psyfer.Substitution(arg))
}
}, },
} }
@ -135,16 +139,15 @@ func main() {
) )
sub.Flags().StringVarP( sub.Flags().StringVarP(
&input, &key,
"input", "key",
"i", "k",
"", "",
"string to be encrypted", "file containing key",
) )
var rootCmd = &cobra.Command{Use: "app"} var rootCmd = &cobra.Command{Use: "app"}
rootCmd.AddCommand(sub, aes, trans, vig) rootCmd.AddCommand(sub, aes, trans, vig)
sub.AddCommand(crack)
aes.AddCommand(crack) aes.AddCommand(crack)
trans.AddCommand(random, railfence, split) trans.AddCommand(random, railfence, split)
vig.AddCommand(crack) vig.AddCommand(crack)

View File

@ -2,7 +2,6 @@ package psyfer
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
) )
@ -13,13 +12,13 @@ type KeyJSON struct {
var k = KeyJSON{} var k = KeyJSON{}
func ReadConfig() { func ReadConfig(file string) {
dat, err := ioutil.ReadFile("key.json") dat, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
json.Unmarshal([]byte(dat), &k) json.Unmarshal([]byte(dat), &k)
fmt.Println(k.Key["a"]) k.Key[" "] = " " // keep spaces alive
} }
func Substitution(input string) string { func Substitution(input string) string {
@ -31,6 +30,5 @@ func Substitution(input string) string {
for i := range inputSlice { for i := range inputSlice {
output += k.Key[inputSlice[i]] output += k.Key[inputSlice[i]]
} }
fmt.Println(output)
return output return output
} }