Skip to content

Commit

Permalink
add version info to linkis-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
peacewong committed Oct 4, 2023
1 parent a77aab0 commit 1d3d752
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@

<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -156,6 +158,23 @@ public static CliCtx buildCtx(String[] args) throws LinkisClientRuntimeException
.init();
logger.info("==========std_var============\n" + CliUtils.GSON.toJson(varAccess));

return new CliCtxImpl(params.getCmdType(), template, varAccess, new HashMap<>());
Properties props = new Properties();
try (InputStream inputStream =
CtxBuilder.class.getClassLoader().getResourceAsStream("version.properties")) {
try (InputStreamReader reader = new InputStreamReader(inputStream)) {
try (BufferedReader bufferedReader = new BufferedReader(reader)) {
props.load(bufferedReader);
}
}
} catch (Exception e) {
logger.warn("Failed to load version info", e);
}

String verion = props.getProperty(CliKeys.VERSION);

Map<String, Object> extraMap = new HashMap<>();
extraMap.put(CliKeys.VERSION, verion);

return new CliCtxImpl(params.getCmdType(), template, varAccess, extraMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import org.apache.linkis.cli.application.exception.CommandException;
import org.apache.linkis.cli.application.interactor.command.CmdTemplateFactory;
import org.apache.linkis.cli.application.interactor.command.template.UniversalCmdTemplate;
import org.apache.linkis.cli.application.interactor.job.help.HelpJob;
import org.apache.linkis.cli.application.interactor.job.interactive.InteractiveJob;
import org.apache.linkis.cli.application.interactor.job.jobcmd.JobCmdJob;
import org.apache.linkis.cli.application.interactor.job.once.LinkisOnceJob;
import org.apache.linkis.cli.application.interactor.job.version.VersionJob;
import org.apache.linkis.cli.application.operator.OperManager;
import org.apache.linkis.cli.application.operator.once.OnceOperBuilder;
import org.apache.linkis.cli.application.operator.ujes.LinkisOperBuilder;
Expand All @@ -49,6 +51,8 @@
public class LinkisClientApplication {
private static Logger logger = LoggerFactory.getLogger(LinkisClientApplication.class);

private static boolean showHelp = false;

public static void main(String[] args) {
/*
generate template
Expand All @@ -64,7 +68,9 @@ public static void main(String[] args) {
} catch (CommandException e) {
CmdTemplate template = CmdTemplateFactory.getTemplateOri(e.getCmdType());
if (template != null) {
printHelp(template);
HelpInfoModel model = new HelpInfoModel();
model.buildModel(ctx.getTemplate());
new HelpPresenter().present(model);
}
LoggerManager.getInformationLogger().error("Failed to build CliCtx", e);
System.exit(-1);
Expand All @@ -80,7 +86,11 @@ public static void main(String[] args) {
run job
*/
Job job;
if (isJobCmd(ctx)) {
if (isVersionCmd(ctx)) {
job = new VersionJob();
} else if (isHelp(ctx)) {
job = new HelpJob();
} else if (isJobCmd(ctx)) {
job = new JobCmdJob();
} else if (isOnceCmd(ctx)) {
job = new LinkisOnceJob();
Expand Down Expand Up @@ -134,21 +144,34 @@ public Map<String, String> getExtraMessage() {
}
}

private static void printHelp(CmdTemplate template) {
HelpInfoModel model = new HelpInfoModel();
model.buildModel(template);
new HelpPresenter().present(model);
}

private static void printIndicator(JobResult jobResult) {
if (jobResult.isSuccess()) {
LoggerManager.getPlaintTextLogger().info(CliConstants.SUCCESS_INDICATOR);
} else {
LoggerManager.getPlaintTextLogger().info(jobResult.getMessage());
StringBuilder b = new StringBuilder();
for (Map.Entry<String, String> e : jobResult.getExtraMessage().entrySet()) {
b.append(e.getKey()).append(":").append(e.getValue()).append(System.lineSeparator());
}
LoggerManager.getPlaintTextLogger().info(b.toString());
LoggerManager.getPlaintTextLogger().info(CliConstants.FAILURE_INDICATOR);
LoggerManager.getPlaintTextLogger().error(jobResult.getMessage());
}
}

private static Boolean isHelp(CliCtx ctx) {
if (ctx.getVarAccess().hasVar(CliKeys.LINKIS_CLIENT_HELP_OPT)) {
return true;
}
return false;
}

private static Boolean isVersionCmd(CliCtx ctx) {
if (ctx.getVarAccess().hasVar(CliKeys.VERSION)) {
return true;
}
return false;
}

private static Boolean isJobCmd(CliCtx ctx) {
if (ctx.getVarAccess().hasVar(CliKeys.LINKIS_CLIENT_KILL_OPT)
|| ctx.getVarAccess().hasVar(CliKeys.LINKIS_CLIENT_STATUS_OPT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class CliKeys {
public static final String DEFAULT_CONFIG_FILE_NAME_KEY = "conf.file";
public static final String LINUX_USER_KEY = "user.name";

public static final String VERSION = "cli.version";

/** Configurable */
/*
execution type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ public class LinkisKeys {
public static final String KEY_YARN_QUEUE = "wds.linkis.rm.yarnqueue";
public static final String KEY_HIVE_RESULT_DISPLAY_TBALE =
"hive.resultset.use.unique.column.names";
public static final String CLI_VERSION = "cli.version";
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ public class UniversalCmdTemplate extends AbstractCmdTemplate implements Cloneab
"specify jobContent map. Input format: -jobContentMap key1=value1 -jobContentMap key2=value2",
true);

protected Flag versionFlag =
flag(
CliKeys.VERSION,
CliKeys.VERSION,
new String[] {"--version"},
"show version",
true,
false);

protected Parameter<String[]> argumentsParas =
parameter(
CliKeys.LINKIS_CLIENT_COMMON,
Expand All @@ -286,6 +295,9 @@ public UniversalCmdTemplate() {

@Override
public void checkParams() throws CommandException {
if (versionFlag.hasVal()) {
return;
}
int cnt = 0;
if (statusOpt.hasVal()) {
cnt++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.linkis.cli.application.interactor.job.help;

import org.apache.linkis.cli.application.entity.context.CliCtx;
import org.apache.linkis.cli.application.entity.job.Job;
import org.apache.linkis.cli.application.entity.job.JobResult;
import org.apache.linkis.cli.application.present.HelpPresenter;
import org.apache.linkis.cli.application.present.model.HelpInfoModel;

import java.util.HashMap;
import java.util.Map;

public class HelpJob implements Job {
private CliCtx ctx;

@Override
public void build(CliCtx ctx) {
this.ctx = ctx;
}

@Override
public JobResult run() {
HelpInfoModel model = new HelpInfoModel();
model.buildModel(ctx.getTemplate());
new HelpPresenter().present(model);
return new JobResult() {
@Override
public Boolean isSuccess() {
return true;
}

@Override
public String getMessage() {
return "";
}

@Override
public Map<String, String> getExtraMessage() {
return new HashMap<>();
}
};
}

@Override
public void onDestroy() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.linkis.cli.application.interactor.job.version;

import org.apache.linkis.cli.application.constants.CliKeys;
import org.apache.linkis.cli.application.entity.context.CliCtx;
import org.apache.linkis.cli.application.entity.job.Job;
import org.apache.linkis.cli.application.entity.job.JobResult;
import org.apache.linkis.cli.application.utils.LoggerManager;

import java.util.HashMap;
import java.util.Map;

public class VersionJob implements Job {
private CliCtx ctx;

@Override
public void build(CliCtx cliCtx) {
this.ctx = cliCtx;
}

@Override
public JobResult run() {
String version = (String) ctx.getExtraMap().get(CliKeys.VERSION);
Map<String, String> extraMap = new HashMap<>();
extraMap.put(CliKeys.VERSION, version);
LoggerManager.getPlaintTextLogger().info("Version=" + version);
return new VersionJobResult(true, "ok", extraMap);
}

@Override
public void onDestroy() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@
* limitations under the License.
*/

package org.apache.linkis.cli.application.entity.version;
package org.apache.linkis.cli.application.interactor.job.version;

public interface Version {}
import org.apache.linkis.cli.application.entity.job.JobResult;

import java.util.Map;

public class VersionJobResult implements JobResult {
private Boolean success;
private String message;
private Map<String, String> extraMsg;

public VersionJobResult(Boolean success, String message, Map<String, String> extraMsg) {
this.success = success;
this.message = message;
this.extraMsg = extraMsg;
}

@Override
public Boolean isSuccess() {
return success;
}

@Override
public String getMessage() {
return message;
}

@Override
public Map<String, String> getExtraMessage() {
return extraMsg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private JobInfoResult queryJobInfoInternal(String user, String taskID)
}
String msg =
MessageFormat.format(
"Get job info failed. retry time : {0}/{1}. taskID={0}, Reason: {1}",
"Get job info failed. retry time : {0}/{1}. taskID={2}, Reason: {3}",
retryTime, MAX_RETRY_TIME, taskID, reason);

logger.debug(
Expand Down Expand Up @@ -299,7 +299,7 @@ private JobInfoResult queryJobInfoInternal(String user, String taskID)
if (jobInfoResult == null) {
reason = "JobInfoResult is null";
} else {
reason = "server returns non-zero status-code";
reason = "server returns non-zero status-code. ";
reason += jobInfoResult.getMessage();
}
String msg =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# 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.
#

cli.version=${project.version}

0 comments on commit 1d3d752

Please sign in to comment.