diff --git a/src/java/org/apache/cassandra/cli/CliCompleter.java b/src/java/org/apache/cassandra/cli/CliCompleter.java new file mode 100644 index 0000000000..4d649bef3a --- /dev/null +++ b/src/java/org/apache/cassandra/cli/CliCompleter.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.cli; + +import jline.SimpleCompletor; + +public class CliCompleter extends SimpleCompletor +{ + private static String[] commands = { + "connect", + "describe keyspace", + "exit", + "help", + "quit", + "show config file", + "show cluster name", + "show keyspaces", + "show version", + }; + private static String[] keyspaceCommands = { + "get", + "set", + "count", + "del" + }; + + public CliCompleter() + { + super(commands); + } + + String[] getKeyspaceCommands() + { + return keyspaceCommands; + } +} diff --git a/src/java/org/apache/cassandra/cli/CliMain.java b/src/java/org/apache/cassandra/cli/CliMain.java index a4bd4c5d05..1eb46434e2 100644 --- a/src/java/org/apache/cassandra/cli/CliMain.java +++ b/src/java/org/apache/cassandra/cli/CliMain.java @@ -40,6 +40,7 @@ public class CliMain private static Cassandra.Client thriftClient_ = null; private static CliSessionState css_ = new CliSessionState(); private static CliClient cliClient_; + private static CliCompleter completer_ = new CliCompleter(); // Establish a thrift connection to cassandra instance public static void connect(String server, int port) @@ -71,6 +72,27 @@ public class CliMain thriftClient_ = cassandraClient; cliClient_ = new CliClient(css_, thriftClient_); + + // Extend the completer with keyspace and column family data. + try + { + for (String keyspace : thriftClient_.get_string_list_property("keyspaces")) + { + // Ignore system column family + if (keyspace.equals("system")) + continue; + + for (String cf : cliClient_.getCFMetaData(keyspace).keySet()) + { + for (String cmd : completer_.getKeyspaceCommands()) + completer_.addCandidateString(String.format("%s %s.%s", cmd, keyspace, cf)); + } + } + } + catch (Exception e) + { + // Yes, we really do want to ignore any exceptions encountered here. + } css_.out.printf("Connected to %s/%d\n", server, port); } @@ -133,7 +155,8 @@ public class CliMain cliClient_ = new CliClient(css_, null); } - ConsoleReader reader = new ConsoleReader(); + ConsoleReader reader = new ConsoleReader(); + reader.addCompletor(completer_); reader.setBellEnabled(false); String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;