Limit scopes and rename bean (#1991)

This limits the scope of all cli related classes, so that
they cannot be used outside of cli context and therefore
cannot confuse other developers.

Secondly, we rename RepositoryCommandDto to
RepositoryCommandBean, because we have no data transfers
here and the name might be confusing otherwise.
This commit is contained in:
René Pfeuffer
2022-04-06 09:16:03 +02:00
committed by GitHub
parent 4823ed59a2
commit dd3b616ba4
9 changed files with 32 additions and 31 deletions

View File

@@ -31,7 +31,7 @@ import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class RepositoryCommandDto {
class RepositoryCommandBean {
private String name;
private String namespace;
private String type;

View File

@@ -40,7 +40,7 @@ import javax.validation.constraints.Email;
@CommandLine.Command(name = "create")
@ParentCommand(value = RepositoryCommand.class)
public class RepositoryCreateCommand implements Runnable {
class RepositoryCreateCommand implements Runnable {
@CommandLine.Mixin
private final RepositoryTemplateRenderer templateRenderer;

View File

@@ -36,7 +36,7 @@ import java.util.Collections;
@CommandLine.Command(name = "delete", aliases = "rm")
@ParentCommand(RepositoryCommand.class)
public class RepositoryDeleteCommand implements Runnable {
class RepositoryDeleteCommand implements Runnable {
private static final String PROMPT_TEMPLATE = "{{i18n.repoDeletePrompt}}";

View File

@@ -35,7 +35,7 @@ import javax.inject.Inject;
@ParentCommand(value = RepositoryCommand.class)
@CommandLine.Command(name = "get")
public class RepositoryGetCommand implements Runnable {
class RepositoryGetCommand implements Runnable {
@CommandLine.Parameters(paramLabel = "namespace/name", index = "0")
private String repository;

View File

@@ -38,7 +38,7 @@ import java.util.stream.Collectors;
@ParentCommand(value = RepositoryCommand.class)
@CommandLine.Command(name = "list", aliases = "ls")
public class RepositoryListCommand implements Runnable {
class RepositoryListCommand implements Runnable {
@CommandLine.Mixin
private final TemplateRenderer templateRenderer;
@@ -69,16 +69,16 @@ public class RepositoryListCommand implements Runnable {
@Override
public void run() {
Collection<RepositoryCommandDto> dtos = manager.getAll().stream().map(mapper::map).collect(Collectors.toList());
Collection<RepositoryCommandBean> beans = manager.getAll().stream().map(mapper::map).collect(Collectors.toList());
if (useShortTemplate) {
templateRenderer.renderToStdout(SHORT_TEMPLATE, ImmutableMap.of("repos", dtos));
templateRenderer.renderToStdout(SHORT_TEMPLATE, ImmutableMap.of("repos", beans));
} else {
Table table = templateRenderer.createTable();
table.addHeader("repoName", "repoType", "repoUrl");
for (RepositoryCommandDto dto : dtos) {
table.addRow(dto.getNamespace() + "/" + dto.getName(), dto.getType(), dto.getUrl());
for (RepositoryCommandBean bean : beans) {
table.addRow(bean.getNamespace() + "/" + bean.getName(), bean.getType(), bean.getUrl());
}
templateRenderer.renderToStdout(TABLE_TEMPLATE, ImmutableMap.of("rows", table, "repos", dtos));
templateRenderer.renderToStdout(TABLE_TEMPLATE, ImmutableMap.of("rows", table, "repos", beans));
}
}

View File

@@ -37,7 +37,7 @@ import javax.validation.constraints.Email;
@ParentCommand(value = RepositoryCommand.class)
@CommandLine.Command(name = "modify")
public class RepositoryModifyCommand implements Runnable {
class RepositoryModifyCommand implements Runnable {
@CommandLine.Mixin
private final RepositoryTemplateRenderer templateRenderer;

View File

@@ -35,7 +35,7 @@ import sonia.scm.template.TemplateEngineFactory;
import javax.inject.Inject;
import java.util.Collections;
public class RepositoryTemplateRenderer extends TemplateRenderer {
class RepositoryTemplateRenderer extends TemplateRenderer {
private static final String DETAILS_TABLE_TEMPLATE = String.join("\n",
"{{#rows}}",
@@ -57,16 +57,16 @@ public class RepositoryTemplateRenderer extends TemplateRenderer {
public void render(Repository repository) {
Table table = createTable();
RepositoryCommandDto dto = mapper.map(repository);
table.addLabelValueRow("repoNamespace", dto.getNamespace());
table.addLabelValueRow("repoName", dto.getName());
table.addLabelValueRow("repoType", dto.getType());
table.addLabelValueRow("repoContact", dto.getContact());
table.addLabelValueRow("repoCreationDate", dto.getCreationDate());
table.addLabelValueRow("repoLastModified", dto.getLastModified());
table.addLabelValueRow("repoUrl", dto.getUrl());
table.addLabelValueRow("repoDescription", dto.getDescription());
renderToStdout(DETAILS_TABLE_TEMPLATE, ImmutableMap.of("rows", table, "repo", dto));
RepositoryCommandBean bean = mapper.map(repository);
table.addLabelValueRow("repoNamespace", bean.getNamespace());
table.addLabelValueRow("repoName", bean.getName());
table.addLabelValueRow("repoType", bean.getType());
table.addLabelValueRow("repoContact", bean.getContact());
table.addLabelValueRow("repoCreationDate", bean.getCreationDate());
table.addLabelValueRow("repoLastModified", bean.getLastModified());
table.addLabelValueRow("repoUrl", bean.getUrl());
table.addLabelValueRow("repoDescription", bean.getDescription());
renderToStdout(DETAILS_TABLE_TEMPLATE, ImmutableMap.of("rows", table, "repo", bean));
}
public void renderInvalidInputError() {

View File

@@ -26,6 +26,7 @@ package sonia.scm.repository.cli;
import com.google.common.annotations.VisibleForTesting;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ObjectFactory;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.RepositoryService;
@@ -43,16 +44,17 @@ public abstract class RepositoryToRepositoryCommandDtoMapper {
@Inject
private RepositoryServiceFactory serviceFactory;
public abstract RepositoryCommandDto map(Repository modelObject);
@Mapping(target = "url", ignore = true)
abstract RepositoryCommandBean map(Repository modelObject);
@ObjectFactory
RepositoryCommandDto createDto(Repository repository) {
RepositoryCommandDto dto = new RepositoryCommandDto();
RepositoryCommandBean createBean(Repository repository) {
RepositoryCommandBean bean = new RepositoryCommandBean();
try (RepositoryService service = serviceFactory.create(repository)) {
Optional<ScmProtocol> protocolUrl = service.getSupportedProtocols().filter(p -> p.getType().equals("http")).findFirst();
protocolUrl.ifPresent(scmProtocol -> dto.setUrl(scmProtocol.getUrl()));
protocolUrl.ifPresent(scmProtocol -> bean.setUrl(scmProtocol.getUrl()));
}
return dto;
return bean;
}
String mapTimestampToISODate(Long timestamp) {