Skip to content

Commit

Permalink
feat: output updates (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored Apr 30, 2024
1 parent 1433759 commit de67290
Show file tree
Hide file tree
Showing 51 changed files with 237 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,17 @@ public void updateSource(Long sourceId, UpdateFileRequest request) throws Respon
}

@Override
public void addSource(AddFileRequest request) throws ResponseException {
public FileInfo addSource(AddFileRequest request) throws ResponseException {
Map<BiPredicate<String, String>, ResponseException> errorHandlers = new LinkedHashMap<BiPredicate<String, String>, ResponseException>() {{
put((code, message) -> message.contains("File from storage with id #" + request.getStorageId() + " was not found"), new RepeatException());
put((code, message) -> StringUtils.contains(message, "Name must be unique"), new ExistsResponseException());
put((code, message) -> StringUtils.contains(message, "Invalid SRX specified"), new ResponseException("Invalid SRX file specified"));
put((code, message) -> StringUtils.containsAny(message, "isEmpty", "Value is required and can't be empty"), new EmptyFileException("Value is required and can't be empty"));
}};
executeRequestWithPossibleRetry(
return executeRequestWithPossibleRetry(
errorHandlers,
() -> this.client.getSourceFilesApi()
.addFile(this.projectId, request));
.addFile(this.projectId, request).getData());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ default CrowdinProjectFull downloadFullProject() {

void updateSource(Long sourceId, UpdateFileRequest request) throws ResponseException;

void addSource(AddFileRequest request) throws ResponseException;
FileInfo addSource(AddFileRequest request) throws ResponseException;

UploadStringsProgress addSourceStringsBased(UploadStringsRequest request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
Map<String, Long> branches = client.listBranches().stream()
.collect(Collectors.toMap(Branch::getName, Branch::getId));
if (!branches.containsKey(name)) {
client.addBranch(RequestBuilder.addBranch(name, title, exportPattern, priority));
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.branch"), name)));
Branch branch = client.addBranch(RequestBuilder.addBranch(name, title, exportPattern, priority));
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.branch.list"), branch.getId(), branch.getName())));
} else {
out.println(SKIPPED.withIcon(String.format(RESOURCE_BUNDLE.getString("message.branch_already_exists"), name)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.crowdin.cli.utils.console.ConsoleSpinner;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

class BranchListAction implements NewAction<ProjectProperties, ProjectClient> {

Expand All @@ -30,7 +29,7 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
.forEach((key, value) -> {
String name = value.getName().replaceAll("^[/\\\\]+", "");
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.branch.list"), key, name)));
out.println(String.format(RESOURCE_BUNDLE.getString("message.branch.list"), key, name));
} else {
out.println(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public void act(Outputter out, ProjectProperties pb, ClientBundle client) {
}

if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.bundle.added"), bundle.getId(), bundle.getName())));
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.bundle.list"), bundle.getId(),
bundle.getFormat(),
bundle.getExportPattern(), bundle.getName())));
} else {
out.println(String.valueOf(bundle.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public void act(Outputter out, ProjectProperties pb, ClientBundle client) {
for (Bundle bundle : bundles) {
if (!plainView) {
out.println(String.format(RESOURCE_BUNDLE.getString("message.bundle.list"), bundle.getId(),
bundle.getName(),
bundle.getFormat(), bundle.getExportPattern()));
bundle.getFormat(),
bundle.getExportPattern(), bundle.getName()));
} else {
out.println(bundle.getId() + " " + bundle.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void act(Outputter out, ProjectProperties pb, ClientComment client) {
RESOURCE_BUNDLE.getString("message.comment.list.verbose"),
comment.getId(),
commentText,
comment.getLanguageId(),
Objects.toString(comment.getIssueType(), StringUtils.EMPTY),
Objects.toString(comment.getIssueStatus(), StringUtils.EMPTY).toLowerCase()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void act(Outputter out, ProjectProperties pb, ClientDistribution client)
}

if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.distribution.added"), distribution.getName())));
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.distribution.list"), distribution.getHash(), distribution.getName(), distribution.getExportMode())));
} else {
out.println(String.valueOf(distribution.getName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

class FileListAction implements NewAction<ProjectProperties, ProjectClient> {

Expand Down Expand Up @@ -80,12 +79,12 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
if (isVerbose) {
if (entry.getValue() instanceof File) {
File file = (File) entry.getValue();
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.file.list_verbose_full"), entry.getValue().getId(), entry.getKey(), entry.getValue().getType(), file.getParserVersion(), file.getRevisionId())));
out.println(String.format(RESOURCE_BUNDLE.getString("message.file.list_verbose_full"), entry.getValue().getId(), entry.getKey(), entry.getValue().getType(), file.getParserVersion(), file.getRevisionId()));
} else {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.file.list_verbose"), entry.getValue().getId(), entry.getKey(), entry.getValue().getType())));
out.println(String.format(RESOURCE_BUNDLE.getString("message.file.list_verbose"), entry.getValue().getId(), entry.getKey(), entry.getValue().getType()));
}
} else {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.file.list"), entry.getValue().getId(), entry.getKey())));
out.println(String.format(RESOURCE_BUNDLE.getString("message.file.list"), entry.getValue().getId(), entry.getKey()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.functionality.*;
import com.crowdin.cli.properties.FileBean;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.PlaceholderUtil;
import com.crowdin.cli.utils.Utils;
Expand Down Expand Up @@ -153,7 +152,12 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
branch.ifPresent(b -> request.setBranchId(b.getId()));
excludedLanguageNames.ifPresent(request::setExcludedTargetLanguages);
try {
client.addSource(request);
FileInfo fileInfo = client.addSource(request);
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.file.list"), fileInfo.getId(), fileFullPath)));
} else {
out.println(fileFullPath);
}
} catch (ExistsResponseException e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_already_exists"), fileFullPath));
} catch (Exception e) {
Expand Down Expand Up @@ -193,11 +197,11 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
ConsoleSpinner.update(String.format(RESOURCE_BUNDLE.getString("message.spinner.uploading_strings_percents"), 100));
return uploadStrings;
});
}
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.uploading_file"), fileFullPath)));
} else {
out.println(fileFullPath);
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.uploading_file"), fileFullPath)));
} else {
out.println(fileFullPath);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.URL;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

class GlossaryDownloadAction implements NewAction<BaseProperties, ClientGlossary> {

Expand All @@ -43,7 +44,7 @@ public void act(Outputter out, BaseProperties pb, ClientGlossary client) {
}
GlossaryExportStatus status = this.buildGlossary(out, client, targetGlossary.getId(), RequestBuilder.exportGlossary(format));
downloadGlossary(client, targetGlossary.getId(), status.getIdentifier());
out.println(String.format(RESOURCE_BUNDLE.getString("message.glossary.download_success"), to));
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.glossary.download_success"), to)));
}

private Glossary getGlossary(ClientGlossary client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public void act(Outputter out, BaseProperties pb, ClientGlossary client) {
List<Glossary> glossaries = client.listGlossaries();
for (Glossary glossary : glossaries) {
if (!plainView) {
out.println(OK.withIcon(
String.format(RESOURCE_BUNDLE.getString("message.glossary.list"), glossary.getId(), glossary.getName(), glossary.getTerms())));
out.println(String.format(RESOURCE_BUNDLE.getString("message.glossary.list"), glossary.getId(), glossary.getName(), glossary.getTerms()));
if (isVerbose && mayHaveTerms(glossary)) {
try {
List<Term> terms = client.listTerms(glossary.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void act(Outputter out, ProjectProperties properties, ClientLabel client)
Label label = client.addLabel(request);
if (!plainView) {
out.println(ExecutionStatus.OK.withIcon(
String.format(RESOURCE_BUNDLE.getString("message.label.added"), label.getId(), label.getTitle())
String.format(RESOURCE_BUNDLE.getString("message.label.list"), label.getId(), label.getTitle())
));
} else {
out.println(title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
if (!plainView) {
languages.stream()
.map(lang -> String.format(RESOURCE_BUNDLE.getString("message.language.list"), this.getCode(langMapping, lang), lang.getName()))
.map(OK::withIcon)
.forEach(out::println);
} else {
languages.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl
for (Screenshot screenshot : screenshots) {
if (!plainView) {
out.println(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"),
screenshot.getId(), screenshot.getName(), screenshot.getTagsCount()));
screenshot.getId(), screenshot.getTagsCount(), screenshot.getName()));
} else {
out.println(screenshot.getId() + " " + screenshot.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,27 @@ class ScreenshotUploadAction implements NewAction<ProjectProperties, ClientScree

@Override
public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) {
Screenshot screenshot;
List<Screenshot> screenshotList = client.listScreenshots(null);
Optional<Screenshot> existingScreenshot = screenshotList.stream()
.filter((s) -> file.getName().equals(s.getName())).findFirst();
.filter((s) -> file.getName().equals(s.getName())).findFirst();
if (existingScreenshot.isPresent()) {
UpdateScreenshotRequest request = new UpdateScreenshotRequest();
request.setStorageId(uploadToStorage(file));
request.setName(file.getName());
try {
screenshot = client.updateScreenshot(existingScreenshot.get().getId(), request);
Screenshot screenshot = client.updateScreenshot(existingScreenshot.get().getId(), request);
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"),
screenshot.getId(), screenshot.getTagsCount(), screenshot.getName())));
} else {
out.println(file.getName());
}
} catch (Exception e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_updated"), request), e);
}
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.updated"), screenshot.getName())));
} else {
out.println(String.valueOf(screenshot.getName()));
}
return;
}

AddScreenshotRequest request = new AddScreenshotRequest();
CrowdinProjectFull project = ConsoleSpinner.execute(
out,
Expand All @@ -77,32 +78,39 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl
() -> this.projectClient.downloadFullProject());
if (nonNull(branchName)) {
Branch branch = project.findBranchByName(branchName)
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), branchName)));
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), branchName)));
request.setBranchId(branch.getId());
}
if (nonNull(pathToSourceFile)) {
final String normalizedPath = Utils.toUnixPath(Utils.sepAtStart(pathToSourceFile));
FileInfo fileInfo = project.getFileInfos().stream()
.filter(f -> normalizedPath.equals(f.getPath())).findFirst()
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), pathToSourceFile)));
.filter(f -> normalizedPath.equals(f.getPath())).findFirst()
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), pathToSourceFile)));
request.setFileId(fileInfo.getId());
}
if (nonNull(directoryPath)) {
final String normalizedPath = Utils.toUnixPath(Utils.sepAtStart(directoryPath));
Directory directory = project.getDirectories().values().stream()
.filter(d -> normalizedPath.equals(d.getPath())).findFirst()
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.dir_not_exists"), directoryPath)));
.filter(d -> normalizedPath.equals(d.getPath())).findFirst()
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.dir_not_exists"), directoryPath)));
request.setDirectoryId(directory.getId());
}
if (nonNull(labelNames) && !labelNames.isEmpty()) {
request.setLabelIds(prepareLabelIds());
}

request.setStorageId(uploadToStorage(file));
request.setName(file.getName());
request.setAutoTag(autoTag);

try {
client.uploadScreenshot(request);
Screenshot screenshot = client.uploadScreenshot(request);
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"),
screenshot.getId(), screenshot.getTagsCount(), screenshot.getName())));
} else {
out.println(file.getName());
}
} catch (AutoTagInProgressException e) {
if (!plainView) {
out.println(WARNING.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.not_auto-tagged"), file.getName())));
Expand All @@ -112,11 +120,6 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl
} catch (Exception e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_uploaded"), request), e);
}
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.uploaded"), file.getName())));
} else {
out.println(file.getName());
}
}

private Long uploadToStorage(File fileToUpload) {
Expand All @@ -131,12 +134,12 @@ private Long uploadToStorage(File fileToUpload) {

private Long[] prepareLabelIds() {
Map<String, Long> labels = projectClient.listLabels().stream()
.collect(Collectors.toMap(Label::getTitle, Label::getId));
.collect(Collectors.toMap(Label::getTitle, Label::getId));
labelNames.stream()
.distinct()
.forEach(labelName -> labels.computeIfAbsent(labelName, (title) -> projectClient.addLabel(RequestBuilder.addLabel(title)).getId()));
.distinct()
.forEach(labelName -> labels.computeIfAbsent(labelName, (title) -> projectClient.addLabel(RequestBuilder.addLabel(title)).getId()));
return labelNames.stream()
.map(labels::get)
.toArray(Long[]::new);
.map(labels::get)
.toArray(Long[]::new);
}
}
Loading

0 comments on commit de67290

Please sign in to comment.