package kvrepl import ( "fmt" "os" "strings" ) // Parse decides what commands to run and returns a string containing the // value of a read or an error if one occurred. func Parse(s string, d *DB) (string, error) { args := strings.Split(s, " ") switch strings.ToLower(args[0]) { case "read": return read(args[1:], whichDB(d)) case "write": return "", write(args[1:], whichDB(d)) case "delete": return "", del(args[1:], whichDB(d)) case "start": if len(args) != 1 { return "", fmt.Errorf("incorrect usage: START") } d.nest++ d.PKV = false // copy contents of KV1 to KV2 d.KV2 = make(map[string]string) for k, v := range d.KV1 { d.KV2[k] = v } case "commit": if len(args) != 1 { return "", fmt.Errorf("incorrect usage: COMMIT") } if d.PKV { return "", fmt.Errorf("not in a transaction") } if d.nest == 0 { d.PKV = true // copy contents of KV2 to KV1 d.KV1 = make(map[string]string) for k, v := range d.KV2 { d.KV1[k] = v } } case "abort": if len(args) != 1 { return "", fmt.Errorf("incorrect usage: ABORT") } d.PKV = true case "quit": fmt.Println("goodbye") os.Exit(0) default: fmt.Println("not known command") } return "", nil }