package main import ( "bufio" "flag" "fmt" "os" "strings" "g.arns.lt/zordsdavini/zordfsdb" ) var ( rootDir string executeCommand string ) func main() { flag.StringVar(&rootDir, "d", "./", "database root directory") flag.StringVar(&executeCommand, "e", "", "execute command") flag.Parse() db, err := zordfsdb.InitDB(rootDir) if err != nil { panic(err) } if len(executeCommand) > 0 { ExecuteCommand(db, executeCommand) } else { reader := bufio.NewReader(os.Stdin) fmt.Println("ZordFsDB Shell") fmt.Println("---------------------") for { fmt.Print("> ") text, _ := reader.ReadString('\n') // convert CRLF to LF text = strings.Replace(text, "\n", "", -1) if strings.Compare("exit", strings.ToLower(text)) == 0 { fmt.Println("BYE :)") break } ExecuteCommand(db, text) } } } func ExecuteCommand(db zordfsdb.DB, commands string) error { for _, command := range strings.Split(commands, ";") { command = strings.Trim(command, " ") parts := strings.Split(command, " ") switch strings.ToLower(parts[0]) { case "keys": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: KEYS [path]") } fmt.Printf("{%s} KEYS: ", parts[1]) fmt.Println(db.Keys(parts[1])) break case "get": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: GET [path]") } value, ok := db.Get(parts[1]) if !ok { fmt.Println("No value found.") } else { fmt.Printf("{%s} VALUE: ", parts[1]) fmt.Println(value) } break case "inc": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: INC [path]") } ok := db.Inc(parts[1]) if !ok { fmt.Println("No value found.") } else { fmt.Printf("{%s} INC OK\n", parts[1]) } break case "dec": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: DEC [path]") } ok := db.Dec(parts[1]) if !ok { fmt.Println("No value found.") } else { fmt.Printf("{%s} DEC OK\n", parts[1]) } break case "now": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: NOW [path]") } ok := db.Now(parts[1]) if !ok { fmt.Println("No value found.") } else { fmt.Printf("{%s} NOW OK\n", parts[1]) } break case "del": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: DEL [path]") } ok := db.Del(parts[1]) if !ok { fmt.Println("No value/object found.") } else { fmt.Printf("{%s} DELETED\n", parts[1]) } break case "save": if len(parts) < 3 || len(parts[1]) == 0 { return fmt.Errorf("err format: SAVE [path] [value|long value]") } value := strings.Trim(strings.Join(parts[2:], " "), "\"") ok := db.Save(parts[1], value) if !ok { fmt.Println("No value found.") } else { fmt.Printf("{%s} SAVED\n", parts[1]) } break case "create": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: CREATE [path]") } ok := db.CreateNode(parts[1]) if !ok { fmt.Println("No path found.") } else { fmt.Printf("{%s} CREATED\n", parts[1]) } break case "add": if len(parts) == 1 || len(parts[1]) == 0 { return fmt.Errorf("err format: ADD [path]") } id, err := db.AddObject(parts[1]) if err != nil { fmt.Println("No path found.") } else { fmt.Printf("{%s} ADDED with ID: %s\n", parts[1], id) } break } } return nil }