func SetCommand() cli.Command { return cli.Command{ Name: "set", Usage: "Set a key value pair in the encrypted data store", Action: func(c *cli.Context) { var file string = c.String("file") var key string = c.Args().First() var value string if file != "" { if !hasExpectedArgs(c.Args(), 1) { trousseau.ErrorLogger.Fatal("Invalid number of arguments provided to set command") } } else { if !hasExpectedArgs(c.Args(), 2) { trousseau.ErrorLogger.Fatal("Invalid number of arguments provided to set command") } value = c.Args()[1] } trousseau.SetAction(key, value, file) if c.Bool("verbose") == true { trousseau.InfoLogger.Println(fmt.Sprintf("%s:%s", key, value)) } }, Flags: []cli.Flag{ cli.StringFlag{ Name: "file, f", Usage: "Read key's value from provided file", }, }, } }
func main() { if len(os.Args) < 2 { log.Fatal("Command is not specified.") os.Exit(1) } pwd, err := os.Getwd() if err != nil { log.Fatal(err) } switch os.Args[1] { case "get": if len(os.Args) < 3 { log.Fatal("get key") } key := os.Args[2] tr, err := trousseau.OpenTrousseau(trousseau.InferStorePath()) if err != nil { log.Fatal(err) } store, err := tr.Decrypt() if err != nil { log.Fatal(err) } keys := store.Data.Keys() dir := pwd for dir != "." && dir != "/" { for _, k := range keys { if k == dir+SEPARATOR+key { data, err := store.Data.Get(k) if err != nil { log.Fatal(err) } fmt.Print(data) os.Exit(0) } } dir = path.Dir(dir) } os.Exit(1) case "set": if len(os.Args) < 4 { log.Fatal("set key value") } trousseau.SetAction(pwd+SEPARATOR+os.Args[2], os.Args[3], "") case "list": tr, err := trousseau.OpenTrousseau(trousseau.InferStorePath()) if err != nil { log.Fatal(err) } store, err := tr.Decrypt() if err != nil { log.Fatal(err) } keys := store.Data.Keys() dir := pwd results := make(map[string]interface{}) for dir != "." && dir != "/" { for _, k := range keys { if strings.HasPrefix(k, dir+SEPARATOR) { key := strings.TrimPrefix(k, dir+SEPARATOR) if _, ok := results[key]; !ok && !strings.HasPrefix(key, "_") { results[key], err = store.Data.Get(k) if err != nil { log.Fatal(err) } } } } dir = path.Dir(dir) } for k, v := range results { fmt.Printf("'%s'='%s'\n", k, v) } default: log.Fatal("Unknown command " + os.Args[1]) } }