ddanielr commented on code in PR #6133: URL: https://github.com/apache/accumulo/pull/6133#discussion_r2825585000
########## core/src/main/java/org/apache/accumulo/core/util/ShellCompletionCommand.java: ########## @@ -0,0 +1,117 @@ +/* + * 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 + * + * https://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.accumulo.core.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.accumulo.start.Main; +import org.apache.accumulo.start.spi.CommandGroup; +import org.apache.accumulo.start.spi.CommandGroups; +import org.apache.accumulo.start.spi.KeywordExecutable; +import org.apache.zookeeper.common.StringUtils; + +import com.google.auto.service.AutoService; + +@AutoService(KeywordExecutable.class) +public class ShellCompletionCommand implements KeywordExecutable { + + private static final String HEADER = """ + #! /usr/bin/env bash + # + # 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 + # + # https://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. + # + """; + + private static final String FOOTER = """ + complete -F _complete_accumulo accumulo + """; + + @Override + public String keyword() { + return "create-autocomplete-script"; + } + + @Override + public CommandGroup commandGroup() { + return CommandGroups.OTHER; + } + + @Override + public String description() { + return "Generates a bash completion script"; + } + + @Override + public void execute(String[] args) throws Exception { + System.out.println(HEADER); + printCompletionMethod(); + System.out.println(FOOTER); + } + + private void printCompletionMethod() { + Map<CommandGroup,Map<String,KeywordExecutable>> executables = + Main.getExecutables(ClassLoader.getSystemClassLoader()); + + System.out.println("_complete_accumulo() {"); + System.out.println(" prior=\"$3\""); + System.out.println(" current=\"$2\""); + + System.out.println(" case \"$prior\" in"); + for (CommandGroup group : executables.keySet()) { + if (group == CommandGroups.CLIENT) { + continue; + } + List<String> cmds = new ArrayList<>(executables.get(group).keySet()); + System.out.println(" " + group.key() + ")"); + System.out.println(" commands=\"" + StringUtils.joinStrings(cmds, " ") + "\""); + System.out + .println(" mapfile -t COMPREPLY < <(compgen -W \"${commands}\" -- \"${current}\")"); + System.out.println(" return 0"); + System.out.println(" ;;"); + } + List<String> cmds = new ArrayList<>(executables.get(CommandGroups.CLIENT).keySet()); + cmds.addAll(executables.keySet().stream().map(cg -> cg.key()).collect(Collectors.toList())); + System.out.println(" *)"); + System.out.println(" commands=\"" + StringUtils.joinStrings(cmds, " ") + "\""); Review Comment: ```suggestion System.out.println(" commands=\"" + String.join(" ", cmds) + "\""); ``` ########## core/src/main/java/org/apache/accumulo/core/util/ShellCompletionCommand.java: ########## @@ -0,0 +1,117 @@ +/* + * 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 + * + * https://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.accumulo.core.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.accumulo.start.Main; +import org.apache.accumulo.start.spi.CommandGroup; +import org.apache.accumulo.start.spi.CommandGroups; +import org.apache.accumulo.start.spi.KeywordExecutable; +import org.apache.zookeeper.common.StringUtils; + +import com.google.auto.service.AutoService; + +@AutoService(KeywordExecutable.class) +public class ShellCompletionCommand implements KeywordExecutable { + + private static final String HEADER = """ + #! /usr/bin/env bash + # + # 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 + # + # https://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. + # + """; + + private static final String FOOTER = """ + complete -F _complete_accumulo accumulo + """; + + @Override + public String keyword() { + return "create-autocomplete-script"; + } + + @Override + public CommandGroup commandGroup() { + return CommandGroups.OTHER; + } + + @Override + public String description() { + return "Generates a bash completion script"; + } + + @Override + public void execute(String[] args) throws Exception { + System.out.println(HEADER); + printCompletionMethod(); + System.out.println(FOOTER); + } + + private void printCompletionMethod() { + Map<CommandGroup,Map<String,KeywordExecutable>> executables = + Main.getExecutables(ClassLoader.getSystemClassLoader()); + + System.out.println("_complete_accumulo() {"); + System.out.println(" prior=\"$3\""); + System.out.println(" current=\"$2\""); + + System.out.println(" case \"$prior\" in"); + for (CommandGroup group : executables.keySet()) { + if (group == CommandGroups.CLIENT) { + continue; + } + List<String> cmds = new ArrayList<>(executables.get(group).keySet()); + System.out.println(" " + group.key() + ")"); + System.out.println(" commands=\"" + StringUtils.joinStrings(cmds, " ") + "\""); Review Comment: I don't think the zk helper utility is needed ```suggestion System.out.println(" commands=\"" + String.join(" ", cmds) + "\""); ``` ########## core/src/main/java/org/apache/accumulo/core/util/ShellCompletionCommand.java: ########## @@ -0,0 +1,117 @@ +/* + * 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 + * + * https://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.accumulo.core.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.accumulo.start.Main; +import org.apache.accumulo.start.spi.CommandGroup; +import org.apache.accumulo.start.spi.CommandGroups; +import org.apache.accumulo.start.spi.KeywordExecutable; +import org.apache.zookeeper.common.StringUtils; + +import com.google.auto.service.AutoService; + +@AutoService(KeywordExecutable.class) +public class ShellCompletionCommand implements KeywordExecutable { + + private static final String HEADER = """ + #! /usr/bin/env bash + # + # 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 + # + # https://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. + # + """; + + private static final String FOOTER = """ + complete -F _complete_accumulo accumulo + """; + + @Override + public String keyword() { + return "create-autocomplete-script"; + } + + @Override + public CommandGroup commandGroup() { + return CommandGroups.OTHER; + } + + @Override + public String description() { + return "Generates a bash completion script"; + } + + @Override + public void execute(String[] args) throws Exception { + System.out.println(HEADER); + printCompletionMethod(); + System.out.println(FOOTER); + } + + private void printCompletionMethod() { + Map<CommandGroup,Map<String,KeywordExecutable>> executables = + Main.getExecutables(ClassLoader.getSystemClassLoader()); + + System.out.println("_complete_accumulo() {"); + System.out.println(" prior=\"$3\""); + System.out.println(" current=\"$2\""); + + System.out.println(" case \"$prior\" in"); + for (CommandGroup group : executables.keySet()) { + if (group == CommandGroups.CLIENT) { + continue; + } + List<String> cmds = new ArrayList<>(executables.get(group).keySet()); + System.out.println(" " + group.key() + ")"); + System.out.println(" commands=\"" + StringUtils.joinStrings(cmds, " ") + "\""); + System.out + .println(" mapfile -t COMPREPLY < <(compgen -W \"${commands}\" -- \"${current}\")"); + System.out.println(" return 0"); + System.out.println(" ;;"); + } + List<String> cmds = new ArrayList<>(executables.get(CommandGroups.CLIENT).keySet()); + cmds.addAll(executables.keySet().stream().map(cg -> cg.key()).collect(Collectors.toList())); Review Comment: The stream can be shortened up a bit. ```suggestion cmds.addAll(executables.keySet().stream().map(CommandGroup::key).toList()); ``` Otherwise, the Client group key is getting added to the wildcard section in the script and it shows up as a double space. ``` *) commands="help shell version admin compaction core other process" mapfile -t COMPREPLY < <(compgen -W "${commands}" -- "${current}") return 0 ;; ``` It's very trivial but the following line could be added to remove it. `cmds.remove("");` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
