Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.hadoop.hdds.cli;

import static java.util.Collections.unmodifiableList;

import jakarta.annotation.Nonnull;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/** Parameter for specifying list of items, reading from stdin if "-" is given as first item. */
public abstract class ItemsFromStdin implements Iterable<String> {

protected static final String FORMAT_DESCRIPTION =
": one or more, separated by spaces. To read from stdin, specify '-' and supply one item per line.";

private List<String> items;

protected void setItems(List<String> arguments) {
items = readItemsFromStdinIfNeeded(arguments);
}

public List<String> getItems() {
return unmodifiableList(items);
}

@Nonnull
@Override
public Iterator<String> iterator() {
return items.iterator();
}

public int size() {
return items.size();
}

private static List<String> readItemsFromStdinIfNeeded(List<String> parameters) {
if (parameters.isEmpty() || !"-".equals(parameters.iterator().next())) {
return parameters;
}

List<String> items = new ArrayList<>();
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name());
while (scanner.hasNextLine()) {
items.add(scanner.nextLine().trim());
}
return items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.hadoop.hdds.scm.cli.container;

import java.util.List;
import org.apache.hadoop.hdds.cli.ItemsFromStdin;
import picocli.CommandLine;

/** Parameter for specifying list of container IDs. */
@CommandLine.Command
public class ContainerIDParameters extends ItemsFromStdin {

@CommandLine.Parameters(description = "Container IDs" + FORMAT_DESCRIPTION,
arity = "1..*",
paramLabel = "<container ID>")
public void setContainerIDs(List<String> arguments) {
setItems(arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.client.ReplicationConfig;
Expand All @@ -43,7 +42,6 @@
import org.apache.hadoop.hdds.server.JsonUtils;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;

/**
* This is the handler that process container info command.
Expand All @@ -60,40 +58,20 @@ public class InfoSubcommand extends ScmSubcommand {
description = "Format output as JSON")
private boolean json;

@Parameters(description = "One or more container IDs separated by spaces. " +
"To read from stdin, specify '-' and supply the container IDs " +
"separated by newlines.",
arity = "1..*",
paramLabel = "<container ID>")
private String[] containerList;
@CommandLine.Mixin
private ContainerIDParameters containerList;

private boolean multiContainer = false;

@Override
public void execute(ScmClient scmClient) throws IOException {
boolean first = true;
boolean stdin = false;
if (containerList.length > 1) {
multiContainer = true;
} else if (containerList[0].equals("-")) {
stdin = true;
// Assume multiple containers if reading from stdin
multiContainer = true;
}
multiContainer = containerList.size() > 1;

printHeader();
if (stdin) {
Scanner scanner = new Scanner(System.in, "UTF-8");
while (scanner.hasNextLine()) {
String id = scanner.nextLine().trim();
printOutput(scmClient, id, first);
first = false;
}
} else {
for (String id : containerList) {
printOutput(scmClient, id, first);
first = false;
}
for (String id : containerList) {
printOutput(scmClient, id, first);
first = false;
}
printFooter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,23 @@

package org.apache.hadoop.hdds.scm.cli.datanode;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.apache.hadoop.hdds.cli.ItemsFromStdin;
import picocli.CommandLine;

/** Parameter for specifying list of hostnames. */
@CommandLine.Command
public class HostNameParameters {
public class HostNameParameters extends ItemsFromStdin {

@CommandLine.Parameters(description = "One or more host names separated by spaces. " +
"To read from stdin, specify '-' and supply the host names " +
"separated by newlines.",
@CommandLine.Parameters(description = "Host names" + FORMAT_DESCRIPTION,
arity = "1..*",
paramLabel = "<host name>")
private List<String> parameters = new ArrayList<>();
public void setHostNames(List<String> arguments) {
setItems(arguments);
}

public List<String> getHostNames() {
List<String> hosts;
// Whether to read from stdin
if (parameters.get(0).equals("-")) {
hosts = new ArrayList<>();
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name());
while (scanner.hasNextLine()) {
hosts.add(scanner.nextLine().trim());
}
} else {
hosts = parameters;
}
return hosts;
return getItems();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private static class ExecutionMode {

public Shell() {
super(new PicocliCommandsFactory());
getCmd().setExecutionStrategy(this::execute);
}

public String name() {
Expand All @@ -79,26 +80,19 @@ public String prompt() {
return name();
}

@Override
public void run(String[] argv) {
private int execute(CommandLine.ParseResult parseResult) {
name = spec.name();

try {
// parse args to check if interactive mode is requested
getCmd().parseArgs(argv);
} catch (Exception ignored) {
// failure will be reported by regular, non-interactive run
}

if (executionMode != null && (executionMode.interactive || !executionMode.command.isEmpty())) {
if (parseResult.hasMatchedOption("--interactive") || parseResult.hasMatchedOption("--execute")) {
spec.name(""); // use short name (e.g. "token get" instead of "ozone sh token get")
installBatchExceptionHandler();
new REPL(this, getCmd(), (PicocliCommandsFactory) getCmd().getFactory(), executionMode.command);
} else {
TracingUtil.initTracing("shell", getOzoneConf());
String spanName = spec.name() + " " + String.join(" ", argv);
TracingUtil.executeInNewSpan(spanName, () -> super.run(argv));
return 0;
}

TracingUtil.initTracing("shell", getOzoneConf());
String spanName = spec.name() + " " + String.join(" ", parseResult.originalArgs());
return TracingUtil.executeInNewSpan(spanName, () -> new CommandLine.RunLast().execute(parseResult));
}

private void installBatchExceptionHandler() {
Expand Down