Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cluster list #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
119 changes: 46 additions & 73 deletions src/main/java/com/alibaba/nacos/cli/commands/NacosCtl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.nacos.cli.commands;

import com.alibaba.nacos.cli.commands.cluster.NacosCluster;
import com.alibaba.nacos.cli.commands.config.NacosConfig;
import com.alibaba.nacos.cli.commands.instance.NacosInstance;
import com.alibaba.nacos.cli.commands.namespace.NacosNamespace;
Expand All @@ -20,80 +21,52 @@
*
* @author lehr
*/
@Command(
name = APP_NAME,
mixinStandardHelpOptions = MIXIN_STANDARD_HELP_OPTIONS,
version = VERSION_NAME,
description = CLI_DESCRIPTION,
commandListHeading = COMMAND_LIST_HEADING,
footer = FOOTER,
subcommands = {
CommandLine.HelpCommand.class,
NacosQuit.class,
NacosClear.class,
NacosConfig.class,
NacosNamespace.class,
NacosInstance.class,
NacosSwitch.class,
NacosService.class,
// NacosMetrics.class,
NacosUse.class,
// NacosWatch.class,
})
@Command(name = APP_NAME, mixinStandardHelpOptions = MIXIN_STANDARD_HELP_OPTIONS, version = VERSION_NAME, description = CLI_DESCRIPTION, commandListHeading = COMMAND_LIST_HEADING, footer = FOOTER, subcommands = {
CommandLine.HelpCommand.class, NacosQuit.class, NacosClear.class, NacosConfig.class, NacosNamespace.class,
NacosInstance.class, NacosSwitch.class, NacosService.class,
// NacosMetrics.class,
NacosUse.class, NacosCluster.class
// NacosWatch.class,
})
public class NacosCtl implements Runnable {

@CommandLine.Option(
names = {"-e", "--endpoint"},
paramLabel = "<endpoint ip>",
description = "The Nacos-Server host Ip.")
private String host;

@CommandLine.Option(
names = {"-p", "--port"},
paramLabel = "<port>",
description = "The port of Nacos-Server.")
private Integer port;

@CommandLine.Option(
names = {"-u", "--username"},
paramLabel = "<username>",
description = "Nacos authentication username.")
private String username;

@CommandLine.Option(
names = {"-pswd", "--password"},
paramLabel = "<password>",
description = "Nacos password username.")
private String password;

@CommandLine.Option(
names = {"-ak", "--accessKey"},
paramLabel = "<accessKey>",
description = "Nacos access key.")
private String accessKey;

@CommandLine.Option(
names = {"-sk", "--secretKey"},
paramLabel = "<secretKey>",
description = "Nacos secret key.")
private String secretKey;

@Override
public void run() {
System.out.println(GREETING);
Map<String,String> confs = new HashMap<>();
for (Field f : this.getClass().getDeclaredFields()) {
if(f.isAnnotationPresent(CommandLine.Option.class)){
try{
Object o = f.get(this);
if(o!=null){
confs.put(f.getName(),o.toString());
}
}catch (Exception e){
//todo

@CommandLine.Option(names = {"-e",
"--endpoint"}, paramLabel = "<endpoint ip>", description = "The Nacos-Server host Ip.")
private String host;

@CommandLine.Option(names = {"-p", "--port"}, paramLabel = "<port>", description = "The port of Nacos-Server.")
private Integer port;

@CommandLine.Option(names = {"-u",
"--username"}, paramLabel = "<username>", description = "Nacos authentication username.")
private String username;

@CommandLine.Option(names = {"-pswd",
"--password"}, paramLabel = "<password>", description = "Nacos password username.")
private String password;

@CommandLine.Option(names = {"-ak", "--accessKey"}, paramLabel = "<accessKey>", description = "Nacos access key.")
private String accessKey;

@CommandLine.Option(names = {"-sk", "--secretKey"}, paramLabel = "<secretKey>", description = "Nacos secret key.")
private String secretKey;

@Override
public void run() {
System.out.println(GREETING);
Map<String, String> confs = new HashMap<>();
for (Field f : this.getClass().getDeclaredFields()) {
if (f.isAnnotationPresent(CommandLine.Option.class)) {
try {
Object o = f.get(this);
if (o != null) {
confs.put(f.getName(), o.toString());
}
} catch (Exception e) {
//todo
}
}
}
}
ConfigLoader.preload(confs);
}
ConfigLoader.preload(confs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.alibaba.nacos.cli.commands.cluster;

import com.alibaba.nacos.cli.commands.config.NacosConfigGet;
import picocli.CommandLine;

import static com.alibaba.nacos.cli.utils.HintUtils.CLUSTER;
import static com.alibaba.nacos.cli.utils.HintUtils.CLUSTER_CONFIG;
import static com.alibaba.nacos.cli.utils.HintUtils.DESCRIPTION_CLUSTER;
import static com.alibaba.nacos.cli.utils.HintUtils.DESCRIPTION_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.HEADER_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.OPTION_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.PARAMETER_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.SORT_OPTIONS;
import static com.alibaba.nacos.cli.utils.HintUtils.SYNOPSIS_HEADING;

/**
* cluster subcommand.
*
* @author zzq
*/
@CommandLine.Command(name = CLUSTER, sortOptions = SORT_OPTIONS,
headerHeading = HEADER_HEADING, synopsisHeading = SYNOPSIS_HEADING,
descriptionHeading = DESCRIPTION_HEADING, parameterListHeading = PARAMETER_LIST_HEADING,
optionListHeading = OPTION_LIST_HEADING, header = CLUSTER_CONFIG, description = DESCRIPTION_CLUSTER,
subcommands = {CommandLine.HelpCommand.class, NacosClusterList.class, NaocsClusterGet.class, NaocsClusterDelete.class})
public class NacosCluster implements Runnable {

@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

@Override
public void run() {
spec.commandLine().usage(System.err);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.alibaba.nacos.cli.commands.cluster;

import com.alibaba.nacos.cli.core.LogicHandler;
import com.alibaba.nacos.cli.core.bean.ClusterVo;
import com.alibaba.nacos.cli.core.exception.HandlerException;
import de.vandermeer.asciitable.AsciiTable;
import picocli.CommandLine;

import java.util.List;

import static com.alibaba.nacos.cli.utils.HintUtils.DESCRIPTION_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.HEADER_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.NAME_LIST;
import static com.alibaba.nacos.cli.utils.HintUtils.OPTION_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.PARAMETER_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.SORT_OPTIONS;
import static com.alibaba.nacos.cli.utils.HintUtils.SYNOPSIS_HEADING;

/**
* cluster list command handler.
* @author zzq
*/
@CommandLine.Command(name = NAME_LIST, sortOptions = SORT_OPTIONS,
headerHeading = HEADER_HEADING, synopsisHeading = SYNOPSIS_HEADING,
descriptionHeading = DESCRIPTION_HEADING, parameterListHeading = PARAMETER_LIST_HEADING,
optionListHeading = OPTION_LIST_HEADING, header = "list node",
description = "List node from cluster.")
public class NacosClusterList implements Runnable {

@CommandLine.Option(names = {"-i", "--ip"}, paramLabel = "<ip>", description = "Cluster node ip.")
String ip = "";

@CommandLine.Option(names = {"-n", "--pageNo"}, paramLabel = "<pageNo>", description = "Page number.")
Integer pageNo = 1;

@CommandLine.Option(names = {"-s", "--pageSize"}, paramLabel = "<pageSize>", description = "Page size.")
Integer pageSize = 20;

@SuppressWarnings("checkstyle:VariableDeclarationUsageDistance")
@Override
public void run() {
try {
System.out.println(
drawingTable(LogicHandler.listCluster(ip, pageNo, pageSize)).render()
);
} catch (HandlerException e) {
System.out.println(e.getMessage());
}
}

private AsciiTable drawingTable(List<ClusterVo> vo) {
AsciiTable at = new AsciiTable();
this.definitionTableHead(at);
this.fillData(at, vo);
return at;
}

private void fillData(AsciiTable at, List<ClusterVo> vo) {
for (ClusterVo row : vo) {
at.addRow(row.getIp(), row.getPort(), row.getState());
}
at.addRule();
}

private void definitionTableHead(AsciiTable at) {
at.getContext().setWidth(60);
at.addRule();
at.addRow("ip", "port", "state");
at.addRule();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.nacos.cli.commands.cluster;

import com.alibaba.nacos.cli.core.LogicHandler;
import com.alibaba.nacos.cli.core.exception.HandlerException;
import com.alibaba.nacos.cli.input.InputGetter;
import picocli.CommandLine;

import static com.alibaba.nacos.cli.utils.HintUtils.DESCRIPTION_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.HEADER_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.NAME_DELETE;
import static com.alibaba.nacos.cli.utils.HintUtils.OPTION_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.PARAMETER_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.SORT_OPTIONS;
import static com.alibaba.nacos.cli.utils.HintUtils.SYNOPSIS_HEADING;

/**
* cluster get command handler.
* @author zzq
*/
@CommandLine.Command(name = NAME_DELETE, sortOptions = SORT_OPTIONS,
headerHeading = HEADER_HEADING, synopsisHeading = SYNOPSIS_HEADING,
descriptionHeading = DESCRIPTION_HEADING, parameterListHeading = PARAMETER_LIST_HEADING,
optionListHeading = OPTION_LIST_HEADING, header = "delete cluster node",
description = "delete cluster node.")
public class NaocsClusterDelete implements Runnable{

@CommandLine.Parameters(paramLabel = "<ip:port>", description = "cluster node metadata.")
String keyword;

@Override
public void run() {
if(InputGetter.cancelConfirm()){
return ;
}
try {
LogicHandler.deleteClusterNode(keyword);
} catch (HandlerException e) {
e.printStackTrace();
}
System.out.println("done");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.alibaba.nacos.cli.commands.cluster;

import com.alibaba.nacos.cli.core.LogicHandler;
import com.alibaba.nacos.cli.core.bean.ClusterVo;
import com.alibaba.nacos.cli.core.exception.HandlerException;
import picocli.CommandLine;

import java.util.List;

import static com.alibaba.nacos.cli.utils.HintUtils.DESCRIPTION_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.HEADER_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.NAME_GET;
import static com.alibaba.nacos.cli.utils.HintUtils.OPTION_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.PARAMETER_LIST_HEADING;
import static com.alibaba.nacos.cli.utils.HintUtils.SORT_OPTIONS;
import static com.alibaba.nacos.cli.utils.HintUtils.SYNOPSIS_HEADING;

/**
* cluster get command handler.
* @author zzq
*/
@CommandLine.Command(
name = NAME_GET,
sortOptions = SORT_OPTIONS,
headerHeading = HEADER_HEADING,
synopsisHeading = SYNOPSIS_HEADING,
descriptionHeading = DESCRIPTION_HEADING,
parameterListHeading = PARAMETER_LIST_HEADING,
optionListHeading = OPTION_LIST_HEADING,
header = "get metadata",
description = "get cluster node metadata.")
public class NaocsClusterGet implements Runnable{

@CommandLine.Parameters(paramLabel = "<ip:port>", description = "cluster node metadata.")
String keyword;

@Override
public void run() {
try {
List<ClusterVo> clusterVos = LogicHandler.listCluster(keyword, 1, 1);
if (clusterVos.size() > 1) {
throw new HandlerException("Multiple data found, Please enter the complete ip:port");
}
System.out.println(clusterVos.isEmpty() ? "" : clusterVos.get(0).getExtendInfo());
} catch (HandlerException e) {
System.out.println(e.getMessage());
}
}
}
Loading